Pro Yearly is on sale from $80 to $50! »

ここ最近のClojureScript

 ここ最近のClojureScript

2017/03/14に開催されたnishi-shinju-clojure #1の発表資料です。

E170ae2eb0adcf3d136ab0990f0ea671?s=128

OHTA Shogo

March 14, 2017
Tweet

Transcript

  1. ͜͜࠷ۙͷ ɹ$MPKVSF4DSJQU OJTIJTIJOKVDMPKVSFPO !BUIPT

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

  3. ͜͜࠷ۙͷ$MPKVSF$MPKVSF4DSJQU ‣ $MPKVSFք۾͸͕ϦϦʔε͞Εͣɺ͋·Γେ͖ͳ ಈ͖͕ͳ͍ ‣ ҰํͰɺ$MPKVSF4DSJQUͷपล͸Ί·͙Δ͘͠มԽ ͍ͯ͠Δ ‣ ࠓճ͸ͦͷͳ͔Ͱɺൺֱతʹେ͖ͦ͏ͩͱݸਓతʹ ࢥ͏΋ͷΛϐοΫΞοϓͯ͠঺հ

  4. $MPKVSF4DSJQUͷେ͖ͳมԽ ‣ .BDSP*OGFSFODF ‣ &YUFSOT*OGFSFODF ‣ +4Ϟδϡʔϧαϙʔτ BMQIB  ‣

    ηϧϑϗετ$MPKVSF4DSJQUπʔϧଓʑొ৔
  5. .BDSP*OGFSFODF ‣ $MPKVSF4DSJQUͰ͸ϚΫϩΛ$MPKVSFͰॻ͘ ‣ ͜Ε·Ͱ͸ϓϩάϥϚ͕ʮͲͷࣝผࢠ͕ϚΫϩ͔ʯ Λ໌ࣔతʹίϯύΠϥʹڭ͑ͯ΍Δඞཁ͕͋ͬͨ ‣ .BDSPJOGFSFODFʹΑΓɺίϯύΠϥ͕উखʹ Ͳͷࣝผࢠ͕ϚΫϩ͔Λࣝผͯ͘͠ΕΔΑ͏ʹ

  6. (ns bar.core (:require-macros [foo.core :refer [add]]) (:require [foo.core :refer [subtract]]))

    (add 2 3) (subtract 7 4) ͜Ε·Ͱͷίʔυ ‣ ϚΫϩ͔Ͳ͏͔Ͱ:require:require-macrosΛ࢖͍෼͚Δඞཁ͋Γ ‣ bar.coreΛDMKDʹ͢Δ৔߹ʹ͸SFBEFSDPOEJUJPOBM͕ඞཁ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs
  7. (ns bar.core (:require-macros [foo.core :refer [add]]) (:require [foo.core :refer [subtract]]))

    (add 2 3) (subtract 7 4) ͜Ε·Ͱͷίʔυ ‣ ϚΫϩ͔Ͳ͏͔Ͱ:require:require-macrosΛ࢖͍෼͚Δඞཁ͋Γ ‣ bar.coreΛDMKDʹ͢Δ৔߹ʹ͸SFBEFSDPOEJUJPOBM͕ඞཁ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs ϚΫϩͷ৔߹
  8. (ns bar.core (:require-macros [foo.core :refer [add]]) (:require [foo.core :refer [subtract]]))

    (add 2 3) (subtract 7 4) ͜Ε·Ͱͷίʔυ ‣ ϚΫϩ͔Ͳ͏͔Ͱ:require:require-macrosΛ࢖͍෼͚Δඞཁ͋Γ ‣ bar.coreΛDMKDʹ͢Δ৔߹ʹ͸SFBEFSDPOEJUJPOBM͕ඞཁ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs ϚΫϩͷ৔߹ ͦΕҎ֎ͷ৔߹
  9. (ns bar.core (:require [foo.core :refer [subtract] :refer-macros [add]])) (add 2

    3) (subtract 7 4) ͜Ε·Ͱͷίʔυ ‣ :refer-macrosΛ࢖ͬͯ:requireΛͭʹ·ͱΊΔͱଟগ͖ͬ͢Γ ‣ Կ͕ϚΫϩ͔ΛϓϩάϥϚ͕஫ҙ͢Δඞཁ͸·ͩ͋Δ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs
  10. (ns bar.core (:require [foo.core :as foo :include-macros true])) (foo/add 2

    3) (foo/subtract 7 4) ͜Ε·Ͱͷίʔυ ‣ :include-macrosΛ࢖͑͹Կ͕ϚΫϩ͔Λؾʹ͢Δඞཁ͸ͳ͘ͳΔ ‣ bar.coreΛDMKDʹ͢Δ৔߹ʹ͸SFBEFSDPOEJUJPOBM͕ඞཁ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs
  11. (ns bar.core (:require [foo.core :refer [add subtract]])) (add 2 3)

    (subtract 7 4) ͜Ε͔Β͸͜͏ॻ͚Δ ϚΫϩΛఆٛ͢Δ໊લۭؒଆͰҰ޻෉͓ͯ͘͠ͱɺԿ͕ ϚΫϩ͔͸ίϯύΠϥͰࣗಈతʹ൑ผͯ͘͠ΕΔΑ͏ʹ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core (:require-macros foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs
  12. (ns bar.core (:require [foo.core :refer [add subtract]])) (add 2 3)

    (subtract 7 4) ͜Ε͔Β͸͜͏ॻ͚Δ ϚΫϩΛఆٛ͢Δ໊લۭؒଆͰҰ޻෉͓ͯ͘͠ͱɺԿ͕ ϚΫϩ͔͸ίϯύΠϥͰࣗಈతʹ൑ผͯ͘͠ΕΔΑ͏ʹ (ns foo.core) (defmacro add [a b] `(+ ~a ~b)) (ns foo.core (:require-macros foo.core) (defn subtract [a b] (- a b)) foo/core.clj foo/core.cljs bar/core.cljs
  13. &YUFSOT*OGFSFODF ‣ $MPKVSF4DSJQUίϯύΠϥ͸಺෦Ͱ(PPHMF$MPTVSF $PNQJMFS +4ˠ+4ͷ࠷దԽίϯύΠϥ Λ࢖͏ ‣ $MPTVSF$PNQJMFS͸࠷దԽΦϓγϣϯʹΑͬͯ͸ϑΟʔ ϧυ໊΍ϝιου໊Λ͔ͳΓΞάϨογϒʹϦωʔϜ͢Δ ‣

    ֎෦ϥΠϒϥϦΛ࢖͏৔߹౳ɺ$MPTVSF$PNQJMFS͕஌Β ͳ໊͍લ͕ଘࡏ͢Δͱίʔυ͕յΕΔ৔߹΋ ‣ &YUFSOT*OGFSFODFʹΑΓܕώϯτΛ͚ͭΔ͚ͩͰ໊લ ΛղܾͰ͖ΔΑ͏ʹͳͬͨ
  14. $MPTVSF$PNQJMFSͷϦωʔϜ ίʔυΛNJOJGZ͢ΔࡍɺίϯύΠϥ͕੔߹ੑΛอͬͨ·· ϑΟʔϧυ໊ɾϝιου໊ΛϦωʔϜ (deftype Foo [bar baz]) (defn make-foo [bar

    baz] (Foo. bar baz)) (defn area [foo] (* (.-bar foo) (.-baz foo))) function He(a,b) { return { this.xb=a; this.yb=b; } } … function Je(a,b) { return a.xb*b.yb } ClojureScript ίϯύΠϧޙJavaScript
  15. $MPTVSF$PNQJMFSͷϦωʔϜ ίʔυΛNJOJGZ͢ΔࡍɺίϯύΠϥ͕੔߹ੑΛอͬͨ·· ϑΟʔϧυ໊ɾϝιου໊ΛϦωʔϜ (deftype Foo [bar baz]) (defn make-foo [bar

    baz] (Foo. bar baz)) (defn area [foo] (* (.-bar foo) (.-baz foo))) function He(a,b) { return { this.xb=a; this.yb=b; } } … function Je(a,b) { return a.xb*b.yb } ClojureScript ίϯύΠϧޙJavaScript
  16. $MPTVSF$PNQJMFSͷϦωʔϜ ίʔυΛNJOJGZ͢ΔࡍɺίϯύΠϥ͕੔߹ੑΛอͬͨ·· ϑΟʔϧυ໊ɾϝιου໊ΛϦωʔϜ (deftype Foo [bar baz]) (defn make-foo [bar

    baz] (Foo. bar baz)) (defn area [foo] (* (.-bar foo) (.-baz foo))) function He(a,b) { return { this.xb=a; this.yb=b; } } … function Je(a,b) { return a.xb*b.yb } ClojureScript ίϯύΠϧޙJavaScript
  17. $MPTVSF$PNQJMFSͷϦωʔϜ ίʔυΛNJOJGZ͢ΔࡍɺίϯύΠϥ͕੔߹ੑΛอͬͨ·· ϑΟʔϧυ໊ɾϝιου໊ΛϦωʔϜ (deftype Foo [bar baz]) (defn make-foo [bar

    baz] (Foo. bar baz)) (defn area [foo] (* (.-bar foo) (.-baz foo))) function He(a,b) { return { this.xb=a; this.yb=b; } } … function Je(a,b) { return a.xb*b.yb } ClojureScript ίϯύΠϧޙJavaScript
  18. $MPTVSF$PNQJMFSͷϦωʔϜ ίʔυΛNJOJGZ͢ΔࡍɺίϯύΠϥ͕੔߹ੑΛอͬͨ·· ϑΟʔϧυ໊ɾϝιου໊ΛϦωʔϜ (deftype Foo [bar baz]) (defn make-foo [bar

    baz] (Foo. bar baz)) (defn area [foo] (* (.-bar foo) (.-baz foo))) function He(a,b) { return { this.xb=a; this.yb=b; } } … function Je(a,b) { return a.xb*b.yb } ClojureScript ίϯύΠϧޙJavaScript
  19. $MPTVSF$PNQJMFSͷϦωʔϜ +4ϥΠϒϥϦΛ࢖͏৔߹౳ίϯύΠϥ͕஌Βͳ໊͍લΛࢀর ͨ͠৔߹ʹ͸ϦωʔϜͰ੔߹ੑ͕ͱΕͣΤϥʔʹͳΔ͜ͱ΋ (defn make-foo [bar baz] (js/Foo. bar baz))

    (defn area [foo] (* (.-bar foo) (.-baz foo))) function Ne(a,b) { return new Foo(a,b) } … function Oe(a,b) { return a.Nb*b.Ob } ClojureScript ίϯύΠϧޙJavaScript function Foo(bar, baz) { this.bar = bar; this.baz = baz; } JavaScript
  20. $MPTVSF$PNQJMFSͷϦωʔϜ +4ϥΠϒϥϦΛ࢖͏৔߹౳ίϯύΠϥ͕஌Βͳ໊͍લΛࢀর ͨ͠৔߹ʹ͸ϦωʔϜͰ੔߹ੑ͕ͱΕͣΤϥʔʹͳΔ͜ͱ΋ (defn make-foo [bar baz] (js/Foo. bar baz))

    (defn area [foo] (* (.-bar foo) (.-baz foo))) function Ne(a,b) { return new Foo(a,b) } … function Oe(a,b) { return a.Nb*b.Ob } ClojureScript ίϯύΠϧޙJavaScript function Foo(bar, baz) { this.bar = bar; this.baz = baz; } JavaScript
  21. ϦωʔϜ໰୊ͷճආࡦ ‣ FYUFSOTϑΝΠϧΛ࡞Δ  $MPTVSF$PNQJMFSʹ֎෦ϥΠϒϥϦͷɹɹɹɹɹɹɹɹɹɹ ໊લΛڭ͑Δํ๏ ‣ DMKTKTϥΠϒϥϦΛ࢖͏  FYUFSOTϑΝΠϧΛόϯυϧͨ͠+4ϥΠϒϥϦ

    ‣ ഑ྻͷΠϯσΫγϯάͰ໊લʹΞΫηε  agetasetΛ࢖͏  $MPTVSF$PNQJMFSͷϦωʔϜͰจࣈྻ͸มߋ͞Εͳ͍͜ͱΛ ར༻ var Foo = { ”bar” = {}, ”baz" = {} } externs.js
  22. &YUFSOT*OGFSFODF ‣ &YUFSOTJOGFSFODFΛ࢖͑͹ɺίϯύΠϥ͕FYUFSOT ϑΝΠϧΛࣗಈੜ੒ͯ͘͠ΕΔ ‣ ϦωʔϜ͕໰୊ʹͳΔՕॴʹܕώϯτΛ͚ͭΔ (set! *warn-on-infer* true) (defn

    make-foo [bar baz] (js/Foo. bar baz)) (defn area [^js/Foo foo] (* (.-bar foo) (.-baz foo))) ClojureScript var Foo; Foo.prototype.bar; Foo.prototype.bar; inferred-externs.js ࣗಈੜ੒ʂ
  23. +4Ϟδϡʔϧαϙʔτ BMQIB ‣ +4ʹ͸Ϟδϡʔϧͷఆٛํ๏͕͍ͭ͘΋͋Δ  $PNNPO+4 ".% &$."4DSJQU ʜ ‣

    $MPKVSF4DSJQUඪ४Ͱ͸$MPTVSF-JCSBSZϞδϡʔϧ ͔͠ѻ͑ͳ͔ͬͨ ‣ ίϯύΠϥΦϓγϣϯͰϞδϡʔϧλΠϓΛࢦఆ͢Δ ͜ͱͰͦΕҎ֎ͷϞδϡʔϧΛ࢖͑ΔΑ͏ʹ :foreign-libs [{:file "resources/libs/calculator.js" :provides ["calculator"] :module-type :commonjs}]
  24. ηϧϑϗετ$MPKVSF4DSJQUπʔϧଓʑొ৔ ‣ $MPKVSF4DSJQU͕ࣗ෼ࣗ਎ΛίϯύΠϧ ηϧϑϗεςΟϯά ɹ Ͱ͖ΔΑ͏ʹͳͬͨ͜ͱʹΑΓɺ$MPKVSFʹґଘ͠ͳ͍ॲཧܥ ͕ग़͖͍ͯͯΔ ‣ ࣮ߦʹ+7.͕ඞཁͳ͍ͷͰىಈ͕࣌ؒ୹͘ɺεΫϦϓτ࣮ߦ ౳ͷ༻్ʹ޲͘΋ͷ΋͋Δ

    ‣ ॲཧܥͷྫ  1MBODL$MPKVSF4DSJQUPO+BWB4DSJQU$PSF  -VNP$MPKVSF4DSJQUPO/PEFKT  DBMWJO-VNP্Ͱಈ͘$MPKVSF4DSJQU༻Ϗϧυπʔϧ
  25. ͕࣌ؒ͋Ε͹ -VNPʹ͍ͭͯܰ͘σϞ

  26. -VNPͷಛ௃ ‣ OQNͰαΫοͱΠϯετʔϧͰ͖Δ ‣ ىಈ͕ૣ͍ ‣ ୯ମͰ$MPKVSF4DSJQUͷϏϧυ͕Ͱ͖Δ

  27. ·ͱΊ ‣ $MPKVSF4DSJQUͷ$MPKVSF΍+4ʹର͢Δ਌࿨ੑ͸ɹ ·͢·͢ߴ·͍ͬͯΔ ‣ ҰํͰɺηϧϑϗεςΟϯάʹΑΓ$MPKVSF4DSJQU ୯ମͰΤίγεςϜΛங͚ΔՄೳੑ·Ͱݟ͖͑ͯͨ ‣ $MPKVSF$MPKVSF4DSJQU ʴ+4

    ͷ૊Έ߹Θͤํ͸ɹ ࠓޙ͞Βʹଟ༷Խ͍͖ͯͦ͠͏ͳงғؾ
  28. ࢀߟ ‣ $MPKVSF4DSJQU.BDSP4VHBS  IUUQCMPHpLFTGBSNDPNQPTUTDMPKVSFTDSJQUNBDSPTVHBSIUNM  .JLF'JLFT 1MBODL։ൃऀ ʹΑΔNBDSPJOGFSFODFͷղઆهࣄ ‣

    &YUFSOT*OGFSFODF  IUUQTHJTUHJUIVCDPNTXBOOPEFUUFGDDDDGDEBGD  %BWJE/PMFOʹΑΔղઆهࣄ ‣ +BWB4DSJQU.PEVMF4VQQPSU "MQIB   IUUQTHJUIVCDPNDMPKVSFDMPKVSFTDSJQUXJLJ+BWB4DSJQU.PEVMF4VQQPSU "MQIB   ެࣜXJLJϖʔδ ‣ $PNQJMJOH$MPKVSF4DSJQU1SPKFDUT8JUIPVUUIF+7.  IUUQTBONPOUFJSPDPNDPNQJMJOHDMPKVSFTDSJQUQSPKFDUTXJUIPVUUIFKWN  "OUÓOJP/.POUFJSP -VNP։ൃऀ ʹΑΔ+7.ͳ͠Ͱͷ$MPKVSF4DSJQUͷϏϧυͷղઆهࣄ
  29. ࢀߟ ‣ 1MBODL  IUUQTHJUIVCDPNNpLFTQMBODL ‣ -VNP  IUUQTHJUIVCDPNBONPOUFJSPMVNP ‣

    DBMWJO  IUUQTHJUIVCDPNFHJOF[DBMWJO