$30 off During Our Annual Pro Sale. View Details »

ClojureScript のある生活

ayato
December 16, 2015

ClojureScript のある生活

ayato

December 16, 2015
Tweet

More Decks by ayato

Other Decks in Programming

Transcript

  1. $MPKVSF4DSJQU ͕ ͋Δੜ׆ !@BZBUP@Q

  2. ࣗݾ঺հ +4Φδαϯ$MPKVSFΦδαϯ +BWB4DSJQUॳ৺ऀͰ͢ ࠓ೥$MPKVSF͔͠ॻ͍ͯͳ͍ਓ $ZCP[V4UBSUVQT *OD

  3. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ ྫ͑͹ΦϯϥΠϯγϣοϓΈ͍ͨͳγεςϜɻ

  4. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ Χʔτͷ߹ܭֹۚΛϑϩϯτΤϯυͱ
 αʔόʔαΠυ྆ํͰಉ͡ܭࢉΛ͢Δඞཁ͕͋Δɻ

  5. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ ͔͠͠ɺֹۚܭࢉͳͷͰͲͪΒʹ΋
 ಉ࣮͡૷͢Δͱ͍͏͜ͱ͸ͨ͘͠ͳ͍ɻ

  6. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ ߹ܭֹۚܭࢉ"1*Λୟ͖·ͬͯ͘
 αʔόʔαΠυͰܭࢉͨ݁͠ՌΛϑϩϯτʹฦ͢

  7. ͜Μͳܦݧ͋Γ·ͤΜ͔ʁ ී௨ʹߟ͑ͯͭΒ͍
 ඇಉظॲཧΛ௚ྻԽͨ͠ΓɺϝϞԽͨ͠Γେม

  8. ͦ͜Ͱ$MPKVSF4DSJQUͰ͢Α 3FBEFS$POEJUJPOBMTͷ͓ӄͰ
 8SJUFPODF SVOBOZXIFSF 5SBOTJUϑΥʔϚοτͰ
 γʔϜϨεͳσʔλ௨৴ ϑϩϯτΤϯυ΋αʔόʔαΠυ΋
 $MPKVSFͷ஌͚ࣝͩͰॻ͚Δ

  9. $MPKVSF4DSJQU "MU+4ͱ͍͏ΑΓ͸$MPKVSFͷλʔήοτ͕
 +7.Ͱ͸ͳ͘+BWB4DSJQUʹͳͬͨ΋ͷ ΄ͱΜͲ$MPKVSFͱಉ͚ͩ͡ͲɺҰ෦ҧ͏
 JOUFSPQͱ͔ϚΫϩ࢖͑Δ͚Ͳॻ͚ͳ͍ͱ͔  ΤίγεςϜ͕ڧ͍ ͱͯ΋  'JHXIFFMͰ8FC1BDL).3ͱಉ౳

    #SPXTFS3&1-Ͱϒϥ΢β؀ڥऔΕΔ
  10. 3FBEFS$POEJUJPOBMT $MPKVSF͔Β࢖͑ΔΑ͏ʹͳͬͨݴޠػೳ DMKD֦ுࢠͰఆ͓ٛͯ͘͠ͱ
 $MPKVSF$MPKVSF4DSJQUͲͪΒ͔Β΋ಡࠐՄೳ ݴޠಛ༗ͷ໰୊͚ͩΛࠩ෼ఆٛग़དྷΔ

  11. ;; dates (.getTime #?(:clj (java.util.Date.) :cljs (js/Date.))) ;; uris #?(

    :clj (defn url-encode [string] (some-> string str (URLEncoder/encode "UTF-8") (.replace "+" "%20"))) :cljs (defn url-encode [string] (some-> string str (js/encodeURIComponent) (.replace "+" "%20")))) ;; reflection warnings #?(:clj (set! *warn-on-reflection* true)) 3FBEFS$POEJUJPOBMT
  12. 5SBOTJU $MPKVSFͰ༗໊ͳ$PHOJUFDUͷ࡞ͬͨ
 ৽͍͠σʔλϑΥʔϚοτ ϓϩάϥϜಉ࢜ͷίϛϡχέʔγϣϯʹ࢖͑Δ Ϣʔβʔఆٛܕ৘ใΛૹड৴͢Δ͜ͱ͕ग़དྷΔ ωετͨͨͩ͠ͷϚοϓ΍഑ྻͱάοόΠ

  13. (ns transit-demo.core (:require [cognitect.transit :as transit]) (:import [java.io ByteArrayInputStream ByteArrayOutputStream]))

    (def custom-writer (transit/write-handler-map {Cart (transit/write-handler (constantly "cart") #(:items %)) Item (transit/write-handler (constantly "item") #(apply vector (vals %)))})) (def out (ByteArrayOutputStream. 4096)) (def writer (transit/writer out :json {:handlers custom-writer})) 5SBOTJU
  14. (defprotocol Calculatable (calculate [x])) (defrecord Cart [items] Calculatable (calculate [self]

    (apply + (map calculate items)))) (defn new-cart [items] (map->Cart {:items items})) (defrecord Item [name price] Calculatable (calculate [self] (* price 1.08))) (defn new-item [name price] (map->Item {:name name, :price price})) 5SBOTJU
  15. (def my-cart-data (new-cart [(new-item "Clojure Programming" 1000) (new-item "Clojure Applied"

    2500)])) (transit/write writer my-cart-data) (.toString out) ;; => "[\"~#cart\",[[\"~#item\",[\"Clojure Programming\",1000]], ;; [\"^1\",[\"Clojure Applied\",2500]]]]" 5SBOTJU
  16. (def custom-reader (transit/read-handler-map {"cart" (transit/read-handler new-cart) "item" (transit/read-handler #(apply new-item

    %))})) (def in (ByteArrayInputStream. (.toByteArray out))) (def reader (transit/reader in :json {:handlers custom-reader})) (def my-cart-data' (transit/read reader)) (calculate my-cart-data') ;; => 3780.0 5SBOTJU
  17. ͭ·Γ 5SBOTJU8SJUFS3FBEFS 5ZQF3FDPSE DMK DMKD DMKT 1SPUPDPM 4FSWFSTJEF $MJFOUTJEF

  18. །Ұͷܽ఺ 3BUJPܕ͕$MPKVSF4DSJQUͰαϙʔτ͞Εͳ͍ ϥΠϒϥϦͰղܾͰ͖ΔͷͰ໰୊ͳ͍

  19. ·ͱΊ 5SBOTJUϑΥʔϚοτͰγʔϜϨεʹ
 σʔλΛड͚౉ͤΔ 3FBEFS$POEJUJPOBMTͰϑϩϯτͱαʔόʔα ΠυͰඞཁͳॲཧΛڞ௨Խग़དྷΔ $MPKVSF࠷ߴʂ

  20. Enjoy Clojure