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

Clojureの基礎 〜マクロ編〜

Clojureの基礎 〜マクロ編〜

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

OHTA Shogo

June 11, 2016
Tweet

More Decks by OHTA Shogo

Other Decks in Programming

Transcript

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  7. $PEFBT%BUB
    ‣ $MPKVSFίʔυΛ؆୯ʹσʔλͱͯ͠ѻ͑Δ
    ‣ $MPKVSFͷσʔλͰίʔυΛॻ͍͍ͯΔͱ΋͍͑Δ
    (read-string “(* x x)”)
    ;=> (* x x)
    ͭͷγϯϘϧ Y Y͔ΒͳΔϦετ

    View full-size slide

  8. ॲཧܥͰͷྲྀΕ
    ίʔυ
    จࣈྻ

    ίʔυ
    σʔλ

    όΠτίʔυ

    View full-size slide

  9. ॲཧܥͰͷྲྀΕ
    ίʔυ
    จࣈྻ

    ίʔυ
    σʔλ

    όΠτίʔυ
    Ϧʔυ ίϯύΠϧ

    View full-size slide

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

    View full-size slide

  11. ॲཧܥͰͷྲྀΕ
    ίʔυ
    จࣈྻ

    ίʔυ
    σʔλ

    όΠτίʔυ
    Ϧʔυ ίϯύΠϧ

    View full-size slide

  12. ॲཧܥͰͷྲྀΕ
    ίʔυ
    จࣈྻ

    ίʔυ
    σʔλ

    όΠτίʔυ
    Ϧʔυ ίϯύΠϧ
    ϚΫϩల։

    View full-size slide

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

    View full-size slide

  14. PSΛఆٛͯ͠ΈΔ
    (defmacro or [expr1 expr2]
    `(let [v# ~expr1]
    (if v#
    v#
    ~expr2)))

    View full-size slide

  15. XJUIPQFOΛఆٛͯ͠ΈΔ
    ‣ +BWBͷUSZXJUISFTPVSDFTͷΑ͏ͳߏจ
    (with-open [f (reader “foo”)]
    (write-to-file f))
    (let [f (reader “foo”)]
    (try
    (write-to-file f)
    (finally
    (.close f))))
    ల։

    View full-size slide

  16. XJUIPQFOΛఆٛͯ͠ΈΔ
    (defmacro with-open [[name file] body]
    `(let [~name ~file]
    (try
    ~body
    (finally
    (.close ~name)))))

    View full-size slide

  17. ࣮։ൃͰͷϚΫϩ

    View full-size slide

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

    View full-size slide

  19. 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] …)

    View full-size slide

  20. DPSFUZQFE
    ‣ ੩తܕνΣοΫϥΠϒϥϦ
    (ann new-file [(U URI String) -> File])
    (defn new-file [s]
    (if (string? s)
    (File. ^String s)
    (File. ^URI s)))

    View full-size slide

  21. DPSFBTZOD
    ‣ νϟωϧϕʔεͷඇಉظϓϩάϥϛϯάϥΠϒϥϦ
    (go
    (while true
    (let [v ((println v))))
    (go
    (loop [i 0]
    (>! ch i)
    (recur (inc i))))
    ड৴ଆίʔυ ૹ৴ଆίʔυ

    View full-size slide

  22. ͓·͚
    ‣ ϚΫϩ͸ඇৗʹڧྗͳͷͰѱ༻ͯ͠ݴޠࣗମΛେ෯
    ʹม͑ͯ͠·͏͜ͱ΋Ͱ͖Δ
    ‣ ྫʣΰϧϑ༻3VCZॲཧܥHPSVCZͷ໛฿
    ϝιουΛ୹ॖܗͰݺ΂Δ qFYNBUDIJOH

    - 1.up(10) {|i| p i}
    - [1,4,9].ew{|x,i| ps “#{i}:#{x}”}
    upto
    each_with_index puts

    View full-size slide

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

    View full-size slide

  24. 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Λ࢖ͬͯॻ͖׵͑ͨίʔυ

    View full-size slide

  25. ·ͱΊ
    ‣ -JTQͰ͋Δ͜ͱ͸$MPKVSFͷجૅΛ࡞Δཁૉͷͭ
    ‣ ϚΫϩ͸ڧྗͳϝλϓϩάϥϛϯάͷπʔϧ
    ‣ ࣮։ൃͰ࢖͑Δ͞·͟·ͳศརϚΫϩ͕͋Δ
    ‣ ͨͩ͠ཚ༻͗͢͠Δͱίʔυ͕ಡΊͳ͘ͳΔͷͰɺ
    ຊ౰ʹඞཁͳγνϡΤʔγϣϯΛݟۃΊΔ͜ͱ

    View full-size slide