Skip to content

Instantly share code, notes, and snippets.

@kouddy
Last active September 17, 2015 00:53
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 kouddy/55028dbb669bec7b8394 to your computer and use it in GitHub Desktop.
Save kouddy/55028dbb669bec7b8394 to your computer and use it in GitHub Desktop.
Clojure Syntax
; Syntax
(if (= 0 0) "a" "b") ;"a"
(do (print "a" "b")) ;a"b"
(when true "a") ;"a"
(def a "a") ;"a"
(def b ["a" "b"]) ;["a" "b"]
;Data Structures
(nil? 1) ;false
(nil? nil) ;true
(str "a" "b") ;"ab"
;map
{} ;emptry map
{"str" "a"} ;a map with k = "str", v = "a"
{"str" +} ;a map with k = "str", v = + (a function)
{"a" 1,
"b" {"c" a}} ;a nested map
(get {"a" 1, "b" 2} "b") ;2
(get {:a 1, :b {:c 3}} :b) ;{:c 3}
(get-in {:a 1, :b {:c 3}} [:b :c]) ;3
;keywords
;:a is a keyword
(:a {:a 1, :b 2, :c 3}) ;1, this is a key look up
({:a 1} :a) ;1, this is a key look up as well
(:b (:a {:a {:b 1}})) ;1, nested lookup
({:a 1, :b 2} :c 0) ;0, default value for :c is 0 if not count in map. This is useful when you have some optional parameters.
(conj {:a 1} {:b 2}) ;{:a 1, :b 2}
(conj {:a 1} [:b 2]) ;{:a 1, :b 2}
;vectors
[1 2 3]
[1, 2, 3] ;this works too
(get [3 2 1] 0) ;3, index lookup
(conj [1 2 3] 4) ;[1 2 3 4], add elem to vector
(rest [1 2]) ;[2]
;list
'(1 2) ;(1 2)
'(1, 2) ;this works too
(conj nil 1) ;(1)
(conj '(1 2) 3) ;(3 2 1), conj for list add elements to beginning
(first '(1 2)) ;1
(last '(1 2)) ;2
(rest '(1 2)) ;(2)
;set
#{:a :b} ;#{:b :a}, set doesn't guarantee order
#{:a, :b} ;this works too
(conj #{1 2} 3) ;#{3 1 2}, conj for set will add element if it is not already there.
;Functions
((and (= 1 1) +) 1 2 3) ;6
((or + 1) 1 2 3) ;6
(map inc [1 2 3]) ;[2 3 4]
;Calling functions with threading
(-> [2 1 4 5] reverse rest sort) ;(1 2 4)
(-> {:a 1 :b {:c 2}} :b :c) ;1
(->> [1 2 3] (take 2) (map inc)) ;(2 3), using -> will fail
;Returning a function (with clojure, uses Java's Math/pow)
(fn [n] (fn [x] (Math/pow x n)))
;special forms
(if ...) ;you can't pass them as functions
(let [x 1, y 2] (+ x y))
(let [[x], [0]] x) ;0 -- This is more advanced form of let. First array binds to second array
(let [[a b], [1 2]] (+ a b)) ;3 -- Same as above, this is more advanced form of let.
(let [[[a c] b] [[+ 1] 2]] (a c b)) ;3 -- They have same form.
;Define functions
(defn no-params [] 1)
(no-params) ;1
(defn one-param [x] (+ 1 x))
(one-param 1) ;2
(defn two-params [x, y] (+ x y))
(two-params 1 2) ;3
(defn multi-arity
([x, y] (+ x y))
([x] (+ x 1)))
(multi-arity 1 2) ;3
(multi-arity 1) ;2
(fn [x] (+ 1 x)) ;lambda
#(+ % 5) ;lambda
(let [[a b c d e f g] [1 2 3]] [a b c d]) ;[1 2 3 nil], this is a form of destructuring. See https://gist.github.com/john2x/e1dca953548bfdfb9844
;map
(map inc [1 2 3]) ;(2 3 4)
(map #(+ % 1) [1 2 3]) ;(2 3 4)
;filter
(filter #(> % 3) [2 3 4 5]) ;(4 5)
;reduce
(reduce + []) ;0
(reduce + [1 2 3]) ;6
(reduce + 1 [1 2 3]) ;7, with seed value.
;apply
(apply + [1 2 3]) ;6 basically this is treated as (+ 1 2 3).
(apply + '(1 2 3)) ;6, same as above
(apply str ["a", "b", "c"]) ;"abc"
(apply map list '((1 2) (3 4) (5 6))) ;((1 3 5) (2 4 6)), basically treated as (map list '(1 2) '(3 4) '(5 6))
;Higher order function/closure
(fn [n] (fn [x] (Math/pow x n))) ;Return function to compute f(x) = x^n
(fn [f]
(fn [vec]
(->> vec
(sort-by f)
(partition-by f)
(map (fn [x] (hash-map (f (first x)) x)))
(apply merge)))) ;Return function to group by vec by f
(conj nil 1) ; '(1)
(conj nil 1 2) ; '(2 1)
(conj 1 2) ;will throw error
(seq [1 2]) ;'(1 2)
(seq 1) ;will throw error
(list 1 2) ;'(1 2)
(list nil) ;'(nil)
(list) ;'()
(= (list 1 2) (conj nil 1 2)) ;true, these are equivalent.
;lazy seq
(take 5 ((fn my-f [n, result]
(cons (* result n)
(lazy-seq (my-f (inc n) (* result n))))) 1 1)) ;'(1 2 6 24 120), lazily generating factorials
(conj (take (- 10 2)
((fn my-fib [a b]
(cons (+ a b)
(lazy-seq (my-fib b (+ a b))))) 1 1)) 1 1) ; generates first 10 fib numbers
(str 123) ;"123"
(read-string "123") ;123
(seq (str 123) ;'(\1 \2 \3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment