Last active
December 12, 2016 10:54
-
-
Save ayato-p/bf095837d38aadc5e4f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def user-info ["robert8990" 2011 :name "Bob" :city "Boston"]) | |
;= #'user/user-info | |
(let [[username account-year & extra-info] user-info, | |
{:keys [name city]} (apply hash-map extra-info)] | |
(format "%s is in %s" name city)) | |
;= "Bob is in Boston" | |
;; This example is equal to below. | |
(let [[username account-year & {:keys [name city]}] user-info] | |
(format "%s is in %s" name city)) | |
;= "Bob is in Boston" | |
;; hmmm... | |
;; This one? | |
(let [[username account-year & extra-info] user-info] | |
(println extra-info) | |
(println (type extra-info))) | |
;= (:name Bob :city Boston) | |
;= clojure.lang.PersistentVector$ChunkedSeq | |
;; This one? | |
(let [[username account-year & extra-info] user-info, | |
extra-info-map (apply hash-map extra-info)] | |
(println extra-info-map) | |
(println (type extra-info-map))) | |
;; {:name Bob, :city Boston} | |
;; clojure.lang.PersistentHashMap | |
;; In this case | |
(def user-info ["robert8990" 2011 :name "Bob" :city "Boston"]) | |
;; We can see like below.(look like?) | |
["robert8990", 2011 ,{:name "Bob" :city "Boston"}] | |
[username , account-year ,extra-info] | |
;; We write intuitively | |
(let [[username account-year & {:keys [name city]}] user-info] | |
(format "%s is in %s" name city)) | |
;; Clojure is simple form!>< | |
;; fn or let | |
;; fn use case. for example... | |
(map (fn [x] (* x 2)) | |
[1 2 3]) | |
;= (2 4 6) | |
(reduce (fn [x, y] | |
(+ x y)) | |
0 | |
[1 2 3 4 5]) | |
;= 15 | |
;; let use case. for example... | |
(defn square-adder | |
[x, y] | |
(let [x2 (* x x), y2 (* y y)] ;=> x2 = x*x; y2 = y*y; | |
(+ x2 y2))) | |
(square-adder 10 20) | |
;= 500 | |
;; defn or letfn | |
;; defn use case. for example... | |
(defn hoge [] (print "hogehoge")) | |
(hoge) | |
;= hogehoge | |
;; letfn use case. for example... | |
(letfn [(fuga [] (print "fugafuga"))] | |
(fuga)) | |
;= fugafuga | |
(fuga) | |
;= CompilerException java.lang.RuntimeException: Unable to resolve symbol: fuga in this context, compiling:(NO_SOURCE_PATH:0:0) | |
;; create lexical scope at letfn! | |
;; we can referring function document. | |
(doc doc) | |
(doc hash-map) | |
(doc apply) | |
(doc format) | |
(doc map) | |
(doc reduce) | |
;; by the way | |
;; We can use Macro, it enable to create NEW SYNTAX. | |
;; for example. 'my-if' | |
;; first original 'if' | |
(if true | |
(println "Hello") | |
(println "Hi")) | |
;= Hello | |
;; don't say "Hi". only "Hello". | |
;; normally defn... | |
(defn my-if | |
[test, consequest, alternative] | |
(if test | |
consequest | |
alternative)) | |
;; try to use | |
(def x 10) | |
(my-if (> x 10) "hoge" "fuga") | |
;= fuga | |
;; hmmm... | |
;; but, this case | |
(my-if (> x 10) | |
(println "hoge") | |
(println "fuga")) | |
;= hoge | |
;= fuga | |
;; Oh... I expect only say "fuga". | |
;; Macro use... | |
(defmacro mymacro-if | |
[test, consequest, alternative] | |
(list 'if test | |
consequest | |
alternative)) | |
;; this using | |
(def x 10) | |
(mymacro-if (> x 10) "hoge" "fuga") | |
;= fuga | |
;; this one? | |
(mymacro-if (> x 10) | |
(println "hoge") | |
(println "fuga")) | |
;= fuga | |
;; Yeah!! only say "fuga". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment