Skip to content

Instantly share code, notes, and snippets.

@jarppe
Created May 17, 2015 11:08
Show Gist options
  • Save jarppe/257ac92ce789ca7b9528 to your computer and use it in GitHub Desktop.
Save jarppe/257ac92ce789ca7b9528 to your computer and use it in GitHub Desktop.
Map, filter, reduce example, ClojureBridge Helsinki
(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))))
(->> 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