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

わかる!Java to Clojure

わかる!Java to Clojure

2017.10.4「教養としてのClojure」の発表資料です

Shunsuke Tadokoro

October 05, 2017
Tweet

More Decks by Shunsuke Tadokoro

Other Decks in Technology

Transcript

  1. Θ͔Δʂ+BWBUP$MPKVSF

    ڧཁڭཆͱͯ͠ͷ$MPKVSF
    EDMK
    !UPEPLS
    ָ͘͠

    View full-size slide

  2. ୭ͩ
    ాॴॣ༎!UPEPLS
    ීஈ͸4DBMBΤϯδχΞ
    ϓϥΠϕʔτͰ$MPKVSF$MPKVSF4DSJQUΛͪΐͪ͜ΐ͜৮Δॳ৺ऀ

    View full-size slide

  3. ͓࿩͢͠Δ͜ͱ
    w 8IZ-JTQ 8IZ$MPKVSF
    w جຊతͳจ๏ʹ͍ͭͯ
    w +BWBͷΞϨΛ$MPKVSFͰॻ͍ͯΈΑ͏

    View full-size slide

  4. 8IZ-JTQ
    w େֶͰ৮͍ͬͯͨͷͰ
    w ࢓ࣄͷͨΊ
    w ʮܭࢉػϓϩάϥϜͷߏ଄ͱղऍʯΛಡΉͨΊ
    w &NBDTϢʔβʔͩͬͨͷͰ
    w +BWB4DSJQU͸$ͷൽΛ͔Ϳͬͨ-JTQͩͱฉ͍ͯ
    w ϧϯό͕-JTQͰಈ͍͍ͯΔͱฉ͍ͯ
    w FUD

    View full-size slide

  5. ϙʔϧɾάϨΞϜ
    7JBXFC૑ۀऀ
    :$PNCJOBUPS૑ઃऀ

    View full-size slide

  6. -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ࢖͏΂͖ͳΜͩɻ
    ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷ໾ʹཱͭ
    ϙʔϧɾάϨΞϜ
    7JBXFC૑ۀऀ
    :$PNCJOBUPS૑ઃऀ

    View full-size slide

  7. ී௨ͷౕΒͷ্Λߦ͚ɻ
    -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ࢖͏΂͖ͳΜͩɻ
    ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷ໾ʹཱͭ
    ϙʔϧɾάϨΞϜ
    7JBXFC૑ۀऀ
    :$PNCJOBUPS૑ઃऀ

    View full-size slide

  8. ී௨ͷౕΒͷ্Λߦ͚ɻ
    -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ࢖͏΂͖ͳΜͩɻ
    ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷ໾ʹཱͭ
    ϙʔϧɾάϨΞϜ
    7JBXFC૑ۀऀ
    :$PNCJOBUPS૑ઃऀ
    :BIPPʹച٫

    View full-size slide

  9. ී௨ͷౕΒͷ্Λߦ͚ɻ
    -JTQͰྑ͍ϓϩάϥϜ͕ॻ͚ΔͳΒɺ࢖͏΂͖ͳΜͩɻ
    ͦ͏Ͱͳ͍ͳΒ͍͍ͬͨԿͷ໾ʹཱͭ
    ϙʔϧɾάϨΞϜ
    7JBXFC૑ۀऀ
    :$PNCJOBUPS૑ઃऀ
    Θͨ͠
    ී௨ͷϓϩάϥϚ
    -JTQͰ΢ΣϒαʔϏεΛͭ͘Δͱ
    :BIPPʹԯԁͰങͬͯ΋Β͑Δʂʂʂ
    :BIPPʹച٫

    View full-size slide

  10. ͳͥ$PNNPO-JTQ΍4DIFNFͰ͸ͳ͘

    $MPKVSFʁ

    View full-size slide

  11. 8IZ$MPKVSF
    w ಡΈ΍͢͞
    w ׅހͷ਺͕গͳ͍
    w ׅހͷछྨ͕ଟ͍
    w #FUUFS+BWBͰ͋Δ͜ͱ
    w ϦονɾώοΩʔ

    View full-size slide

  12. 8IZ$MPKVSF
    w ಡΈ΍͢͞
    w ׅހͷ਺͕গͳ͍
    w ׅހͷछྨ͕ଟ͍
    w #FUUFS+BWBͰ͋Δ͜ͱ
    w ϦονɾώοΩʔ

    View full-size slide

  13. ಡΈ΍͢͞
    $MPKVSF͸-JTQͷ఻౷ΛࣺͯɺಡΈ΍͢͞ΛͱͬͨʢΒ͍͠ʣ
    ͦͷͨΊɺ$PNNPO-JTQ΍4DIFNFʹൺ΂ͯ
    ɾׅހ͕গͳ͍
    ɾׅހͷछྨ͕ଟ͍

    View full-size slide

  14. ಡΈ΍͢͞
    ͨͱ͑͹ؔ਺ͷҾ਺
    $PNNPO-JTQ
    4DIFNF
    $MPKVSF
    (defun square (x)
    (* x x))
    (define (square x)
    (* x x))
    (defn square [x]
    (* x x))

    View full-size slide

  15. ಡΈ΍͢͞
    ͨͱ͑͹BTTPDJBUJWFͳσʔλߏ଄ʢ+BWBͰ͍͏.BQʣ
    (defparameter greet '(("ja" . "͜Μʹͪ͸")
    ("en" . "hello")
    ("fr" . "bonjour"))
    (define greet '(("ja" "͜Μʹͪ͸")
    ("en" "hello")
    ("fr" "bonjour"))
    $PNNPO-JTQ
    4DIFNF
    (def greet {"ja" "͜Μʹͪ͸"
    "en" "hello"
    "fr" "bonjour"})
    $MPKVSF

    View full-size slide

  16. 8IZ$MPKVSF
    w ಡΈ΍͢͞
    w ׅހͷ਺͕গͳ͍
    w ׅހͷछྨ͕ଟ͍
    w #FUUFS+BWBͰ͋Δ͜ͱ
    w ϦονɾώοΩʔ

    View full-size slide

  17. 8IZ$MPKVSF
    w ಡΈ΍͢͞
    w ׅހͷ਺͕গͳ͍
    w ׅހͷछྨ͕ଟ͍
    w #FUUFS+BWBͰ͋Δ͜ͱ
    w ϦονɾώοΩʔ

    View full-size slide

  18. #FUUFS+BWBͰ͋Δ͜ͱ
    (.concat "hello" " world") ; -> "hello world"
    ;; "hello".concat(" world");
    (.getName String) ; -> "java.lang.String"
    ; String.getName
    (.. user getAddress getCity) ; -> "Shibuya"
    ; user.getAddress().getCity();
    (.-x (new java.awt.Point 2 3)) ;; 2
    ;; -> new java.awt.Point(2, 3).x
    Math/PI ;; 3.141592653589793
    ;; Math.PI
    +BWBͷ"1*͕ͦͷ··ݺͼग़ͤΔ☕️
    ˠ+BWBͰͰ͖Δ͜ͱ͸Ͱ͖Δʂͱ͍͏҆৺ײ

    View full-size slide

  19. 8IZ$MPKVSF
    w ಡΈ΍͢͞
    w ׅހͷ਺͕গͳ͍
    w ׅހͷछྨ͕ଟ͍
    w #FUUFS+BWBͰ͋Δ͜ͱ
    w ϦονɾώοΩʔ

    View full-size slide

  20. 8IZ$MPKVSF
    w ಡΈ΍͢͞
    w ׅހͷ਺͕গͳ͍
    w ׅހͷछྨ͕ଟ͍
    w #FUUFS+BWBͰ͋Δ͜ͱ
    w ͳʹΑΓָ͍͠ʂ

    View full-size slide

  21. ͳʹΑΓָ͍͠ʂ
    w ߴ֊ؔ਺΍஗ԆධՁͳͲͷؔ਺ܕϓϩάϥϛϯάελΠϧ
    w ঢ়ଶʹ͍ͭͯͷߟ͑ํɺ*EFOUJUZ 4UBUF 7BMVFͷ෼཭
    w γϯϓϧ͞ʹ͍ͭͯ
    ͍Ζ͍Ζܹࢗత

    View full-size slide

  22. IUUQCPYPGQBQFSTIBUFOBCMPHDPNFOUSZTJNQMF@NBEF@FBTZ

    View full-size slide

  23. ͭ·Γ$MPKVSF͸

    ͱ͖ͬͭ΍͍͢ʂ

    ֶͼָ͕͍͋ͬͯ͠ʂ

    View full-size slide

  24. +BWBUP$MPKVSFͯ͠ΈΑ͏
    w )FMMPXPSME
    w ࢛ଇԋࢉ
    w ม਺એݴ
    w ؔ਺એݴ
    w ແ໊ؔ਺

    View full-size slide

  25. खݩͰࢼ͢ͳΒ5SZ$MPKVSF͕͓͢͢Ί
    w 5SZ$MPKVSF
    IUUQXXXUSZDMKDPN

    View full-size slide

  26. ·ͣ͸γϯλοΫεʹ׳ΕΑ͏ʂ

    View full-size slide

  27. -JTQͷγϯλοΫε
    ׅހͷ࢝·Γ͔ΒऴΘΓ·Ͱ͕୯Ґ
    (ԿΒ͔ͷॲཧ ॲཧͷର৅ͳͲ…)

    View full-size slide

  28. ΋͏׳Ε·ͨ͠Ͷ

    View full-size slide

  29. )FMMPXPSME
    System.out.println("Hello world");
    +BWB
    $MPKVSF

    View full-size slide

  30. )FMMPXPSME
    System.out.println("Hello world");
    +BWB
    (println "Hello world")
    $MPKVSF

    View full-size slide

  31. ࢛ଇԋࢉ
    12 + 40
    10 - 1
    2 * 3
    5 / 2
    +BWB
    $MPKVSF

    View full-size slide

  32. ࢛ଇԋࢉ
    12 + 40
    10 - 1
    2 * 3
    5 / 2
    +BWB
    (+ 12 40)
    (- 10 1)
    (* 2 3)
    (/ 5 2)
    $MPKVSF

    View full-size slide

  33. ࢛ଇԋࢉ
    12 + 40
    10 - 1
    2 * 3
    5 / 2
    +BWB
    (+ 12 40)
    (- 10 1)
    (* 2 3)
    (/ 5 2) ; -> 5/2 ෼਺Λѻ͏Ratioܕ
    $MPKVSF

    View full-size slide

  34. ˞จࣈྻͷ݁߹͸TUS
    จࣈྻͷ݁߹͸Ͱ͸ͳ͘TUSΛ࢖͏
    (+ "hoge" "fuga")
    ClassCastException java.lang.String cannot
    be cast to java.lang.Number
    clojure.lang.Numbers.add (Numbers.java:128)
    (str "hoge" "fuga")
    hogefuga

    View full-size slide

  35. ม਺એݴ
    int x = 10;
    +BWB
    $MPKVSF

    View full-size slide

  36. ม਺એݴ
    int x = 10;
    +BWB
    (def x 10)
    $MPKVSF

    View full-size slide

  37. ม਺એݴ
    MFUʹΑΔϩʔΧϧม਺ఆٛʢϩʔΧϧଋറʣΛݟ͔͚Δ΄͏͕ଟ͍
    (let [x 10]
    (+ x 3))
    ; x͸letͷׅހ಺͚ͩͰࢀরͰ͖Δ
    $MPKVSF

    View full-size slide

  38. ؔ਺એݴ
    public int f(int x) {
    return x + 1;
    }
    +BWB
    $MPKVSF

    View full-size slide

  39. ؔ਺એݴ
    public int f(int x) {
    return x + 1;
    }
    +BWB
    (defn f [x]
    (+ x 1))
    $MPKVSF

    View full-size slide

  40. ແ໊ؔ਺
    x -> x * 2
    +BWB
    $MPKVSF

    View full-size slide

  41. ແ໊ؔ਺
    x -> x * 2
    +BWB
    (fn [x] (* x 2))
    $MPKVSF

    View full-size slide

  42. ແ໊ؔ਺
    x -> x * 2
    +BWB
    (fn [x] (* x 2))
    #(* % 2) ; ↑ͷ؆ܿͳه๏
    $MPKVSF

    View full-size slide

  43. ແ໊ؔ਺Λ࢖ͬͯΈΔ
    Arrays.asList(1, 2, 3).stream()
    .map(x -> x * 2)
    .collect(Collectors.toList());
    // List(2, 4, 6)
    +BWB
    $MPKVSF

    View full-size slide

  44. ແ໊ؔ਺Λ࢖ͬͯΈΔ
    Arrays.asList(1, 2, 3).stream()
    .map(x -> x * 2)
    .collect(Collectors.toList());
    // List(2, 4, 6)
    +BWB
    (map (fn [x] (* x 2)) '(1 2 3))
    $MPKVSF

    View full-size slide

  45. ແ໊ؔ਺Λ࢖ͬͯΈΔ
    Arrays.asList(1, 2, 3).stream()
    .map(x -> x * 2)
    .collect(Collectors.toList());
    // List(2, 4, 6)
    +BWB
    (map (fn [x] (* x 2)) '(1 2 3))
    (map #(* % 2) '(1 2 3))
    $MPKVSF

    View full-size slide

  46. ΋ͬͱ+BWBUP$MPKVSFͯ͠ΈΑ͏
    w γʔέϯε಺ͷจࣈྻ݁߹
    w จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ
    w *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ

    View full-size slide

  47. γʔέϯε಺ͷจࣈྻ݁߹

    View full-size slide

  48. γʔέϯε಺ͷจࣈྻ݁߹
    String[] words = { "a", "b", "c" };
    StringBuilder sb = new StringBuilder();
    for(String word : words) {
    sb.append(word);
    }
    System.out.println(sb.toString()); // "abc"
    // Java8
    System.out.println(String.join("", words));
    +BWBͷ৔߹

    View full-size slide

  49. γʔέϯε಺ͷจࣈྻ݁߹
    (def words '("a" "b" "c"))
    (clojure.string/join "" words)
    ; ΋͘͠͸
    (apply str words)
    DMPKVSFͷ৔߹

    View full-size slide

  50. γʔέϯε಺ͷจࣈྻ݁߹
    (def words '("a" "b" "c"))
    (clojure.string/join "" words)
    ; ΋͘͠͸
    (apply str words)
    DMPKVSFͷ৔߹

    View full-size slide

  51. w Մม௕Ҿ਺Λड͚औΔؔ਺ʹγʔέϯεΛ౉͍ͨ͠ͱ͖ʹ࢖͏
    w +BWB4DSJQUͷBQQMZͱҰॹ
    BQQMZ
    (str ["a" "b" "c"]) ; -> ["a" "b" "c"]
    (apply str ["a" "b" "c"]) ; -> abc
    ; (str "a" "b" "c") ͱಉ͡

    View full-size slide

  52. γʔέϯε಺ͷจࣈྻ݁߹
    (def words '("a" "b" "c"))
    (clojure.string/join "" words)
    ; ΋͘͠͸
    (apply str words)
    DMPKVSFͷ৔߹

    View full-size slide

  53. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ

    View full-size slide

  54. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ
    public static boolean isBlank(final CharSequence cs) {
    int strLen;
    if (cs == null || (strLen = cs.length()) == 0) {
    return true;
    }
    for (int i = 0; i < strLen; i++) {
    if (!Character.isWhitespace(cs.charAt(i))) {
    return false;
    }
    }
    return true;
    }
    isBlank("hello!"); // false
    isBlank(null); // true
    isBlank(""); // true
    isBlank("\r\n\t") // true
    +BWBͷ৔߹

    View full-size slide

  55. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ
    (defn blank? [s]
    (every? #(Character/isWhitespace %) s))
    (blank? "helllo!") ; false
    (blank? nil) ; true
    (blank? "") ; true
    (blank? "\n\r\t") ; true
    DMPKVSFͷ৔߹

    View full-size slide

  56. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ
    (defn blank? [s]
    (every? #(Character/isWhitespace %) s))
    (blank? "helllo!") ; false
    (blank? nil) ; true
    (blank? "") ; true
    (blank? "\n\r\t") ; true
    DMPKVSFͷ৔߹

    View full-size slide

  57. FWFSZ
    w ίϨΫγϣϯͷશͯͷཁૉʹରͯ͠

    ड़ޠؔ਺ʢCPPMFBOΛฦؔ͢਺ʣΛద༻
    w શͯUSVFͳΒUSVF
    (every? even? '(2 4 6)) ; true
    (every? even? '(1 2 3)) ; false

    View full-size slide

  58. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ
    (defn blank? [s]
    (every? #(Character/isWhitespace %) s))
    (blank? "helllo!") ; false
    (blank? nil) ; true
    (blank? "") ; true
    (blank? "\n\r\t") ; true
    DMPKVSFͷ৔߹

    View full-size slide

  59. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ

    View full-size slide

  60. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ
    try(BufferedReader reader =
    new BufferedReader(
    new InputStreamReader(
    new FileInputStream("./test.txt")))) {
    StringBuilder sb = new StringBuilder();
    String line;
    while((line = reader.readLine()) != null) {
    sb.append(line);
    }
    System.out.println(sb.toString());
    }
    +BWBͷ৔߹

    View full-size slide

  61. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ
    (require [clojure.java.io :as io])
    (with-open [bis (io/input-stream "./test.txt")]
    (println (slurp bis)))
    $MPKVSFͷ৔߹

    View full-size slide

  62. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ
    (require [clojure.java.io :as io])
    (with-open [bis (io/input-stream "./test.txt")]
    (println (slurp bis)))
    $MPKVSFͷ৔߹

    View full-size slide

  63. DMPKVSFKBWBJPJOQVUTUSFBN
    w Ҿ਺Ͱड͚औͬͨ'JMF 63* 63- 4PDLFU CZUF഑ྻ΍

    4USJOHʢͰදݱ͞ΕͨύεʣΛ*OQVU4USFBNʹม׵
    w ม׵ޙͷ*OQVU4USFBNͷσϑΥϧτ࣮૷͸

    KBWBJP#VGGFSFE*OQVU4USFBN
    (require '[clojure.java.io :as io])
    (io/input-stream "http://example.com")

    View full-size slide

  64. XJUIPQFO
    w +BWBͰ͍͏USZXJUISFTPVSDFߏจɺ

    4DBMBͰ͍͏-PBO1BUUFSOΛ࣮ݱ͢Δ΋ͷ
    w XJUIPQFOͰPQFOͨ͠Ϧιʔε͸উखʹDMPTF͞ΕΔ
    (with-open [in (io/input-stream "./test.txt")]
    (do-something in))
    ;; already closed!

    View full-size slide

  65. TMVSQ
    w 3FBEFS #VGGFSFE3FBEFS *OQVU4USFBN 'JMF 63*
    63- 4PDLFU CZUF഑ྻ DIBS഑ྻΛड͚औΓɺ

    จࣈྻʹͯ͠ฦ͢
    w ରʹͳΔॻ͖ग़͠ͷͨΊͷؔ਺͸TQJU
    (spit "./test.txt" "hello I/O!")
    (println (slurp "./test.txt"))
    ;; -> hello I/O!

    View full-size slide

  66. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ
    (require [clojure.java.io :as io])
    (with-open [bis (io/input-stream "./test.txt")]
    (println (slurp bis)))
    $MPKVSFͷ৔߹

    View full-size slide

  67. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ
    (require [clojure.java.io :as io])
    (with-open [bis (io/input-stream "./test.txt")]
    (println (slurp bis)))
    $MPKVSFͷ৔߹
    ;; όοϑΝϦϯάͤͣʹϑΝΠϧ͔ΒಡΉ͚ͩͳΒˣͰOK
    (println (slurp "./test.txt"))

    View full-size slide

  68. ࠓ೔+BWBUP$MPKVSFͨ͠΋ͷ
    w )FMMPXPSME
    w ࢛ଇԋࢉ
    w ม਺એݴ
    w ؔ਺એݴ
    w ແ໊ؔ਺
    w γʔέϯε಺ͷจࣈྻ݁߹
    w จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ
    w *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ

    View full-size slide