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

わかる!Java to Clojure

わかる!Java to Clojure

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

9aba2147bb6e43333fcc42e2afc570f2?s=128

Shunsuke Tadokoro

October 05, 2017
Tweet

Transcript

  1. Θ͔Δʂ+BWBUP$MPKVSF  ڧཁڭཆͱͯ͠ͷ$MPKVSF EDMK !UPEPLS ָ͘͠

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

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

  5. 8IZ-JTQ w େֶͰ৮͍ͬͯͨͷͰ w ࢓ࣄͷͨΊ w ʮܭࢉػϓϩάϥϜͷߏ଄ͱղऍʯΛಡΉͨΊ w &NBDTϢʔβʔͩͬͨͷͰ w

    +BWB4DSJQU͸$ͷൽΛ͔Ϳͬͨ-JTQͩͱฉ͍ͯ w ϧϯό͕-JTQͰಈ͍͍ͯΔͱฉ͍ͯ w FUD
  6. None
  7. ϙʔϧɾάϨΞϜ 7JBXFC૑ۀऀ :$PNCJOBUPS૑ઃऀ

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

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

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

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

    :BIPPʹച٫
  12. ͳͥ$PNNPO-JTQ΍4DIFNFͰ͸ͳ͘
 $MPKVSFʁ

  13. 8IZ$MPKVSF w ಡΈ΍͢͞ w ׅހͷ਺͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w

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

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

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

    x)) (define (square x) (* x x)) (defn square [x] (* x x))
  17. ಡΈ΍͢͞ ͨͱ͑͹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
  18. 8IZ$MPKVSF w ಡΈ΍͢͞ w ׅހͷ਺͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w

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

    ϦονɾώοΩʔ
  20. #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ͰͰ͖Δ͜ͱ͸Ͱ͖Δʂͱ͍͏҆৺ײ
  21. 8IZ$MPKVSF w ಡΈ΍͢͞ w ׅހͷ਺͕গͳ͍ w ׅހͷछྨ͕ଟ͍ w #FUUFS+BWBͰ͋Δ͜ͱ w

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

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

  24. IUUQCPYPGQBQFSTIBUFOBCMPHDPNFOUSZTJNQMF@NBEF@FBTZ

  25. ͭ·Γ$MPKVSF͸
 ͱ͖ͬͭ΍͍͢ʂ
 ֶͼָ͕͍͋ͬͯ͠ʂ

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

    ແ໊ؔ਺
  27. खݩͰࢼ͢ͳΒ5SZ$MPKVSF͕͓͢͢Ί w 5SZ$MPKVSF IUUQXXXUSZDMKDPN

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

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

  30. ΋͏׳Ε·ͨ͠Ͷ

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

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

  33. ࢛ଇԋࢉ 12 + 40 10 - 1 2 * 3

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

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

    5 / 2 +BWB (+ 12 40) (- 10 1) (* 2 3) (/ 5 2) ; -> 5/2 ෼਺Λѻ͏Ratioܕ $MPKVSF
  36. ˞จࣈྻͷ݁߹͸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
  37. ม਺એݴ int x = 10; +BWB $MPKVSF

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

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

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

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

    } +BWB (defn f [x] (+ x 1)) $MPKVSF
  42. ແ໊ؔ਺ x -> x * 2 +BWB $MPKVSF

  43. ແ໊ؔ਺ x -> x * 2 +BWB (fn [x] (*

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

    x 2)) #(* % 2) ; ↑ͷ؆ܿͳه๏ $MPKVSF
  45. ແ໊ؔ਺Λ࢖ͬͯΈΔ Arrays.asList(1, 2, 3).stream() .map(x -> x * 2) .collect(Collectors.toList());

    // List(2, 4, 6) +BWB $MPKVSF
  46. ແ໊ؔ਺Λ࢖ͬͯΈΔ 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
  47. ແ໊ؔ਺Λ࢖ͬͯΈΔ 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
  48. ΋ͬͱ+BWBUP$MPKVSFͯ͠ΈΑ͏ w γʔέϯε಺ͷจࣈྻ݁߹ w จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ w *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ

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

  50. γʔέϯε಺ͷจࣈྻ݁߹ 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ͷ৔߹
  51. γʔέϯε಺ͷจࣈྻ݁߹ (def words '("a" "b" "c")) (clojure.string/join "" words) ;

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

    ΋͘͠͸ (apply str words) DMPKVSFͷ৔߹
  53. w Մม௕Ҿ਺Λड͚औΔؔ਺ʹγʔέϯεΛ౉͍ͨ͠ͱ͖ʹ࢖͏ w +BWB4DSJQUͷBQQMZͱҰॹ BQQMZ (str ["a" "b" "c"]) ;

    -> ["a" "b" "c"] (apply str ["a" "b" "c"]) ; -> abc ; (str "a" "b" "c") ͱಉ͡
  54. γʔέϯε಺ͷจࣈྻ݁߹ (def words '("a" "b" "c")) (clojure.string/join "" words) ;

    ΋͘͠͸ (apply str words) DMPKVSFͷ৔߹
  55. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ

  56. จࣈྻ͕ۭ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ͷ৔߹
  57. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ (defn blank? [s] (every? #(Character/isWhitespace %) s)) (blank? "helllo!")

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

    ; false (blank? nil) ; true (blank? "") ; true (blank? "\n\r\t") ; true DMPKVSFͷ৔߹
  59. FWFSZ w ίϨΫγϣϯͷશͯͷཁૉʹରͯ͠
 ड़ޠؔ਺ʢCPPMFBOΛฦؔ͢਺ʣΛద༻ w શͯUSVFͳΒUSVF (every? even? '(2 4

    6)) ; true (every? even? '(1 2 3)) ; false
  60. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ (defn blank? [s] (every? #(Character/isWhitespace %) s)) (blank? "helllo!")

    ; false (blank? nil) ; true (blank? "") ; true (blank? "\n\r\t") ; true DMPKVSFͷ৔߹
  61. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ

  62. *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ͷ৔߹
  63. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println

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

    (slurp bis))) $MPKVSFͷ৔߹
  65. 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")
  66. XJUIPQFO w +BWBͰ͍͏USZXJUISFTPVSDFߏจɺ
 4DBMBͰ͍͏-PBO1BUUFSOΛ࣮ݱ͢Δ΋ͷ w XJUIPQFOͰPQFOͨ͠Ϧιʔε͸উखʹDMPTF͞ΕΔ (with-open [in (io/input-stream "./test.txt")]

    (do-something in)) ;; already closed!
  67. 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!
  68. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println

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

    (slurp bis))) $MPKVSFͷ৔߹ ;; όοϑΝϦϯάͤͣʹϑΝΠϧ͔ΒಡΉ͚ͩͳΒˣͰOK (println (slurp "./test.txt"))
  70. ࠓ೔+BWBUP$MPKVSFͨ͠΋ͷ w )FMMPXPSME w ࢛ଇԋࢉ w ม਺એݴ w ؔ਺એݴ w

    ແ໊ؔ਺ w γʔέϯε಺ͷจࣈྻ݁߹ w จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ w *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ
  71. None