Skip to content

Instantly share code, notes, and snippets.

@kouphax
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kouphax/147f4118e22e795ca4fa to your computer and use it in GitHub Desktop.
Save kouphax/147f4118e22e795ca4fa to your computer and use it in GitHub Desktop.
; DATA TYPES
(class 1)
(class "Hello")
(class 1.0)
(class \H)
(class true)
(class nil)
(class (fn [] 1))
(class 5/3)
(class :test)
(class 'a)
(class '(1 2))
(class [1 2])
(class #{1 2})
(class {:a 1 :b 2})
(= __ true)
(= __ (= 2 2/1))
(= :a (keyword __))
(= __ (== 2.0 2))
(= __ (= "a" :a 'a))
(= __ (= 2 2/1))
(not= __ false)
(= false (not __))
; LISTS, VECTORS & SETS
(list 1 2 3 2 1)
(vector 1 2 3 2 1)
(hash-set 1 2 3 2 1)
'(1 2 3 2 1)
[1 2 3 2 1]
#{1 2 3}
(= __ (count '(42)))
(= __ (conj [1 2] 3))
(= __ (cons 1 [2 3]))
(= __ (first [1 2 3]))
(= __ (last [1 2 3]))
(= __ (rest [1 2 3]))
(= __ (nth [1 2 3] 2))
(= __ (peek [1 2 3]))
(= __ (pop [1 2 3]))
(= __ (rest []))
; MAPS
(hash-map {:a 1})
{ :a 1 }
(hash-map {:a 1 :b})
(= __ (get {:b 2} :b))
(= __ ({:a 1} :a))
(= __ (:a {:a 1}))
(= __ (:b {:a 1}))
(= __ (:b {:a 1} 2))
(= __ (count {:a 1}))
(= __ (:b {:a 1} 2))
(= __ (assoc {:a 1} :b 2))
(= __ (dissoc {:a 1 :b 2} :a))
(= __ (dissoc {:a 1 :b 2} :a :b))
(= __ (contains? {:a nil :b nil} :b))
(= __ (keys {:a 1 :b 2}))
(= __ (vals {:a 1 :b 2}))
; FUNCTIONS
(def sq (fn [a] (* a a)))
(defn sq [a] (* a a))
(def sq #(* % %))
(= __ ((fn [n] (* 5 n)) 2))
(= __ (#(* 15 %) 4))
(= __ (#(+ %1 %2 %3) 4 5 6))
(= __ (#(* 15 %2) 1 2))
(= 9 (((fn [] ___)) 4 5))
; CONDITIONALS
(= __ (if (false? (= 4 5))
:a :b))
(= __ (if (> 4 3) []))
(let [x 5]
(= :your-road
(cond (= x __) :road-not-taken
(= x __) :another-not-taken
:else __)))
(let [choice 5]
(= :your-road (case choice
__ :road-not-taken
__ :your-road
:another-not-taken)))
; LOOPING
(= __ (loop [v 1]
(if-not (> v 5)
(recur (inc v))
v))
; HIGHER ORDER FUNCTIONS
(= [__ __ __] (map #(* 4 %)) [1 2 3]))
(= __ (filter nil? [:a :b nil :c :d]))
(= __ (reduce * [1 2 3 4]))
; LAZY SEQUENCES
(= __ (range 1 5))
(= __ (range 5))
(= [0 1 2 3 4 5] (take __ (range 100)))
(= __ (take 20 (iterate inc 0)))
(= [:a :a :a :a :a :a] (repeat __ __))
; USEFUL MACROS
(= __ (-> "a b c d"
.toUpperCase
(.replace "A" "X")
(.split " ")
first))
(= __ (->> (range)
(filter even?)
(take 10)
(reduce +)))
(= __ (try
(/ 1 0)
true
(catch Exception e
false)))
; ATOMS
(let [my-atom (atom 1)]
(= __ @my-atom)
(swap! my-atom inc)
(= __ @my-atom)
(reset! my-atom 4)
(= __ @my-atom))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment