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

わかる!Java to Clojure

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.

わかる!Java to Clojure

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

Avatar for Shunsuke Tadokoro

Shunsuke Tadokoro

October 05, 2017
Tweet

More Decks by Shunsuke Tadokoro

Other Decks in Technology

Transcript

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

    +BWB4DSJQU͸$ͷൽΛ͔Ϳͬͨ-JTQͩͱฉ͍ͯ w ϧϯό͕-JTQͰಈ͍͍ͯΔͱฉ͍ͯ w FUD
  2. ಡΈ΍͢͞ ͨͱ͑͹ؔ਺ͷҾ਺ $PNNPO-JTQ 4DIFNF $MPKVSF (defun square (x) (* x

    x)) (define (square x) (* x x)) (defn square [x] (* x x))
  3. ಡΈ΍͢͞ ͨͱ͑͹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
  4. #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ͰͰ͖Δ͜ͱ͸Ͱ͖Δʂͱ͍͏҆৺ײ
  5. ࢛ଇԋࢉ 12 + 40 10 - 1 2 * 3

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

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

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

    x 2)) #(* % 2) ; ↑ͷ؆ܿͳه๏ $MPKVSF
  10. ແ໊ؔ਺Λ࢖ͬͯΈΔ 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
  11. γʔέϯε಺ͷจࣈྻ݁߹ 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ͷ৔߹
  12. w Մม௕Ҿ਺Λड͚औΔؔ਺ʹγʔέϯεΛ౉͍ͨ͠ͱ͖ʹ࢖͏ w +BWB4DSJQUͷBQQMZͱҰॹ BQQMZ (str ["a" "b" "c"]) ;

    -> ["a" "b" "c"] (apply str ["a" "b" "c"]) ; -> abc ; (str "a" "b" "c") ͱಉ͡
  13. จࣈྻ͕ۭ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ͷ৔߹
  14. จࣈྻ͕ۭPSۭനจࣈ͔Ͳ͏͔ͷ൑ఆ (defn blank? [s] (every? #(Character/isWhitespace %) s)) (blank? "helllo!")

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

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

    ; false (blank? nil) ; true (blank? "") ; true (blank? "\n\r\t") ; true DMPKVSFͷ৔߹
  17. *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ͷ৔߹
  18. 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")
  19. 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!
  20. *OQVU4USFBN͔ΒจࣈྻΛ͢΂ͯಡΉ (require [clojure.java.io :as io]) (with-open [bis (io/input-stream "./test.txt")] (println

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

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