Created
August 23, 2013 07:50
-
-
Save stephenLee/6316615 to your computer and use it in GitHub Desktop.
Learn Clojure
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
| ; map | |
| (map clojure.string/lower-case ["Java" "Imperative" "Weeping" "Clojure"]) | |
| (map * [1 2 3 4] [5 6 7 8]) | |
| ; reduce | |
| (reduce max [0 -3 10 48]) | |
| (reduce + 50 [1 2 3 4]) | |
| ; partial | |
| (def only-strings (partial filter string?)) | |
| (only-strings ["a" 5 "b" 6]) | |
| ; comp | |
| (def negated-sum-str (comp str - +)) | |
| (negated-sum-str 10 12 3.4) | |
| ; high order function | |
| (defn adder | |
| [n] | |
| (fn [x] (+ n x))) | |
| ((adder 5) 18) | |
| (defn doubler | |
| [f] | |
| (fn [& args] | |
| (* 2 (apply f args)))) | |
| (def double-+ (doubler +)) | |
| (double-+ 1 2 3) | |
| ; pure function | |
| (defn prime? | |
| [n] | |
| (cond | |
| (== 1 n) false | |
| (== 2 n) true | |
| (even? n) false | |
| :else (->> (range 3 (inc (Math/sqrt n)) 2) | |
| (filter #(zero? (rem n %))) | |
| empty?))) | |
| (time (prime? 112345678908769)) | |
| ;; list | |
| '(a b :name 12.5) | |
| ;; vector | |
| ['a 'b :anme 12.5] | |
| ;; map | |
| {:name "Chas" :age 31} | |
| ;; set | |
| #{1 2 3} | |
| ;; operation on vector | |
| (def v [1 2 3]) | |
| (conj v 4) | |
| (conj v 4 5) | |
| (seq v) | |
| ;; operation on map | |
| (def m {:a 5 :b 6}) | |
| (conj m [:c 7]) | |
| (seq m) | |
| ;; operation on set | |
| (def s #{1 2 3}) | |
| (conj s 10) | |
| (conj s 3 4) | |
| (seq s) | |
| ;; operation on list | |
| (def lst '(1 2 3)) | |
| (conj lst 0) | |
| (conj lst 0 4) | |
| (seq lst) | |
| ;; into | |
| (into v [4 5]) | |
| (into [1] {:a 1 :b 2}) | |
| (into #{1 2} [2 3 4 5 2 3]) | |
| ;; collection function | |
| ;; conj, seq, count, empty, = | |
| (take-nth 2 (drop 1 (range 10))) | |
| ;; Sequence | |
| (seq "Clojure") | |
| (seq {:a 5 :b 6}) | |
| (seq (java.util.ArrayList. (range 5))) | |
| (seq (into-array ["Clojure" "Programming"])) | |
| (seq []) | |
| (seq nil) | |
| (map str "Clojure") | |
| (set "Programming") | |
| (first "Clojure") | |
| (rest "Clojure") | |
| (next "Clojure") | |
| ;; construct seq | |
| (cons 0 (range 1 5)) | |
| (cons :a [:b :c :d]) | |
| ;; lazy-seq | |
| (defn random-ints | |
| "Returns a lazy seq of random integers in the range [0,limit]" | |
| [limit] | |
| (lazy-seq | |
| (println "realizing random number") | |
| (cons (rand-int limit) | |
| (random-ints limit)))) | |
| (take 10 (random-ints 50)) | |
| ;;Associative, associate key and value(map) | |
| ;; vector is special map(key is index) | |
| ;; assoc, dissoc, get, contains? | |
| (def m {:a 1 :b 2}) | |
| (get m :b) | |
| (get m :d) | |
| (get m :d "not-found") | |
| (assoc m :d 4) | |
| (dissoc m :b) | |
| (assoc m | |
| :x 4 | |
| :y 5 | |
| :z 6) | |
| (dissoc m :a :c) | |
| (def v [1 2 3]) | |
| (get v 1) | |
| (get v 10) | |
| (get v 10 "not found") | |
| (assoc v | |
| 1 4 | |
| 0 -12) | |
| ;; contains, get, find | |
| (find {:ethel nil} :lucy) | |
| (find {:ethel nil} :ethel) | |
| (sorted-map-by compare :z 5 :x 9 :y 0 :b 2 :a 3 :c 4) | |
| (sorted-map-by (comp - compare) :z 5 :x 9 :y 0 :b 2 :a 3 :c 4) | |
| (defn magnitude | |
| [x] | |
| (-> x Math/log10 Math/floor)) | |
| (magnitude 100) | |
| (magnitude 100000) | |
| ;; collection is function | |
| ([:a :b :c] 2) | |
| ({:a 5 :b 2} :b) | |
| ({:a 5 :b 2} :c 7) | |
| (#{1 2 3} 3) | |
| ;; the key of collection is function | |
| (:b {:a 5 :b 6}) | |
| (:c {:a 5 :b 6} 7) | |
| (:d #{:a :b :c}) | |
| ;; some | |
| (some #{1 3 7} [0 2 4 5 6]) | |
| (some #{1 3 7} [0 2 3 4 5 6]) | |
| '(1 2 (+ 1 2)) | |
| ;; list | |
| (list 1 2 (+ 1 2)) | |
| ;; vector | |
| (vector 1 2 3) | |
| (vec (range 5)) | |
| ;; vector as tuple | |
| (defn euclidian-division | |
| [x y] | |
| [(quot x y) (rem x y)]) | |
| (euclidian-division 42 8) | |
| (hash-set :a :b :c :d) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment