Clean up group-by descriptions & spelling
This commit is contained in:
parent
d6b1b57942
commit
86a6790ae4
@ -216,7 +216,7 @@
|
|||||||
{:last-name "Smith" :id 1}]
|
{:last-name "Smith" :id 1}]
|
||||||
2 [{:name "Mike" :id 2}]}
|
2 [{:name "Mike" :id 2}]}
|
||||||
nil
|
nil
|
||||||
{:nauty-list [{:name "Jimmy" :bad true}
|
{:naughty-list [{:name "Jimmy" :bad true}
|
||||||
{:name "Joe" :bad true}]
|
{:name "Joe" :bad true}]
|
||||||
:nice-list [{:name "Jack" :bad false}]}]}]
|
:nice-list [{:name "Jack" :bad false}]}]}]
|
||||||
]
|
]
|
||||||
|
@ -4,29 +4,32 @@
|
|||||||
|
|
||||||
(meditations
|
(meditations
|
||||||
|
|
||||||
"To split a collection categorized by keys use the group-by function."
|
"To categorize a collection by some function, use group-by."
|
||||||
(= __ (group-by count ["hello" "world" "foo" "bar"]))
|
(= __ (group-by count ["hello" "world" "foo" "bar"]))
|
||||||
|
|
||||||
|
|
||||||
"Sometimes you want to filter a sequence but also keep the predicate."
|
"You can simulate filter + remove in one pass"
|
||||||
(= (get-odds-and-evens '(1 2 3 4 5))
|
(= (get-odds-and-evens [1 2 3 4 5])
|
||||||
[[1 3 5] [2 4]])
|
((partial (juxt filter remove) odd?) [1 2 3 4 5])
|
||||||
|
[[1 3 5] [2 4]])
|
||||||
|
|
||||||
"You can also group by a primary key"
|
"You can also group by a primary key"
|
||||||
(= __
|
(= __
|
||||||
(group-by :id [{:id 1 :name "Bob"}
|
(group-by :id [{:id 1 :name "Bob"}
|
||||||
{:id 2 :name "Mike"}
|
{:id 2 :name "Mike"}
|
||||||
{:id 1 :last-name "Smith"} ]))
|
{:id 1 :last-name "Smith"} ]))
|
||||||
|
|
||||||
"but be careful when you group by non primary key"
|
"But be careful when you group by non-required key"
|
||||||
(= {"Bob" [{:name "Bob" :id 1}], "Mike" [{:name "Mike" :id 2}], __ [{:last-name "Smith" :id 1}]}
|
(= {"Bob" [{:name "Bob" :id 1}]
|
||||||
|
"Mike" [{:name "Mike" :id 2}]
|
||||||
|
__ [{:last-name "Smith" :id 1}]}
|
||||||
(group-by :name [{:id 1 :name "Bob"}
|
(group-by :name [{:id 1 :name "Bob"}
|
||||||
{:id 2 :name "Mike"}
|
{:id 2 :name "Mike"}
|
||||||
{:id 1 :last-name "Smith"}]))
|
{:id 1 :last-name "Smith"}]))
|
||||||
|
|
||||||
"The true power of group-by is using a custom function"
|
"The true power of group-by comes with custom functions"
|
||||||
(= __
|
(= __
|
||||||
(group-by #(if (:bad %) :nauty-list :nice-list)
|
(group-by #(if (:bad %) :naughty-list :nice-list)
|
||||||
[{:name "Jimmy" :bad true}
|
[{:name "Jimmy" :bad true}
|
||||||
{:name "Jack" :bad false}
|
{:name "Jack" :bad false}
|
||||||
{:name "Joe" :bad true}])))
|
{:name "Joe" :bad true}])))
|
||||||
|
Loading…
Reference in New Issue
Block a user