Clojure 1.10 概要紹介

E170ae2eb0adcf3d136ab0990f0ea671?s=47 OHTA Shogo
November 29, 2018

Clojure 1.10 概要紹介

2018/11/29のLisp meetup #70の発表資料です。

E170ae2eb0adcf3d136ab0990f0ea671?s=128

OHTA Shogo

November 29, 2018
Tweet

Transcript

  1. $MPKVSF֓ཁ঺հ -JTQNFFUVQPO !BUIPT

  2. ֓ཁ ‣ ઌ೔  ɺ3$͕ϦϦʔε  BMQIBϦϦʔε  CFUBϦϦʔε 

    3$ϦϦʔε ‣ pOBMϦϦʔε΋໨લͳͷͰɺͰͷओཁͳมߋʹ ͍͓ͭͯ͞Β͍
  3. $MPKVSFͷओཁͳมߋ ‣ Τϥʔϝοηʔδͷվળ ‣ EBUBGZ ‣ ϝλσʔλʹΑΔϓϩτίϧͷ֦ு ‣ QSFQM ‣

    UBQ
  4. Τϥʔϝοηʔδͷվળ ‣ ࠓճͷϦϦʔεʹ͓͚ΔҰ൪ͷ໨ۄ ‣ Τϥʔϝοηʔδͱͯ͠ɺϢʔβʹͲ͏͍ͬͨ৘ใΛఏࣔ ͢Δ͔Λൈຊతʹ੔ཧ ‣ ඪ४3&1-ͰͷΤϥʔϝοηʔδվળ͚ͩͰͳ͘ଞͷ։ൃ ؀ڥͰ΋Τϥʔ৘ใΛศརʹ׆༻Ͱ͖ΔΑ͏ͳ"1*Λఏڙ ‣

    ·ͩඪ४ͷΤϥʔϝοηʔδ͸ඞͣ͠΋෼͔Γ΍͍͢Θ͚ Ͱ͸ͳ͍ͷͰɺࠓޙͷ։ൃ؀ڥଆͷվળ͕ظ଴͞ΕΔ
  5. Τϥʔͷ෼͔Γʹ͘͞ͷݪҼ ‣ $MPKVSFͷଟ͘ͷؔ਺͸ΨʔϕδΠϯɾΨʔϕδΞ΢τ  Ҿ਺νΣοΫ͕ͳ͍ͨΊɺClassCastException΍ IllegalArgumentException౳ɺ௚઀తʹΤϥʔՕॴΛࣔ ͞ͳ͍Τϥʔ͕ग़ͯ͠·͏  DMPKVSFTQFDʹΑͬͯղܾ͞Ε͍ͯ͘ݟࠐΈ ʙ

     ‣ Τϥʔͷ৘ใྔ͕ଟ͗ͯ͢Կ͕ॏཁͳͷ͔෼͔Γʹ͍͘  ಛʹTQFDͷΤϥʔ৘ใ͕๲େʹͳΓ͕ͪ ‣ ॲཧܥଆͷݺͼग़͕͠ελοΫτϨʔεʹฆΕࠐΜͰ͠·͏  ॲཧܥ͕Ͳ͜ͰΤϥʔΛݟ͚͔ͭͨ͸ॏཁͰ͸ͳ͍
  6. Τϥʔͷ੔ཧ ‣ ͲͷϑΣʔζͰग़ͨΤϥʔ͔Λ෼ྨɺϑΣʔζຖʹ༗༻ͳ৘ใΛ࣋ͨͤΔ  ϑΣʔζɿϦʔυ࣌ɺϚΫϩߏจνΣοΫ࣌ɺϚΫϩల։࣌ɺίϯύΠϧ ࣌ɺ࣮ߦ࣌ɺ݁Ռग़ྗ࣌ ‣ CompilerException ࣮ߦ࣌ΑΓલͷϑΣʔζͷΤϥʔ ͔Βex-data

    ͰҎԼͷ৘ใΛڞ௨ͯ͠औಘՄೳʹ  :clojure.error/phase Τϥʔ͕ൃੜͨ͠ϑΣʔζ   :clojure.error/source Τϥʔ͕ൃੜͨ͠ιʔεϑΝΠϧ   :clojure.error/line Τϥʔ͕ൃੜͨ͠ιʔεϑΝΠϧͷߦ൪߸   :clojure.error/column Τϥʔ͕ൃੜͨ͠ιʔεϑΝΠϧͷܻ   :clojure.error/symbol ΤϥʔͷݪҼʹͳͬͨγϯϘϧ   :clojure.error/class ΤϥʔΫϥε໊   :clojure.error/cause Τϥʔϝοηʔδ   :clojure.error/spec εϖοΫΤϥʔ ͋Ε͹
  7. ॏཁ౓ͷ௿͍৘ใͷ࡟ݮ εϖοΫΤϥʔ σʔλ ౳ ਓؒʹͱͬͯ༗ӹͰͳ͍ ৘ใ͸දࣔ͠ͳ͍Α͏ʹ ͷϚΫϩߏจΤϥʔ ͷϚΫϩߏจΤϥʔ

  8. ΤϥʔՕॴͷࢦఠ ‣ ϦʔυΤϥʔ ‣ ίϯύΠϧΤϥʔ ʁ ຊ౰ʹ஌Γ͍ͨͷ͸ॲཧܥ͕ͲͷॲཧதʹΤϥʔΛ ݟ͚͔ͭͨͰ͸ͳ͘ίʔυͷͲ͜ʹΤϥʔ͕͔͋ͬͨ ʁ

  9. ex-triageex-str ‣ ࣮ߦ࣌Τϥʔ͸Ұൠʹ͸+BWBͷྫ֎ɾΤϥʔͷՄೳੑ΋  +BWBͷྫ֎ɾΤϥʔͷதʹ͸ɺ$MPKVSFʹ౎߹ͷ͍͍ଐੑΛ ࣋ͨͳ͍΋ͷ΋͋Δ ‣ ex-triage  ͋ΒΏΔΤϥʔΛ౷ҰతͳϑΥʔϚοτͷϚοϓσʔλʹ

    ม׵͢Δ  ϦϞʔτͰͭͳ͕Δ؀ڥͰΤϥʔ৘ใΛ΍ΓͱΓ͢ΔϑΥʔ Ϛοτͱͯ͠࢖͑Δ ‣ ex-str  ex-triageΛจࣈྻԽ͢Δඪ४తͳํ๏
  10. EBUBGZ ‣ DMPKVSFEBUBGZ ‣ ৽͍͠ϓϩτίϧDatafiableͱNavigable͕௥Ճ ‣ ͲͪΒͷϓϩτίϧ΋ৄࡉͳઆ໌͸·ͩͳ͍ ‣ جຊతʹ͸ΦϒδΣΫτΛʮσʔλԽʯ͢ΔͨΊͷ΋ͷ

  11. EBUBGZ ‣ :extend-via-metadataʹ͍ͭͯ͸ޙड़ (defprotocol Datafiable :extend-via-metadata true (datafy [o] “return

    a representation of o as data”)) (defprotocol Navigable :extend-via-metadata true (nav [coll k v] "return v in the context of coll and k (a key/index or nil)"))
  12. EBUBGZͷ࢖͍ํ ‣ ݱ࣌఺Ͱ͸ɺΫϥε΍ྫ֎౳গ਺ͷ࣮૷͕ఏڙ͞Ε͍ͯΔͷΈ (require ‘[clojure.datafy :as datafy]) (datafy/datafy java.io.Closeable) ;=>

    {:bases #{java.lang.AutoCloseable}, :flags #{:interface :public :abstract}, :members {close [#clojure.reflect.Method{…}, :name java.io.Closeable} (datafy/datafy (ex-info “error” {})) ;=> {:cause “error!!”, :data {:value 42}, :trace [[user$eval199 invokeStatic "NO_SOURCE_FILE" 1] [user$eval199 invoke "NO_SOURCE_FILE" 1] [clojure.lang.Compiler eval "Compiler.java" 7176] …] :via [{:type clojure.lang.ExceptionInfo, :message “error!!", :data {:value 42}, :at [user$eval199 invokeStatic "NO_SOURCE_FILE" 1]}]}
  13. EBUBGZͷิ଍ ‣ ʮσʔλԽʯͱ͍͏ͱ༻్͕෯޿͘ײ͡Δ͕ɺ Throwable->mapͷΑ͏ʹɺΦϒδΣΫτͷΠϯεϖΫ γϣϯ༻్͕ϝΠϯʹࢥ͑Δ ݸਓతײ૝   SFJGZͰ࡞ͬͨΦϒδΣΫτ͕಺෦తʹอ࣋͢Δ৘ใͷެ։ 

    ಛʹεϖοΫΦϒδΣΫτͷΠϯεϖΫγϣϯɺτϥόʔε ‣ ΦϒδΣΫτͷγϦΞϥΠζ౳ͷ༻్͸͋·Γ૝ఆ͞Εͯ ͍ͳ͍͜ͱ͕ެࣜʹ͸΄ͷΊ͔͞Ε͍ͯΔ
  14. ϝλσʔλʹΑΔϓϩτίϧͷ֦ு ‣ EBUBGZಋೖͷաఔͰऔΓೖΕΒΕͨ ‣ ͜Ε·Ͱϓϩτίϧ͸Ϩίʔυ΍σʔλܕͰ௚઀࣮૷͢Δ ͔ɺextend-protocolextend-typeͰ֦ு͢Δ͔ͩͬͨ ‣ ͔Β͸ϝλσʔλʹΑΓϓϩτίϧΛ֦ுͰ͖ΔΑ͏ ʹͳΓɺΦϒδΣΫτຖͷଟ૬తͳৼΔ෣͍ΛఆٛՄೳʹ

  15. ैདྷͷϓϩτίϧͷར༻๏ (defprotocol P (m [this])) ;; ϨίʔυʹΑΔ௚઀࣮૷ (defrecord R []

    P (m [this] 42)) (m (->R)) ;=> 42 ;; extend-protocol/extend-typeʹΑΔ֦ு (extend-protocol P Long (m [this] this)) (m 101) ;=> 101
  16. ϝλσʔλʹΑΔ֦ு ‣ :extend-via-metadata true Λࢦఆ͢Δͱɺϓϩτίϧ͕ ϝλσʔλʹΑΓ֦ுՄೳʹ (defprotocol P :extend-via-metadata true

    (m [this])) (def obj (with-meta {:x 42} {`m (fn [this] (:x this))})) (m obj) ;=> 42 (m (with-meta obj {`m (fn [this] 43)})) ;=> 43
  17. ϝλσʔλʹΑΔ֦ுͷ஫ҙ఺ ‣ ϓϩτίϧͷఆٛ࣌ʹ:extend-via-metadataͷࢦఆ͕ ͳ͚Ε͹ϝλσʔλʹΑΔ֦ு͸༗ޮʹͳΒͳ͍ ‣ ϓϩτίϧͷ࣮૷ͷ༏ઌॱҐ͸  ϨίʔυɾσʔλܕʹΑΔ௚઀࣮૷  ϝλσʔλʹΑΔ֦ு

     extend-protocolextend-typeʹΑΔ֦ு ‣ ϝλσʔλʹΑΔ֦ுΛ༗ޮʹͨ͠ϓϩτίϧͷϝιου ݺͼग़͠͸ੑೳ໘ͰͷΦʔόʔϔου͕͋Δ
  18. QSFQM ‣ lQSPHSBNNBCMFz3&1- ‣ ৽͍͠ετϦʔϜϕʔε3&1- ‣ $MPKVSFϑΥʔϜΛೖྗʹͱΓɺߏ଄Խ͞Εͨग़ྗΛฦ͢ ‣ Ͱಋೖ͞Εͨιέοταʔό͔Βར༻͠ɺϦϞʔτ 3&1-Λ࣮ݱ͢ΔͨΊʹ࢖͏͜ͱΛ૝ఆ͞Ε͍ͯΔ

    ‣ 6OSFQMϓϩτίϧʹྨࣅ͢Δ෦෼͕ଟ͍  IUUQTHJUIVCDPN6OSFQMVOSFQM
  19. QSFQMͷ࢖͍ํ ‣ ιέοταʔόͷىಈ ‣ ΫϥΠΞϯτ͔Βͷ઀ଓ ‣ $ clojure -J-Dclojure.server.repl="{:port 5555

    :accept clojure.core.server/io-prepl}" $ nc localhost 5555 (map inc [1 2 3]) {:tag :ret, :val "(2 3 4)", :ns "user", :ms 19, :form "(map inc [1 2 3])”} (dotimes [i 3] (Thread/sleep 1000) (println i)) {:tag :out, :val "0\n"} {:tag :out, :val "1\n"} {:tag :out, :val "2\n"} {:tag :ret, :val "nil", :ns "user", :ms 3214, :form "(dotimes [i 3] (Thread/sleep 1000) (println i))”}
  20. ैདྷͷ3&1-ͷ໰୊఺ ‣ ैདྷͷ3&1-Λιέοταʔό͔Β࢖͏͜ͱΛߟ͑Δͱʮධ Ձ݁Ռ͔͠ฦͤͳ͍ʯ͜ͱͷ͕خ͘͠ͳ͍͜ͱ΋ ‣ ͨͱ͑͹ʜ  ධՁ͍ͯ͠Δ్தͰग़ྗ݁Ռ౳ɺධՁ݁ՌҎ֎ͷ΋ͷΛฦ͢ ͷ͕೉͍͠ 

    ྫ֎ɾΤϥʔ͸ධՁ݁Ռͱ͸ผʹѻ͍͍ͨɺΫϥΠΞϯτଆ Ͱৄࡉͳ৘ใΛࣗ༝ʹऔಘ͍ͨ͠  ௕͍γʔέϯεͷলུදࣔɾల։ɺϚϧνϝσ ΟΞ ը૾ ͷ 3&1-Ͱͷදࣔ౳ɺΫϥΠΞϯτ΁ͷϦονͳػೳͷఏڙ ‣ ݱঢ়Ͱ͸ɺ͜ΕΒͷ໰୊͸O3&1-Ͱղܾ͞Ε͍ͯΔ
  21. O3&1-ͱͷҧ͍ ‣ O3&1-͸ೖྗ΋ߏ଄Խ͞Εͨϝοηʔδ  ϋϯυϥʹϛυϧ΢ΣΞΛ௥Ճ͢Δ͜ͱͰॲཧͰ͖Δϝο ηʔδͷछྨΛ૿΍͢࢓૊Έ  O3&1-αʔόىಈޙʹػೳΛ௥Ճ͢Δͷ͸೉͍͠ ‣ QSFQMͷೖྗ͸୯ͳΔϑΥʔϜ

     ػೳΛ௥Ճ͠Α͏ͱࢥ͑͹ͦͷػೳΛ࣮ݱ͢ΔϑΥʔϜΛ ྲྀ͠ࠐΉ͚ͩ  ΫϥΠΞϯτଆ͔ΒඞཁͳػೳΛ޷͖ͳΑ͏ʹ௥ՃͰ͖Δ l3&1-UIFVMUJNBUFDPOUFOUOFHPUJBUJPOQSPUPDPMz   DG6OSFQMCMPC
  22. ͞·͟·ͳQSFQM࣮૷ ‣ QSFQMɿඪ४ͷQSFQM ‣ JPQSFQM  ඪ४ೖग़ྗܦ༝Ͱ΍ΓͱΓ͢Δ࣮૷  ιέοταʔό͔Βݺͼग़͞ΕΔ͜ͱΛ૝ఆ ‣

    SFNPUFQSFQM  ϦϞʔτͰ଴ͪड͚ΔQSFQMαʔόʹ઀ଓ͢Δ࣮૷
  23. UBQ ‣ άϩʔόϧʹར༻ՄೳͳγϯϓϧͳΠϕϯτγεςϜ ‣ σόοά΍ϩΪϯά౳ͷ༻్Λҙਤ͍ͯ͠Δ ‣ QSFQMಋೖʹ൐ͬͯऔΓೖΕΒΕͨ

  24. UBQͷ࢖͍ํ ‣ add-tapremove-tapͰϋϯυϥؔ਺Λొ࿥ɾ࡟আ ‣ tap>Ͱ஋Λૹ৴ɺ͢΂ͯͷϋϯυϥؔ਺Ͱ஋͕ॲཧ͞ΕΔ (defn log [x] (println “tapped

    value: “ x)) (add-tap log) (tap> 1) ;; tapped value: 1 ;=> 1 (tap> 2) ;; tapped value: 2 ;=> 2 (remove-tap log) (tap> 3) ;=> 3
  25. QSFQMͰͷUBQ ‣ UBQ͸QSFQM্Ͱಛผʹॲཧ͞ΕΔ ‣ QSFQM্Ͱσόοά΍ϩΪϯάɺϓϩάϨεͷදࣔ౳ʹར༻ Ͱ͖Δ $ nc localhost 5555

    (dotimes [i 5] (Thread/sleep 1000) (tap> i)) {:tag :tap, :val "0"} {:tag :tap, :val "1"} {:tag :tap, :val "2"} {:tag :tap, :val "3"} {:tag :tap, :val "4"} {:tag :ret, :val "nil", :ns "user", :ms 5216, :form "(dotimes [i 5] (Thread/sleep 1000) (tap> i))"}
  26. ͦͷଞͷมߋ ‣ +7.ͷ࠷খαϙʔτόʔδϣϯมߋ  ࠷খαϙʔτόʔδϣϯ͕͔Βʹ ‣ SFRVJSJOHSFTPMWF  ʮ࣮ߦ࣌ʹSFRVJSFͯ͠ݺͼग़͠ʯ͕؆୯ʹॻ͚ΔΑ͏ʹ ‣

    SFBE TUSJOH  Ϧʔυ݁Ռͱͯ͠ݩͷจࣈྻදݱ΋ҰॹʹऔಘͰ͖Δ
  27. ·ͱΊ ‣ $MPKVSF͸ΤϥʔϝοηʔδͷվળͱQSFQMͷ௥ՃΛத ৺ͱͨ͠มߋ ‣ ݸʑͷಠཱͨ͠ػೳͱ͍͏ΑΓɺϦονͳ։ൃ؀ڥΛ։ൃ ͢ΔͨΊͷ࿮૊ΈΛఏڙ͢ΔͨΊͷϦϦʔεͱ͍͏ҹ৅ ‣ 4UBZUVOFE

  28. ࢀߟจݙ ‣ DIBOHFTNE  IUUQTHJUIVCDPNDMPKVSFDMPKVSFCMPCNBTUFSDIBOHFTNE ‣ 6OSFQM  IUUQTHJUIVCDPN6OSFQMVOSFQM