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. ͜͜࠷ۙͷ
    ɹ$MPKVSF4DSJQU
    OJTIJTIJOKVDMPKVSFPO
    !BUIPT

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    ‣ ηϧϑϗετ$MPKVSF4DSJQUπʔϧଓʑొ৔

    View full-size slide

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

    View full-size slide

  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

    View full-size slide

  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
    ϚΫϩͷ৔߹

    View full-size slide

  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
    ϚΫϩͷ৔߹
    ͦΕҎ֎ͷ৔߹

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  13. &YUFSOT*OGFSFODF
    ‣ $MPKVSF4DSJQUίϯύΠϥ͸಺෦Ͱ(PPHMF$MPTVSF
    $PNQJMFS +4ˠ+4ͷ࠷దԽίϯύΠϥ
    Λ࢖͏
    ‣ $MPTVSF$PNQJMFS͸࠷దԽΦϓγϣϯʹΑͬͯ͸ϑΟʔ
    ϧυ໊΍ϝιου໊Λ͔ͳΓΞάϨογϒʹϦωʔϜ͢Δ
    ‣ ֎෦ϥΠϒϥϦΛ࢖͏৔߹౳ɺ$MPTVSF$PNQJMFS͕஌Β
    ͳ໊͍લ͕ଘࡏ͢Δͱίʔυ͕յΕΔ৔߹΋
    ‣ &YUFSOT*OGFSFODFʹΑΓܕώϯτΛ͚ͭΔ͚ͩͰ໊લ
    ΛղܾͰ͖ΔΑ͏ʹͳͬͨ

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  21. ϦωʔϜ໰୊ͷճආࡦ
    ‣ FYUFSOTϑΝΠϧΛ࡞Δ
    $MPTVSF$PNQJMFSʹ֎෦ϥΠϒϥϦͷɹɹɹɹɹɹɹɹɹɹ
    ໊લΛڭ͑Δํ๏
    ‣ DMKTKTϥΠϒϥϦΛ࢖͏
    FYUFSOTϑΝΠϧΛόϯυϧͨ͠+4ϥΠϒϥϦ
    ‣ ഑ྻͷΠϯσΫγϯάͰ໊લʹΞΫηε
    agetasetΛ࢖͏
    $MPTVSF$PNQJMFSͷϦωʔϜͰจࣈྻ͸มߋ͞Εͳ͍͜ͱΛ
    ར༻
    var Foo = {
    ”bar” = {},
    ”baz" = {}
    }
    externs.js

    View full-size slide

  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
    ࣗಈੜ੒ʂ

    View full-size slide

  23. +4Ϟδϡʔϧαϙʔτ BMQIB

    ‣ +4ʹ͸Ϟδϡʔϧͷఆٛํ๏͕͍ͭ͘΋͋Δ
    $PNNPO+4 ".% &$."4DSJQU ʜ
    ‣ $MPKVSF4DSJQUඪ४Ͱ͸$MPTVSF-JCSBSZϞδϡʔϧ
    ͔͠ѻ͑ͳ͔ͬͨ
    ‣ ίϯύΠϥΦϓγϣϯͰϞδϡʔϧλΠϓΛࢦఆ͢Δ
    ͜ͱͰͦΕҎ֎ͷϞδϡʔϧΛ࢖͑ΔΑ͏ʹ
    :foreign-libs [{:file "resources/libs/calculator.js"
    :provides ["calculator"]
    :module-type :commonjs}]

    View full-size slide

  24. ηϧϑϗετ$MPKVSF4DSJQUπʔϧଓʑొ৔
    ‣ $MPKVSF4DSJQU͕ࣗ෼ࣗ਎ΛίϯύΠϧ ηϧϑϗεςΟϯά
    ɹ
    Ͱ͖ΔΑ͏ʹͳͬͨ͜ͱʹΑΓɺ$MPKVSFʹґଘ͠ͳ͍ॲཧܥ
    ͕ग़͖͍ͯͯΔ
    ‣ ࣮ߦʹ+7.͕ඞཁͳ͍ͷͰىಈ͕࣌ؒ୹͘ɺεΫϦϓτ࣮ߦ
    ౳ͷ༻్ʹ޲͘΋ͷ΋͋Δ
    ‣ ॲཧܥͷྫ
    1MBODL$MPKVSF4DSJQUPO+BWB4DSJQU$PSF
    -VNP$MPKVSF4DSJQUPO/PEFKT
    DBMWJO-VNP্Ͱಈ͘$MPKVSF4DSJQU༻Ϗϧυπʔϧ

    View full-size slide

  25. ͕࣌ؒ͋Ε͹
    -VNPʹ͍ͭͯܰ͘σϞ

    View full-size slide

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

    View full-size slide

  27. ·ͱΊ
    ‣ $MPKVSF4DSJQUͷ$MPKVSF΍+4ʹର͢Δ਌࿨ੑ͸ɹ
    ·͢·͢ߴ·͍ͬͯΔ
    ‣ ҰํͰɺηϧϑϗεςΟϯάʹΑΓ$MPKVSF4DSJQU
    ୯ମͰΤίγεςϜΛங͚ΔՄೳੑ·Ͱݟ͖͑ͯͨ
    ‣ $MPKVSF$MPKVSF4DSJQU ʴ+4
    ͷ૊Έ߹Θͤํ͸ɹ
    ࠓޙ͞Βʹଟ༷Խ͍͖ͯͦ͠͏ͳงғؾ

    View full-size slide

  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ͷϏϧυͷղઆهࣄ

    View full-size slide

  29. ࢀߟ
    ‣ 1MBODL
    IUUQTHJUIVCDPNNpLFTQMBODL
    ‣ -VNP
    IUUQTHJUIVCDPNBONPOUFJSPMVNP
    ‣ DBMWJO
    IUUQTHJUIVCDPNFHJOF[DBMWJO

    View full-size slide