Created
May 17, 2015 11:08
-
-
Save jarppe/257ac92ce789ca7b9528 to your computer and use it in GitHub Desktop.
Map, filter, reduce example, ClojureBridge Helsinki
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
(def authors [{:first "Margaret" :last "Atwood" :books 15} | |
{:first "Doris" :last "Lessing" :books 67} | |
{:first "Ursula" :last "Le Guin" :books 23} | |
{:first "Alice" :last "Munro" :books 21}]) | |
(defn get-name [author] | |
(get author :first)) | |
(defn first-name-short? [author] | |
(let [first-name (get-name author) | |
name-len (count first-name)] | |
(> 7 name-len))) | |
(defn get-books [author] | |
(get author :books)) | |
(def total-books (reduce + (map get-books (filter first-name-short? authors)))) |
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
(->> authors | |
(filter (comp (partial > 7) count :first)) | |
(map :books) | |
(reduce +)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment