Upgrade to Pro — share decks privately, control downloads, hide ads and more …

JavaSE7 Launch Event: Java7xGroovy

JavaSE7 Launch Event: Java7xGroovy

Avatar for Yasuharu Nakano

Yasuharu Nakano

March 25, 2023
Tweet

More Decks by Yasuharu Nakano

Other Decks in Programming

Transcript

  1. $ time groovy -e 'println "Hello, world!";' Hello, world! real

    0m1.177s user 0m1.321s sys 0m0.171s $ time groovyclient -e 'println "Hello, world!";' Hello, world! real 0m0.029s user 0m0.001s sys 0m0.002s
  2. +BWB4USJOHTJO4XJUDI String 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; } 㾎4USJOH͕৚݅෼ذʹ࢖͑Δ Α͏ʹͳΓ·ͨ͠ʁ
  3. (SPPWZ"OZUIJOHJO4XJUDI  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 // .... }
  4. (SPPWZ"OZUIJOHJO4XJUDI  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 }
  5. (SPPWZ$MPTVSF 㾎ຊ෺ͷΫϩʔδϟ͕طʹ͋Γ·͢ def highestScore = students. findAll({ s -> s.gradYear

    == 2010 }). collect({ s -> s.score }). max() ֤छ಺෦ΠςϨʔγϣϯදه΋ؚΊͯɺศ རͳίϨΫγϣϯૢ࡞΋΋ͪΖΜ׬උ
  6. +BWBҎલVHMZGJOBMMZDMPTJOH 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) { } } }
  7. +BWBUSZXJUISFTPVSDFT 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); } }
  8. (SPPWZ-PBO1BUUFSO new 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 㾎όοϑΝϦϯά͕ෆཁͳΒʢࢀߟʣ
  9. +BWB'PSL+PJO'SBNFXPSL import 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(); } }
  10. (SPPWZ(1BST import 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) == 55 assert fibo(20) == 6765 㾎 ଞʹ΋ˣʹରԠ 㾎 1BSBMMFM"SSBZ 㾎 .BQ3FEVDF 㾎 4DBMBͷ"DUPS 㾎 $MPKVSFͷ"HFOU