Created
June 8, 2015 19:13
-
-
Save plexus/8715d201b32453950a0b to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| ;; Anything you type in here will be executed | |
| ;; immediately with the results shown on the | |
| ;; right. | |
| (get {:a 0, :b 1} :c) | |
| (get {:a 0, :b 1} :c "UNICORNS") | |
| (get-in {:a 0 :b {:c "ho hum"}} [:b :c]) | |
| (def nested-maps {:a 0, :b {:c "ho hum" :d "hello" :e ["r2d2" "c3po" "wall-e"]}}) | |
| (get nested-maps :b) | |
| (get (get nested-maps :b) :c) | |
| (get-in nested-maps [:b :e 1]) | |
| (get ["r2d2" "c3po" "wall-e"] 0) | |
| (assoc-in nested-maps [:b :e 2] "robots") | |
| (def nested-maps2 (update-in nested-maps [:b :e 1] #(.toUpperCase %))) | |
| nested-maps | |
| nested-maps2 | |
| ;; [{a: 1, b: 2}, {a: 'foo', b: 'bar'}].pluck(:a, :b) #=> [[1, 2], ['foo', 'bar']] | |
| (def a_and_b (juxt :a :b)) | |
| (a_and_b {:a 2 :b 3 :c 4}) | |
| (map a_and_b [{:a 1, :b 2}, {:a "foo", :b "bar"}]) | |
| ;; vector | |
| ;; - fixed order | |
| ;; - associative (by index) | |
| (get [1 2 3] 1) | |
| ;; map | |
| ;; - associative (by keyword) | |
| (get {:a 3} :a) | |
| ;; list | |
| ;; - fixed order | |
| ;; get doesn't work, not associative | |
| (get '(1 2 2 2 3 4 5 6 7 8 9) 1) | |
| ;; can walk through up to element n, but that's slow | |
| (nth '(1 2 2 3 3) 4) | |
| ;; set | |
| ;; - no duplicates | |
| ;; - associative (by value) | |
| (get #{:a :B :c} :B) | |
| (type '(1 2 3)) | |
| (type []) | |
| (map identity #{"hannah montanna" "miley cyrus" 20 45}) | |
| (quote (map identity #{"hannah montanna" "miley cyrus" 20 45})) | |
| '(map identity #{"hannah montanna" "miley cyrus" 20 45}) | |
| (hash-set 1 2 1 3 1 4 1 5) | |
| (type (hash-set 1 2 1 3 1 4 1 5)) | |
| (type #{1 2 3}) | |
| (set [1 2 1 3 1 4 1 5]) | |
| (type (set [1 2 1 3 1 4 1 5])) | |
| (sorted-set 1 4 5 2 3) | |
| (type (sorted-set 1 2 3)) | |
| 123 | |
| "str" | |
| true | |
| :foo | |
| 'this-is-a-symbol | |
| [] | |
| {} | |
| #{} | |
| '() | |
| (def foo 123) | |
| foo | |
| 'foo | |
| ;; eval | |
| ;; values -> values | |
| ;; =================== | |
| ;; 1. primitive -> primitive | |
| ;; 2. symbol -> looks up the value def'ed for the symbol | |
| ;; 3. list -> depends on the first element in the list | |
| ;; 3a. list starts with the symbol quote -> return rest of the list | |
| (quote (1 2 3)) | |
| ;; shorthand | |
| '(1 2 3) | |
| foo | |
| 'foo | |
| (quote foo) | |
| ;; 3b. list starts with another symbol -> eval each element in the list, apply rest to first | |
| (+ 1 2 3) | |
| + | |
| (apply + '(1 2 3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment