Skip to content

Instantly share code, notes, and snippets.

@tallus

tallus/syntax.clj

Created Nov 24, 2017
Embed
What would you like to do?
Cloure Syntax Notes
;; this is a comment
;; Never use single quotes allways use double quotes
;; % is like an anonymous function argumenrt
;; assignment
;; globally scoped to name-space
(def foo 1)
(== foo 1) ;; true
;; functions
(def hello-world (fn [] println "Hello world")) ;; anonymous func
(defn hello-world-again [] (println "Hello world")) ;; equivalent named
(def hello-world-mum #(println "Hello world")) ;; equivalent anonymous
;; like lambda
;; WARNING regex syntax is #String
(hello-world) ;; prints Hello world
;; p.s. functions are also known as closures () is a list and also a closure
;; closures explictly return the last thing/end state
(defn implicit-return [] 1) ;; returns 1
;; assignment again
;; locally scoped with closure (so this should be in a function)
(let [foo 1])
(== foo 1) ;; true
;; combining the two
;; Sequences
;; both of these are like pythons list
'(1 2 3) ;; unevaluted list
[1 2 3] ;; vector
#{1 2 3} ;; set, like pythons set
(= '(1 2 3) [1 2 3]) ;; true
(= #{1 2 3} [1 2 3]) ;; false
(def my-vector [1 2 3])
(def foo (nth my-vector 1)) ;; access the second element of my-vector
(== foo 2) ;; true
;; Hash Maps (like dicts)
{"foo" 1 "bar" 2}
{:foo 1 :bar 1} ;; N.B. :foo != "foo"
(= {"foo" 1 "bar" 2} {:foo 1 :bar 2})
;; access
(def my-map {:foo 1 :bar 2})
(def baz (my-map :foo)) ;; assigns baz to the value of :foo from my-map
(== 1 baz) ;; true
;; TODO lazy sequences
;; The Good Stuff - threading macros
;; thread first
;; TODO make working examples
;; (-> a b c) ;; do b to a then do c to the result
;; technically a is passed as the first argument to b
;; hence used on scalars (single things like a string))
;; (->> a b c) ;; a is passed as the last argument to b
;; used for things like sequences maps
;; namespace
;; TODO
;; filter map inc apply sum identity
;; namespace/imports
;; slices ie. 1 to nth
(def a (map #(+ 1 %) [1 2 3])) ;; here we adding 1 to every member
;; % is being substituted for the members
(= a [2 3 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.