javase7 launch event: java7xgroovy
TRANSCRIPT
Java7×Groovy
Who am I?✓氏名:中野 靖治(a.k.a. nobeans)✓所属:NTTソフトウェア株式会社✓ブログ:豆無日記 http://d.hatena.ne.jp/nobeans/
✓プロダクト: https://github.com/nobeans/
$ time groovy -e 'println "Hello, world!";'
Hello, world!
real 0m1.177suser 0m1.321ssys 0m0.171s
$ time groovyclient -e 'println "Hello,
world!";'Hello, world!
real 0m0.029suser 0m0.001ssys 0m0.002s
Java7のその機能Groovyには既にありますけど何か?
Strings in Switch
Java7 Strings in SwitchString s = ...switch (s) { case "quux": processQuux(s); // fall-through
case "foo": case "bar": processFooOrBar(s); break;
case "baz": processBaz(s); // fall-through
default: processDefault(s); break;}
✓Stringが条件分岐に使えるようになりました?
Groovy Anything in Switch (1)import java.util.regex.Matcher
Object x = ...switch (x) { case "abc": // xの内容が"abc"のとき break
case ~/a(.*)c/: // xが正規表現パターンにマッチするとき assert Matcher.lastMatcher[0][0] == "abbc" assert Matcher.lastMatcher[0][1] == "bb" break
case 1..3: // xが1~3の範囲に含まれるとき break
case [1,3,5]: // xが1,3,5のどれかであるとき break
// ....}
Groovy Anything in Switch (2)switch (x) {
// ....
case String: // xがStringのインスタンスであるとき break
case {it % 2 == 0}: // xが偶数のとき break
case null: // xがnullのとき!!! break
case a: // aがユーザ定義型のインスタンスで
// a.isCase(x)がtrueのとき break}
Lambda
Java8 Lambda? Closure?
double highestScore = students .filter(#{ s -> s.gradYear == 2010 }) .map( #{ s -> s.score }) .max();
✓ついにJavaにもクロージャが...?
Groovy Closure !
✓本物のクロージャが既にあります
def highestScore = students. findAll({ s -> s.gradYear == 2010 }). collect({ s -> s.score }). max()
各種内部イテレーション表記も含めて、便利なコレクション操作ももちろん完備
try-with-resources
Java6以前 ugly-finally-closing
InputStream in = null;OutputStream out = null;try { in = new FileInputStream(“src”); out = new FileOutputStream(“dst”); byte[] buf = new byte[8192]; int n = 0; while((n = in.read(buf)) >= 0) { out.write(buf, 0, n); }} catch (IOException e) { // 例外処理} finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } }}
Java7 try-with-resources
try (InputStream in = new FileInputStream(“src”); OutputStream out = new FileOutputStream(“dst”)) { byte[] buf = new byte[8192]; int n = 0; while((n = in.read(buf)) >= 0) { out.write(buf, 0, n); }}
Groovy Loan Patternnew File(“dest”).withOutputStream { out -> new File(“src”).withInputStream { ins -> byte[] buf = new byte[8192] int n = 0 while((n = ins.read(buf)) >= 0) { out.write(buf, 0, n); } }}
new File("dest") << new File("src").bytes
✓バッファリングが不要なら(参考)
Diamond
Java Diamond~Java6: Map<String, Map<String, List<Employee>>> map = new HashMap<String, Map<String, List<Employee>>>();
Java7: Map<String, Map<String, List<Employee>>> map = new HashMap<>();
Groovy ジェネリクスの型指定なんてただの飾りです(キリッ
Map<String, Map<String, List<Employee>>> map = [:]
map.put(1, “A”) // 「えっ?」
map.put(“HOGE”, new Date()) // 「えっ?」
Fork/JoinFramwork
Java7 Fork/Join Frameworkimport java.util.concurrent.*;
public class Fibonacci extends RecursiveTask<Integer> { final int n; public Fibonacci(int n) { this.n = n; } public Integer compute() { if (n <= 1) return n; Fibonacci f1 = new Fibonacci(n - 1); f1.fork(); Fibonacci f2 = new Fibonacci(n - 2); return f2.compute() + f1.join(); }}
Groovy GParsimport static groovyx.gpars.GParsPool.*
def fibo(num) { withPool { runForkJoin(num) { n -> switch (n) { case 0..1: return n default: forkOffChild(n - 1) forkOffChild(n - 2) childrenResults.sum() } } }}assert fibo(10) == 55assert fibo(20) == 6765
✓他にも↓に対応✓ ParallelArray✓ Map/Reduce✓ ScalaのActor✓ ClojureのAgent
Java7の居る場所は、既に̶̶̶
Groovyが ○年前に通過した場所だッ
と、煽って書いてみましたが、JavaがあってこそのGroovy なので、今後とも何卒よろしくお願いいたします。いやマジで。