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

ここ最近のClojureScript

 ここ最近のClojureScript

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

OHTA Shogo

March 14, 2017
Tweet

More Decks by OHTA Shogo

Other Decks in Programming

Transcript

  1. (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
  2. (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 ϚΫϩͷ৔߹
  3. (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 ϚΫϩͷ৔߹ ͦΕҎ֎ͷ৔߹
  4. (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
  5. (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
  6. (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
  7. (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
  8. &YUFSOT*OGFSFODF ‣ $MPKVSF4DSJQUίϯύΠϥ͸಺෦Ͱ(PPHMF$MPTVSF $PNQJMFS +4ˠ+4ͷ࠷దԽίϯύΠϥ Λ࢖͏ ‣ $MPTVSF$PNQJMFS͸࠷దԽΦϓγϣϯʹΑͬͯ͸ϑΟʔ ϧυ໊΍ϝιου໊Λ͔ͳΓΞάϨογϒʹϦωʔϜ͢Δ ‣

    ֎෦ϥΠϒϥϦΛ࢖͏৔߹౳ɺ$MPTVSF$PNQJMFS͕஌Β ͳ໊͍લ͕ଘࡏ͢Δͱίʔυ͕յΕΔ৔߹΋ ‣ &YUFSOT*OGFSFODFʹΑΓܕώϯτΛ͚ͭΔ͚ͩͰ໊લ ΛղܾͰ͖ΔΑ͏ʹͳͬͨ
  9. $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
  10. $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
  11. $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
  12. $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
  13. $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
  14. $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
  15. $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
  16. ϦωʔϜ໰୊ͷճආࡦ ‣ FYUFSOTϑΝΠϧΛ࡞Δ  $MPTVSF$PNQJMFSʹ֎෦ϥΠϒϥϦͷɹɹɹɹɹɹɹɹɹɹ ໊લΛڭ͑Δํ๏ ‣ DMKTKTϥΠϒϥϦΛ࢖͏  FYUFSOTϑΝΠϧΛόϯυϧͨ͠+4ϥΠϒϥϦ

    ‣ ഑ྻͷΠϯσΫγϯάͰ໊લʹΞΫηε  agetasetΛ࢖͏  $MPTVSF$PNQJMFSͷϦωʔϜͰจࣈྻ͸มߋ͞Εͳ͍͜ͱΛ ར༻ var Foo = { ”bar” = {}, ”baz" = {} } externs.js
  17. &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 ࣗಈੜ੒ʂ
  18. +4Ϟδϡʔϧαϙʔτ BMQIB ‣ +4ʹ͸Ϟδϡʔϧͷఆٛํ๏͕͍ͭ͘΋͋Δ  $PNNPO+4 ".% &$."4DSJQU ʜ ‣

    $MPKVSF4DSJQUඪ४Ͱ͸$MPTVSF-JCSBSZϞδϡʔϧ ͔͠ѻ͑ͳ͔ͬͨ ‣ ίϯύΠϥΦϓγϣϯͰϞδϡʔϧλΠϓΛࢦఆ͢Δ ͜ͱͰͦΕҎ֎ͷϞδϡʔϧΛ࢖͑ΔΑ͏ʹ :foreign-libs [{:file "resources/libs/calculator.js" :provides ["calculator"] :module-type :commonjs}]
  19. ࢀߟ ‣ $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ͷϏϧυͷղઆهࣄ