Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Groovy 1.8の新機能について

7,953 views

Published on

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

Published in: Technology, News & Politics
  • Be the first to comment

Groovy 1.8の新機能について

  1. 1. Groovy1.8 Groovy
  2. 2. Slide # 2 JGGUG G*Workshop 17th / 2011.6.17
  3. 3. http://d.hatena.ne.jp/uehaj/Slide # 3 JGGUG G*Workshop 17th / 2011.6.17
  4. 4. Slide # 4 JGGUG G*Workshop 17th / 2011.6.17
  5. 5. Slide # 5 JGGUG G*Workshop 17th / 2011.6.17
  6. 6. April 28, 2011Slide # 6 JGGUG G*Workshop 17th / 2011.6.17
  7. 7. April 28, 2011Slide # 6 JGGUG G*Workshop 17th / 2011.6.17
  8. 8. Slide # JGGUG G*Workshop 17th / 2011.6.17
  9. 9. $ 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
  10. 10. 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
  11. 11. 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
  12. 12. 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             }     }
  13. 13. 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
  14. 14. (→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
  15. 15. (→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
  16. 16. Slide # 14 JGGUG G*Workshop 17th / 2011.6.17
  17. 17. AST Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  18. 18. AST (curry) Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  19. 19. AST AST AST AST (curry) Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  20. 20. AST AST AST AST (curry) Groovy 1.8.0Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  21. 21. AST AST AST AST (curry) Groovy 1.8.0 (GEP3) $/ /$Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  22. 22. AST AST AST AST (curry) Groovy 1.8.0 GDK Groovy API GPars (GEP3) GSql $/ /$Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
  23. 23. 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
  24. 24. Groovy 1.8.0 (GEP3) $/ /$ / /Slide # 16 JGGUG G*Workshop 17th / 2011.6.17
  25. 25. println 1+1 // m1(a1).m2(a2).m3(a3) m1 a1 m2 a2 m3 a3Slide # 17 JGGUG G*Workshop 17th / 2011.6.17
  26. 26. 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
  27. 27. $/…/$ ///…///( ) /…/ a=$/ /$ $/Slide # 19 JGGUG G*Workshop 17th / 2011.6.17
  28. 28. AST AST Groovy 1.8.0 AST ASTSlide # 20 JGGUG G*Workshop 17th / 2011.6.17
  29. 29. Slide # 21 JGGUG G*Workshop 17th / 2011.6.17
  30. 30. Slide # 21 JGGUG G*Workshop 17th / 2011.6.17
  31. 31. Slide # 22 JGGUG G*Workshop 17th / 2011.6.17
  32. 32. Slide # 23 JGGUG G*Workshop 17th / 2011.6.17
  33. 33. 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/
  34. 34. 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/
  35. 35. @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/
  36. 36. Slide # 27 JGGUG G*Workshop 17th / 2011.6.17
  37. 37. class MyClass {   @WithReadLock def () { }   @WithWriteLock def () { } }Slide # 28 JGGUG G*Workshop 17th / 2011.6.17
  38. 38. class MyClass {   @WithReadLock def () { }   @WithWriteLock def () { } }Slide # 28 JGGUG G*Workshop 17th / 2011.6.17
  39. 39. Slide # 29 JGGUG G*Workshop 17th / 2011.6.17
  40. 40. 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/
  41. 41. 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
  42. 42. 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
  43. 43. Slide # 33 JGGUG G*Workshop 17th / 2011.6.17
  44. 44. Groovy 1.8.0 (curry)Slide # 34 JGGUG G*Workshop 17th / 2011.6.17
  45. 45. @ConditionalInterrupt({ counter > 100 }) class Foo {   int counter = 0   void run() {     while (true) {       counter++ // 100     } // InterruptedException   } }Slide # 35 JGGUG G*Workshop 17th / 2011.6.17
  46. 46. // 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
  47. 47. … … …Slide # 37 JGGUG G*Workshop 17th / 2011.6.17
  48. 48. Slide # 38 JGGUG G*Workshop 17th / 2011.6.17
  49. 49. Groovy 1.8.0 GDK Groovy API GPars GSql JSonSlide # JGGUG G*Workshop 17th / 2011.6.17
  50. 50. Slide # 40 JGGUG G*Workshop 17th / 2011.6.17
  51. 51. !Slide # 40 JGGUG G*Workshop 17th / 2011.6.17
  52. 52. Java JSON ‘’’ {"name": "John HashMap Smith", "age": 33} ‘’’ JsonSlurper #parseText( ) ‘’’ ArrayList ["milk", "bread", "eggs"] ‘’’ 41Slide # JGGUG G*Workshop 17th / 2011.6.17
  53. 53. 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
  54. 54. 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
  55. 55. JSON { "num": 1, JSonBuilde toPrettyString() "’boolean’": true, r "arr": [ 1, 2, 3 ] 44 }Slide # JGGUG G*Workshop 17th / 2011.6.17
  56. 56. Groovy 1.8.0Slide # 45 JGGUG G*Workshop 17th / 2011.6.17
  57. 57. 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
  58. 58. ( 10 !) 1.8.0 int Groovy++ Groovy++ (Java !) Groovy 1.8Slide # 47 JGGUG G*Workshop 17th / 2011.6.17
  59. 59. Groovy 1.8.0 jar Grab GroovyDocSlide # 48 JGGUG G*Workshop 17th / 2011.6.17
  60. 60. $ 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
  61. 61. Slide # 50 JGGUG G*Workshop 17th / 2011.6.17
  62. 62. Slide # 51 JGGUG G*Workshop 17th / 2011.6.17
  63. 63. Slide # 52 JGGUG G*Workshop 17th / 2011.6.17

×