Skip to content

Instantly share code, notes, and snippets.

@pjankiewicz
Last active May 8, 2020 02:51
Show Gist options
  • Save pjankiewicz/8f4898034f2f4d7eb3d7 to your computer and use it in GitHub Desktop.
Save pjankiewicz/8f4898034f2f4d7eb3d7 to your computer and use it in GitHub Desktop.
clojure cheat sheet
; this is a comment
; this is a double function
(defn doublen [x] (* x 2))
; basic collections (lists, vectors, maps)
(def a-list '(1 2 3))
(def a-vector [1 2 3])
(def a-map {:key1 val1 :key2 val2})
; other collections
(sorted-map :a 1 :b 2) ; this is a sorted map
; for each type of the collection generic functions
(count coll) ; counts the elements
(conj coll item) ; add something to the collection
; for maps it joins items of 2 maps (overwrites the old one
; if there is a collision)
(seq coll) ; sequence
; conj on list - prepends the element to the front
(conj '(1 2 3) 4) ; (4 1 2 3)
; conj on vector - appends to the end
(conj [1 2 3] 4) ; [1 2 3 4]
; conj on map - joins 2 maps
; other collection functions
(first '(1 2 3)) ; 1
(second '(1 2 3)) ; 2
(rest '(1 2 3)) ; (2 3)
(last '(1 2 3)) ; 3
; peek and pop are like first and rest
; work on lists and vectors differeny
(assoc vector index val) ; associates index with value
(get vector index) ; gets the element at index
(nth vector index) ; like get but returns an exeption
(subvec vector start end) ; subvector - start inclusive, end exclusive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment