Last active
August 29, 2015 14:27
-
-
Save NeedMoreDesu/027bf7f016b00b018745 to your computer and use it in GitHub Desktop.
This file contains 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
;;; code as data | |
(+ 1 2 3 (* 2 2)) | |
;; => 10 | |
(def var1 (+ 1 2 3 (* 2 2))) ; ? (def symbol doc-string? init?) | |
;; => #'cljtest.core/var1 | |
var1 | |
;; => 10 | |
(def var2 '(+ 1 2 3 (* 2 2))) | |
;; => #'cljtest.core/var2 | |
var2 | |
;; => (+ 1 2 3 (* 2 2)) | |
'var2 | |
;; => var2 | |
(quote (+ 1 2 3 (* 2 2))) | |
;; => (+ 1 2 3 (* 2 2)) | |
(nth var2 0) | |
;; => + | |
(nth var2 4) | |
;; => (* 2 2) | |
(eval (quote (+ 1 2 3 (* 2 2)))) | |
;; => 10 | |
'(1 2 var1) | |
;; => (1 2 var1) | |
`(1 ~2 ~var1) | |
;; => (1 2 10) | |
`(1 ~2 ~var2) | |
;; => (1 2 (+ 1 2 3 (* 2 2))) | |
`(1 ~2 ~@var2) | |
;; => (1 2 + 1 2 3 (* 2 2)) | |
`(~@[1 2 3] ~@[4 5 6]) | |
;; => (1 2 3 4 5 6) | |
;;; persistent data types | |
;;; lists | |
(def list1 (list 1 2 3)) | |
;; => #'cljtest.core/list1 | |
(def list2 (rest list1)) | |
;; => #'cljtest.core/list2 | |
(def list3 (cons 42 list2)) | |
;; => #'cljtest.core/list3 | |
list1 | |
;; => (1 2 3) | |
list2 | |
;; => (2 3) | |
list3 | |
;; => (42 2 3) | |
;;; vectors | |
(def vec1 [1 2 3 4]) | |
;; => #'cljtest.core/vec1 | |
(def vec2 (assoc vec1 0 90)) | |
;; => #'cljtest.core/vec2 | |
(def vec3 (assoc vec1 6 90)) | |
;; CompilerException java.lang.IndexOutOfBoundsException, compiling:(form-init588753203903906575.clj:1:11) | |
(count vec1) | |
;; => 4 | |
(def vec3 (assoc vec1 (count vec1) :brand-new-last-element)) | |
;; => #'cljtest.core/vec3 | |
(def vec4 (conj vec3 :even-newer-element)) | |
;; => #'cljtest.core/vec4 | |
vec1 | |
;; => [1 2 3 4] | |
vec2 | |
;; => [90 2 3 4] | |
vec3 | |
;; => [1 2 3 4 :brand-new-last-element] | |
vec4 | |
;; => [1 2 3 4 :brand-new-last-element :even-newer-element] | |
(nth vec4 4) | |
;; => :brand-new-last-element | |
(get vec4 4) | |
;; => :brand-new-last-element | |
;;; maps | |
(def map1 {:a 3 :b 5}) | |
;; => #'cljtest.core/map1 | |
(def map2 (assoc map1 :c 6)) | |
;; => #'cljtest.core/map2 | |
map1 | |
;; => {:a 3, :b 5} | |
map2 | |
;; => {:a 3, :b 5, :c 6} | |
(:a map1) | |
;; => 3 | |
(:c map1) | |
;; => nil | |
(get map1 :a) | |
;; => 3 | |
;;; Why persistent? | |
;; C code | |
;; #include <iostream> | |
;; #include <vector> | |
;; using namespace std; | |
;; int v1[3] = {1, 2, 3}; | |
;; int sum() { | |
;; int sum = 0; | |
;; for (int i = 0; i < 3; i++) { | |
;; sum = sum + v1[i]; | |
;; } | |
;; return sum; | |
;; } | |
;; void add_impurity() { | |
;; for(int i = 0; i < 3; i++) { | |
;; v1[i] = v1[i] + 100; | |
;; } | |
;; } | |
;; int main() { | |
;; cout << sum() << endl; // 6 | |
;; add_impurity(); | |
;; cout << sum() << endl; // 306 | |
;; return 0; | |
;; } | |
(def v1 [1 2 3]) | |
;; => #'cljtest.core/v1 | |
v1 | |
;; => [1 2 3] | |
(defn sum [] | |
(reduce + v1)) | |
;; => #'cljtest.core/sum | |
(sum) | |
;; => 6 | |
(defn try-to-add-impurity [] | |
(for [i (range 0 3)] | |
(assoc v1 i 100))) | |
;; => #'cljtest.core/try-to-add-impurity | |
(try-to-add-impurity) | |
;; => ([100 2 3] [1 100 3] [1 2 100]) | |
(sum) | |
;; => 6 | |
;;; macro example (from clojure.core) | |
(defmacro and | |
"Evaluates exprs one at a time, from left to right. If a form | |
returns logical false (nil or false), and returns that value and | |
doesn't evaluate any of the other expressions, otherwise it returns | |
the value of the last expr. (and) returns true." | |
{:added "1.0"} | |
([] true) | |
([x] x) | |
([x & next] | |
`(let [and# ~x] | |
(if and# (and ~@next) and#)))) | |
(defmacro or | |
"Evaluates exprs one at a time, from left to right. If a form | |
returns a logical true value, or returns that value and doesn't | |
evaluate any of the other expressions, otherwise it returns the | |
value of the last expression. (or) returns nil." | |
{:added "1.0"} | |
([] nil) | |
([x] x) | |
([x & next] | |
`(let [or# ~x] | |
(if or# or# (or ~@next))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment