• Like
  • Save
Groovy 1.8の新機能について
Upcoming SlideShare
Loading in...5
×
 

Groovy 1.8の新機能について

on

  • 6,085 views

Groovy 1.8の新機能についてです。

Groovy 1.8の新機能についてです。
JGGUG G*Workshop 6/17の発表資料です。

Statistics

Views

Total Views
6,085
Views on SlideShare
2,827
Embed Views
3,258

Actions

Likes
2
Downloads
16
Comments
0

7 Embeds 3,258

http://d.hatena.ne.jp 3190
http://uehaj.hatenablog.com 40
url_unknown 11
http://webcache.googleusercontent.com 8
http://www.slideshare.net 7
https://www.google.co.jp 1
http://news.google.com 1
More...

Accessibility

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • 正直、Groovyから書きなおすのは、たいへんでした。\n
  • \n
  • 「何を処理するか」という最低限の指示は上で十分、\n他はメタ情報。\n
  • 各行を解説していきます。すんなりと読めばコードの意味に対応する。\n
  • 各行を解説していきます。すんなりと読めばコードの意味に対応する。\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • XmlSlurper,PerserのようにDOMとか出てこない。\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Groovy 1.8の新機能について Groovy 1.8の新機能について Presentation Transcript

  • Groovy1.8 Groovy
  • Slide # 2 JGGUG G*Workshop 17th / 2011.6.17
  • http://d.hatena.ne.jp/uehaj/Slide # 3 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 4 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 5 JGGUG G*Workshop 17th / 2011.6.17
  • April 28, 2011Slide # 6 JGGUG G*Workshop 17th / 2011.6.17
  • April 28, 2011Slide # 6 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # JGGUG G*Workshop 17th / 2011.6.17
  • $ cat input.txt That that is is that that is not is not is that it it is $ java WordCount input.txt 1: [That] 2: [not] 2: [it] 4: [that] 6: [is]Slide # 8 JGGUG G*Workshop 17th / 2011.6.17
  • Java WordCount:48      Set<Map.Entry<String, Integer>> entrySet =import java.util.Comparator; map.entrySet();import java.util.HashMap;      Object[] list = entrySet.toArray();import java.util.Map;     Comparator comp = new Comparator(){import java.util.Set;        public int compare(Object o1, Object o2)import java.util.List; {import java.util.Arrays;          Map.Entry<String, Integer> e1 =import java.io.FileReader; (Map.Entry<String, Integer>) o1;import java.io.BufferedReader;          Map.Entry<String, Integer> e2 =import java.io.FileNotFoundException; (Map.Entry<String, Integer>) o2;import java.io.IOException;         return e1.getValue() - e2.getValue();         }public class WordCount {       };  @SuppressWarnings(value = "unchecked")       Arrays.sort(list, comp);  public static void main(String[] args) {       for (Object it: list) {    FileReader fis = null;         Map.Entry<String, Integer> entry =    BufferedReader br = null; (Map.Entry<String, Integer>)it;    try {         System.out.println(entry.getValue() + ":      HashMap<String, Integer> map = new ["+entry.getKey()+"]");HashMap<String, Integer>();       }      fis = new FileReader(args[0]);     }      br = new BufferedReader(fis);     catch (IOException e) {      String line;       try {if (br != null)      while ((line = br.readLine()) != null) { br.close();}catch(IOException ioe){}        for (String it: line.split("s+")) {       try {if (fis !=          map.put(it, (map.get(it)==null) ? 1 : null)fis.close();}catch(IOException ioe){}(map.get(it) + 1));       e.printStackTrace();        }     }      }   } }Slide # 9 JGGUG G*Workshop 17th / 2011.6.17
  • Groovy WordCount(9 ) def map = [:].withDefault{0} new File(args[0]).eachLine {   it.split(/s+/).each {     map[it]++    } } map.entrySet().sort{it.value}.each {   println "${it.value}: [${it.key}]" }Slide # 10 JGGUG G*Workshop 17th / 2011.6.17
  • Java             Set<Map.Entry<String, Integer>>import java.util.Comparator; entrySet = map.entrySet();import java.util.HashMap;             Object[] list = entrySet.toArray();import java.util.Map;             Comparator comp = new Comparator(){import java.util.Set;                 public int compare(Object o1,import java.util.List; Object o2) {import java.util.Arrays;                     Map.Entry<String, Integer> e1import java.io.FileReader; = (Map.Entry<String, Integer>) o1;import java.io.BufferedReader;                     Map.Entry<String, Integer> e2import java.io.FileNotFoundException; = (Map.Entry<String, Integer>) o2;import java.io.IOException;                     return e1.getValue() - e2.getValue(); public class WordCount {                 }     @SuppressWarnings(value = "unchecked")             };     public static void main(String[] args) {             Arrays.sort(list, comp);         FileReader fis = null;             for (Object it: list) {         BufferedReader br = null;                 Map.Entry<String, Integer> entry =         try { (Map.Entry<String, Integer>)it;             HashMap<String, Integer> map = new                  HashMap<String, Integer>(); System.out.println(entry.getValue() + ":             fis = new FileReader(args[0]); ["+entry.getKey()+"]");             br = new BufferedReader(fis);             }             String line;         }             while ((line = br.readLine()) != null)         catch (IOException e) { {             try {if (br != null)                 for (String it: line.split("s br.close();}catch(IOException ioe){} +")) {             try {if (fis !=                     map.put(it, null)fis.close();}catch(IOException ioe){} (map.get(it)==null) ? 1 : (map.get(it) + 1));             e.printStackTrace();Slide # 11       }           JGGUG G*Workshop 17th /         } 2011.6.17             }     }
  • Groovy WordCount(9 ) def map = [:].withDefault{0} // value 0 map new File(args[0]).eachLine { //   it.split(/s+/).each {     // /s+/     map[it]++                // map 1   } } map.entrySet().sort{it.value}.each {// map entrySet value   println "${it.value}: [${it.key}]"// key,value }Slide # 12 JGGUG G*Workshop 17th / 2011.6.17
  • (→4 def map = new File(args[0]).text.split(/s+/).countBy{it} map.entrySet().sort{it.value}.each {   println "${it.value}: [${it.key}]" }Slide # 13 JGGUG G*Workshop 17th / 2011.6.17
  • (→4 def map = new File(args[0]).text.split(/s+/).countBy{it} map.entrySet().sort{it.value}.each {   println "${it.value}: [${it.key}]" }Slide # 13 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 14 JGGUG G*Workshop 17th / 2011.6.17
  • AST Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • AST (curry) Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • AST AST AST AST (curry) Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • AST AST AST AST (curry) Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • AST AST AST AST (curry) Groovy 1.8.0 (GEP3) $/ /$Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • AST AST AST AST (curry) Groovy 1.8.0 GDK Groovy API GPars (GEP3) GSql $/ /$Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • AST AST AST AST (curry) Groovy 1.8.0 jar Grab GroovyDoc GDK Groovy API GPars (GEP3) GSql $/ /$Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  • Groovy 1.8.0 (GEP3) $/ /$ / /Slide # 16 JGGUG G*Workshop 17th / 2011.6.17
  • println 1+1 // m1(a1).m2(a2).m3(a3) m1 a1 m2 a2 m3 a3Slide # 17 JGGUG G*Workshop 17th / 2011.6.17
  • turn left then right p.p1 // turn(left).then(right) paint wall with red, green and yellow // paint(wall).with(red, green).and(yellow) take 3 cookies  // take(3).cookies // take(3).getCookies()    given { } when { } then { } // given({}).when({}).then({}) http://groovy.codehaus.org/Groovy+1.8+release+notesSlide # 18 JGGUG G*Workshop 17th / 2011.6.17
  • $/…/$ ///…///( ) /…/ a=$/ /$ $/Slide # 19 JGGUG G*Workshop 17th / 2011.6.17
  • AST AST Groovy 1.8.0 AST ASTSlide # 20 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 21 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 21 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 22 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 23 JGGUG G*Workshop 17th / 2011.6.17
  • import groovy.util.logging.Log @Log class MyClass {   def invoke() {     log.info info message     log.fine fine message   } }Slide # 24 JGGUG G*Workshop 17th / 2011.6.17 http://canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/
  • log.info < > < > log.isLoggabl(java.util.logging.Level.INFO) ? log.info(< >) : nullSlide # 25 JGGUG G*Workshop 17th / 2011.6.17 http://canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/
  • @TupleConstructor class MyClass { class MyClass {    def a    def a    def b    def b    def c    def c    MyClass(a,b,c) {  }     this.a=a }     this.b=b     this.c=c    }  }Slide # 26 JGGUG G*Workshop 17th / 2011.6.17 http://canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/
  • Slide # 27 JGGUG G*Workshop 17th / 2011.6.17
  • class MyClass {   @WithReadLock def () { }   @WithWriteLock def () { } }Slide # 28 JGGUG G*Workshop 17th / 2011.6.17
  • class MyClass {   @WithReadLock def () { }   @WithWriteLock def () { } }Slide # 28 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 29 JGGUG G*Workshop 17th / 2011.6.17
  • while (true) { ←   println ”xx” } while (true) {   if (java.lang.Thread.currentThread().isInterrupted()) {     throw new InterruptedException(Execution Interrupted)   }   println ”xx” }Slide # 30 JGGUG G*Workshop 17th / 2011.6.17 http://canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/
  • foo.groovy: String a=”ABC” def bar() { println a } bar() // => a class foo extends Script { void run() { String a = ”ABC” bar() } def bar() { println a } }Slide # 31 JGGUG G*Workshop 17th / 2011.6.17
  • foo.groovy: @Field String a=”ABC” def bar() { println a } bar() ==> “ABC” class foo extends Script { String a = ”ABC” void run() { bar() } def bar() { println a } }Slide # 32 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 33 JGGUG G*Workshop 17th / 2011.6.17
  • Groovy 1.8.0 (curry)Slide # 34 JGGUG G*Workshop 17th / 2011.6.17
  • @ConditionalInterrupt({ counter > 100 }) class Foo {   int counter = 0   void run() {     while (true) {       counter++ // 100     } // InterruptedException   } }Slide # 35 JGGUG G*Workshop 17th / 2011.6.17
  • // fib1 = { n ->   n <= 1 ? n : fib1(n - 1) + fib1(n - 2) } // fib2 = { n ->   n <= 1 ? n : fib2(n - 1) + fib2(n - 2) }.memoize()Slide # 36 JGGUG G*Workshop 17th / 2011.6.17
  • … … …Slide # 37 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 38 JGGUG G*Workshop 17th / 2011.6.17
  • Groovy 1.8.0 GDK Groovy API GPars GSql JSonSlide # JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 40 JGGUG G*Workshop 17th / 2011.6.17
  • !Slide # 40 JGGUG G*Workshop 17th / 2011.6.17
  • Java JSON ‘’’ {"name": "John HashMap Smith", "age": 33} ‘’’ JsonSlurper #parseText( ) ‘’’ ArrayList ["milk", "bread", "eggs"] ‘’’ 41Slide # JGGUG G*Workshop 17th / 2011.6.17
  • JSON bldr = new JsonBuilder() bldr { num 1 ‘boolean’ true arr([1,2,3]) JSonBuilde } r JSON toString() ”””{"num": 42 1,"boolean":true," arr":[1,2,3]}”””Slide # JGGUG G*Workshop 17th / 2011.6.17
  • Java bldr=new JsonBuilder() bldr([ num:1, HashMap ’boolean’:true, arr: JSonBuilde [1,2,3] ]) r JSON toString() “””{"num": 43 1,"boolean":true," arr":[1,2,3]}”””Slide # JGGUG G*Workshop 17th / 2011.6.17
  • JSON { "num": 1, JSonBuilde toPrettyString() "’boolean’": true, r "arr": [ 1, 2, 3 ] 44 }Slide # JGGUG G*Workshop 17th / 2011.6.17
  • Groovy 1.8.0Slide # 45 JGGUG G*Workshop 17th / 2011.6.17
  • public class FibBench {   static int fib(int n) {     return n <= 1 ? n : fib(n - 1) + fib(n - 2);   } } (ms) Java 1 Groovy 1.8.0 2664 2.9 Groovy 1.7.10 26702 28.6 Groovy++ 0.4.230_1.8.0 1067 1.14 Java SE 1.6.0_22 933 1.0Slide # 46 JGGUG G*Workshop 17th / 2011.6.17
  • ( 10 !) 1.8.0 int Groovy++ Groovy++ (Java !) Groovy 1.8Slide # 47 JGGUG G*Workshop 17th / 2011.6.17
  • Groovy 1.8.0 jar Grab GroovyDocSlide # 48 JGGUG G*Workshop 17th / 2011.6.17
  • $ java -jar /tool/groovy-1.8.0/target/ install/embeddable/groovy-all-1.8.0.jar fib.groovy time=2542 time=34Slide # 49 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 50 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 51 JGGUG G*Workshop 17th / 2011.6.17
  • Slide # 52 JGGUG G*Workshop 17th / 2011.6.17