More Related Content Similar to Groovy 1.8の新機能について (20) More from Uehara Junji (20) Groovy 1.8の新機能について2. Slide # 2 JGGUG G*Workshop 17th / 2011.6.17
4. Slide # 4 JGGUG G*Workshop 17th / 2011.6.17
5. Slide # 5 JGGUG G*Workshop 17th / 2011.6.17
8. Slide # JGGUG G*Workshop 17th / 2011.6.17
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. 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. 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. 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> 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") };
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. 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. (→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. (→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
17. AST
Groovy 1.8.0
Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
18. AST
(curry)
Groovy 1.8.0
Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
19. AST
AST
AST
AST
(curry)
Groovy 1.8.0
Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
20. AST
AST
AST
AST
(curry)
Groovy 1.8.0
Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
21. AST
AST
AST
AST
(curry)
Groovy 1.8.0
(GEP3)
$/ /$
Slide # 15 JGGUG G*Workshop 17th / 2011.6.17
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. 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. Groovy 1.8.0
(GEP3)
$/ /$
/
/
Slide # 16 JGGUG G*Workshop 17th / 2011.6.17
25. println 1+1 //
m1(a1).m2(a2).m3(a3)
m1 a1 m2 a2 m3 a3
Slide # 17 JGGUG G*Workshop 17th / 2011.6.17
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+notes
Slide # 18 JGGUG G*Workshop 17th / 2011.6.17
27. $/…/$
///…///( )
/…/
a=$/
/$
$/
Slide # 19 JGGUG G*Workshop 17th / 2011.6.17
28. AST
AST
Groovy 1.8.0 AST
AST
Slide # 20 JGGUG G*Workshop 17th / 2011.6.17
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. log.info < >
< >
log.isLoggabl(java.util.logging.Level.INFO)
? log.info(< >) : null
Slide # 25 JGGUG G*Workshop 17th / 2011.6.17
http://canoo.com/blog/2010/09/20/log-groovys-new-and-extensible-logging-conveniences/
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/
37. class MyClass {
@WithReadLock def () { }
@WithWriteLock def () { }
}
Slide # 28 JGGUG G*Workshop 17th / 2011.6.17
38. class MyClass {
@WithReadLock def () { }
@WithWriteLock def () { }
}
Slide # 28 JGGUG G*Workshop 17th / 2011.6.17
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. 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. 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
44. Groovy 1.8.0
(curry)
Slide # 34 JGGUG G*Workshop 17th / 2011.6.17
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. //
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. …
…
…
Slide # 37 JGGUG G*Workshop 17th / 2011.6.17
49. Groovy 1.8.0
GDK Groovy API
GPars
GSql
JSon
Slide # JGGUG G*Workshop 17th / 2011.6.17
52. Java
JSON
‘’’
{"name": "John HashMap
Smith", "age": 33}
‘’’ JsonSlurper
#parseText(
)
‘’’ ArrayList
["milk", "bread",
"eggs"]
‘’’
41
Slide # JGGUG G*Workshop 17th / 2011.6.17
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. 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. JSON
{
"num": 1,
JSonBuilde toPrettyString() "’boolean’": true,
r "arr": [
1,
2,
3
]
44 }
Slide # JGGUG G*Workshop 17th / 2011.6.17
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.0
Slide # 46 JGGUG G*Workshop 17th / 2011.6.17
58. ( 10 !)
1.8.0 int
Groovy++
Groovy++ (Java !)
Groovy 1.8
Slide # 47 JGGUG G*Workshop 17th / 2011.6.17
59. Groovy 1.8.0
jar
Grab
GroovyDoc
Slide # 48 JGGUG G*Workshop 17th / 2011.6.17
60. $ java -jar /tool/groovy-1.8.0/target/
install/embeddable/groovy-all-1.8.0.jar
fib.groovy
time=2542
time=34
Slide # 49 JGGUG G*Workshop 17th / 2011.6.17
Editor's Notes \n \n \n \n \n \n \n \n &#x6B63;&#x76F4;&#x3001;Groovy&#x304B;&#x3089;&#x66F8;&#x304D;&#x306A;&#x304A;&#x3059;&#x306E;&#x306F;&#x3001;&#x305F;&#x3044;&#x3078;&#x3093;&#x3067;&#x3057;&#x305F;&#x3002;\n \n &#x300C;&#x4F55;&#x3092;&#x51E6;&#x7406;&#x3059;&#x308B;&#x304B;&#x300D;&#x3068;&#x3044;&#x3046;&#x6700;&#x4F4E;&#x9650;&#x306E;&#x6307;&#x793A;&#x306F;&#x4E0A;&#x3067;&#x5341;&#x5206;&#x3001;\n&#x4ED6;&#x306F;&#x30E1;&#x30BF;&#x60C5;&#x5831;&#x3002;\n &#x5404;&#x884C;&#x3092;&#x89E3;&#x8AAC;&#x3057;&#x3066;&#x3044;&#x304D;&#x307E;&#x3059;&#x3002;&#x3059;&#x3093;&#x306A;&#x308A;&#x3068;&#x8AAD;&#x3081;&#x3070;&#x30B3;&#x30FC;&#x30C9;&#x306E;&#x610F;&#x5473;&#x306B;&#x5BFE;&#x5FDC;&#x3059;&#x308B;&#x3002;\n &#x5404;&#x884C;&#x3092;&#x89E3;&#x8AAC;&#x3057;&#x3066;&#x3044;&#x304D;&#x307E;&#x3059;&#x3002;&#x3059;&#x3093;&#x306A;&#x308A;&#x3068;&#x8AAD;&#x3081;&#x3070;&#x30B3;&#x30FC;&#x30C9;&#x306E;&#x610F;&#x5473;&#x306B;&#x5BFE;&#x5FDC;&#x3059;&#x308B;&#x3002;\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&#x306E;&#x3088;&#x3046;&#x306B;DOM&#x3068;&#x304B;&#x51FA;&#x3066;&#x3053;&#x306A;&#x3044;&#x3002;\n \n \n \n \n \n \n \n \n \n \n \n