Clojureの基礎 〜マクロ編〜

Clojureの基礎 〜マクロ編〜

2016/6/11 プログラミング言語基礎勉強会のLT発表資料です。

E170ae2eb0adcf3d136ab0990f0ea671?s=128

OHTA Shogo

June 11, 2016
Tweet

Transcript

  1. $MPKVSFͷجૅ ϓϩάϥϛϯάݴޠجૅษڧձ 2016/06/11 @athos0220

  2. ࣗݾ঺հ ‣ 5XJUUFS!BUIPT ‣ χϟϯύε ג ॴଐ ‣ $MPKVSFίϯτϦϏϡʔλ

  3. $MPKVSFͷجૅΛ࡞Δ΋ͷ ࠓճ͸-JTQͱͯ͠ͷಛ௃ʹϑΥʔΧε -JTQ ؔ਺ ϓϩάϥϛϯά ର࿩త ϓϩάϥϛϯά +7.

  4. -JTQ

  5. -JTQ ‣ લஔه๏͕ಛ௃ ‣ ࠷ॳͷཁૉ͕ΦϖϨʔλɺͦΕҎ͕߱Ҿ਺ ‣ ݴޠͷίΞ͕ඇৗʹγϯϓϧ (defn distance [x

    y] (sqrt (+ (* x x) (* y y))))
  6. -JTQ ‣ લஔه๏͕ಛ௃ ‣ ࠷ॳͷཁૉ͕ΦϖϨʔλɺͦΕҎ͕߱Ҿ਺ ‣ ݴޠͷίΞ͕ඇৗʹγϯϓϧ (defn distance [x

    y] (sqrt (+ (* x x) (* y y)))) ΦϖϨʔλ Ҿ਺
  7. $PEFBT%BUB ‣ $MPKVSFίʔυΛ؆୯ʹσʔλͱͯ͠ѻ͑Δ ‣ $MPKVSFͷσʔλͰίʔυΛॻ͍͍ͯΔͱ΋͍͑Δ (read-string “(* x x)”) ;=>

    (* x x)
  8. $PEFBT%BUB ‣ $MPKVSFίʔυΛ؆୯ʹσʔλͱͯ͠ѻ͑Δ ‣ $MPKVSFͷσʔλͰίʔυΛॻ͍͍ͯΔͱ΋͍͑Δ (read-string “(* x x)”) ;=>

    (* x x) ͭͷγϯϘϧ Y Y͔ΒͳΔϦετ
  9. ॲཧܥͰͷྲྀΕ ίʔυ จࣈྻ ίʔυ σʔλ όΠτίʔυ

  10. ॲཧܥͰͷྲྀΕ ίʔυ จࣈྻ ίʔυ σʔλ όΠτίʔυ Ϧʔυ ίϯύΠϧ

  11. ϚΫϩ

  12. ϚΫϩͱ͸ ‣ ίʔυ͔Βίʔυ΁ͷม׵Λఆٛ͢Δؔ਺ ‣ Ϣʔβ͕޷͖ͳΑ͏ʹߏจΛఆٛͰ͖Δ࢓૊Έ ‣ ૉ๿ͳ͕Βڧྗͳϝλϓϩάϥϛϯάͷπʔϧ

  13. ॲཧܥͰͷྲྀΕ ίʔυ จࣈྻ ίʔυ σʔλ όΠτίʔυ Ϧʔυ ίϯύΠϧ

  14. ॲཧܥͰͷྲྀΕ ίʔυ จࣈྻ ίʔυ σʔλ όΠτίʔυ Ϧʔυ ίϯύΠϧ ϚΫϩల։

  15. PSΛఆٛͯ͠ΈΔ ‣ ͭΊͷࣜͷ஋͕USVFͩͬͨΒͭΊΛ୹བྷධՁ (or (expr-1) (expr-2)) (let [v (expr-1)] (if

    v v (expr-2))) ల։
  16. PSΛఆٛͯ͠ΈΔ (defmacro or [expr1 expr2] `(let [v# ~expr1] (if v#

    v# ~expr2)))
  17. XJUIPQFOΛఆٛͯ͠ΈΔ ‣ +BWBͷUSZXJUISFTPVSDFTͷΑ͏ͳߏจ (with-open [f (reader “foo”)] (write-to-file f)) (let

    [f (reader “foo”)] (try (write-to-file f) (finally (.close f)))) ల։
  18. XJUIPQFOΛఆٛͯ͠ΈΔ (defmacro with-open [[name file] body] `(let [~name ~file] (try

    ~body (finally (.close ~name)))))
  19. ࣮։ൃͰͷϚΫϩ

  20. $PNQPKVSF ‣ $MPKVSFͷϧʔςΟϯάϥΠϒϥϦ (defroutes app-routes (GET “/” [] (index)) (GET

    “/en” [] (index)) (GET “/fr” [] (index-fr)) (GET “/:greeting” [greeting] (view greeting)))
  21. DPSFNBUDI ‣ ύλʔϯϚονϥΠϒϥϦ (match inst [:mov z x] (recur (subst

    z x) insts) [:add z x y] (recur (do-op + z x y) insts) [:sub z x y] (recur (do-op - z x y) insts) [:push z x] (recur (push z x) insts) [:pop z] (recur (pop z) insts) [:jnz l] (if (:flag env) [l env] (recur env insts)) [:jz l] (if (:flag env) (recur env insts) [l env] …)
  22. DPSFUZQFE ‣ ੩తܕνΣοΫϥΠϒϥϦ (ann new-file [(U URI String) -> File])

    (defn new-file [s] (if (string? s) (File. ^String s) (File. ^URI s)))
  23. DPSFBTZOD ‣ νϟωϧϕʔεͷඇಉظϓϩάϥϛϯάϥΠϒϥϦ (go (while true (let [v (<! ch)]

    (println v)))) (go (loop [i 0] (>! ch i) (recur (inc i)))) ड৴ଆίʔυ ૹ৴ଆίʔυ
  24. ͓·͚ ‣ ϚΫϩ͸ඇৗʹڧྗͳͷͰѱ༻ͯ͠ݴޠࣗମΛେ෯ ʹม͑ͯ͠·͏͜ͱ΋Ͱ͖Δ ‣ ྫʣΰϧϑ༻3VCZॲཧܥHPSVCZͷ໛฿  ϝιουΛ୹ॖܗͰݺ΂Δ qFYNBUDIJOH 

    - 1.up(10) {|i| p i} - [1,4,9].ew{|x,i| ps “#{i}:#{x}”} upto each_with_index puts
  25. HPDMPKVSF (doseq [[l n] (frequencies (map #(count (str %)) (keys

    (ns-publics ’clojure.core))))] (printf “%2d %s\n” l (apply str (repeat n \*)))) ΦϦδφϧͷίʔυ
  26. HPDMPKVSF (doseq [[l n] (frequencies (map #(count (str %)) (keys

    (ns-publics ’clojure.core))))] (printf “%2d %s\n” l (apply str (repeat n \*)))) (goclojure (dq [[l n] (qs (mp #(ou (sr %)) (ks (nsb ’clojure.core))))] (pf “%2d %s\n” l (ay sr (rep n \*)))) ΦϦδφϧͷίʔυ HPDMPKVSFΛ࢖ͬͯॻ͖׵͑ͨίʔυ
  27. ·ͱΊ ‣ -JTQͰ͋Δ͜ͱ͸$MPKVSFͷجૅΛ࡞Δཁૉͷͭ ‣ ϚΫϩ͸ڧྗͳϝλϓϩάϥϛϯάͷπʔϧ ‣ ࣮։ൃͰ࢖͑Δ͞·͟·ͳศརϚΫϩ͕͋Δ ‣ ͨͩ͠ཚ༻͗͢͠Δͱίʔυ͕ಡΊͳ͘ͳΔͷͰɺ ຊ౰ʹඞཁͳγνϡΤʔγϣϯΛݟۃΊΔ͜ͱ