Created
November 24, 2017 17:00
-
-
Save tallus/f85541aa2dbf6b9d874fdb8023a90f43 to your computer and use it in GitHub Desktop.
Cloure Syntax Notes
This file contains 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
;; 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