-
Notifications
You must be signed in to change notification settings - Fork 60
/
hof.clj
26 lines (21 loc) · 1.02 KB
/
hof.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
(ns joy.hof
"Examples of higher-order and pure functions from chapter 7.")
(def plays [{:band "Burial", :plays 979, :loved 9}
{:band "Eno", :plays 2333, :loved 15}
{:band "Bill Evans", :plays 979, :loved 9}
{:band "Magma", :plays 2665, :loved 31}])
(def sort-by-loved-ratio (partial sort-by #(/ (:plays %) (:loved %))))
(defn columns [column-names]
(fn [row]
(vec (map row column-names))))
(defn keys-apply [f ks m]
"Takes a function, a set of keys, and a map and applies the function
to the map on the given keys. A new map of the results of the function
applied to the keyed entries is returned."
(let [only (select-keys m ks)]
(zipmap (keys only) (map f (vals only)))))
(defn manip-map [f ks m]
"Takes a function, a set of keys, and a map and applies the function
to the map on the given keys. A modified version of the original map
is returned with the results of the function applied to each keyed entry."
(conj m (keys-apply f ks m)))