Last active
February 14, 2019 10:22
-
-
Save raymcdermott/4c22db19d21e8e166b69b1b1472e8fbd 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
;; Simplest use cases | |
;; Transform simple input | |
(map inc [1 2 3 4 5]) | |
(map str [1 2 3 4 5]) | |
(map clojure.string/lower-case ["COBOL" "Ada" "C"]) | |
;; Extract simple input | |
(map :rating [{:frozen-product :ice-cream :flavour "Vanilla" :rating 4.5 } | |
{:frozen-product :ice-cream :flavour "Chocolate" :rating 3.5} | |
{:frozen-product :yoghurt :flavour "Cherry" :rating 4.0}]) | |
;; > 1 collection - shortest 'wins' | |
(map str ["COBOL" "Ada" "C"] (range)) | |
(map str ["COBOL" "Ada" "C"] (range 1)) | |
;; reader # functions | |
(map #(str "Shiny new code from " %) ["Rust" "Reason" "Clojure"]) | |
;; partial functions | |
(map (partial str "Old code from ") ["COBOL" "Ada" "C"]) | |
;; use apply (in and out) | |
(map #(apply max %) [[1 2 3] | |
[4 5 6] | |
[7 8 9]]) | |
(apply map vector [[:a :b :c] | |
[:d :e :f] | |
[:g :h :i]]) | |
;; transducer version | |
;; Used without a collection, map will create a transducer: | |
(def xf (map inc)) | |
;; We can now apply this transducer to a sequence (handy when testing core.async) | |
(transduce xf conj (range 5)) | |
;; threading example | |
(->> [{:frozen-product :ice-cream :flavour "Vanilla" :rating 4.5} | |
{:frozen-product :ice-cream :flavour "Chocolate" :rating 3.5} | |
{:frozen-product :yoghurt :flavour "Cherry" :rating 4.0}] | |
(map :frozen-product) | |
distinct) | |
;; oddities / misc | |
(clojure.set/map-invert {:a 1, :b 2}) | |
;; lazy / eager | |
;; examples TBD | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment