This commit is contained in:
Matthew Davidson 2023-01-04 14:52:31 +01:00
parent 064f6405ad
commit eb47539b33
3 changed files with 11 additions and 11 deletions

File diff suppressed because one or more lines are too long

View file

@ -2,12 +2,12 @@
(:require [koan-engine.core :refer :all]))
(defn get-odds-and-evens [coll]
(let [{odds true evens false} (group-by __ coll)]
(let [{odds true evens false} (group-by odd? coll)]
[odds evens]))
(meditations
"To categorize a collection by some function, use group-by."
(= __ (group-by count ["hello" "world" "foo" "bar"]))
(= {3 ["foo" "bar"] 5 ["hello" "world"]} (group-by count ["hello" "world" "foo" "bar"]))
"You can simulate filter + remove in one pass"
(= (get-odds-and-evens [1 2 3 4 5])
@ -15,7 +15,7 @@
[[1 3 5] [2 4]])
"You can also group by a primary key"
(= __
(= {1 [{:id 1 :name "Bob"} {:id 1 :last-name "Smith"}] 2 [{:id 2 :name "Jennifer"}]}
(group-by :id [{:id 1 :name "Bob"}
{:id 2 :name "Jennifer"}
{:id 1 :last-name "Smith"} ]))
@ -23,13 +23,13 @@
"But be careful when you group by a non-required key"
(= {"Bob" [{:name "Bob" :id 1}]
"Jennifer" [{:name "Jennifer" :id 2}]
__ [{:last-name "Smith" :id 1}]}
nil [{:last-name "Smith" :id 1}]}
(group-by :name [{:id 1 :name "Bob"}
{:id 2 :name "Jennifer"}
{:id 1 :last-name "Smith"}]))
"The true power of group-by comes with custom functions"
(= __
(= {:naughty-list [{:name "Jimmy" :bad true} {:name "Joe" :bad true}] :nice-list [{:name "Jane" :bad false}]}
(group-by #(if (:bad %) :naughty-list :nice-list)
[{:name "Jimmy" :bad true}
{:name "Jane" :bad false}

View file

@ -9,8 +9,8 @@
(defmacro infix-concise [form]
`(~(second form) ; Note the syntax-quote (`) and unquote (~) characters!
__
__))
~(first form)
~(nth form 2)))
(defmacro recursive-infix [form]
(cond (not (seq? form))
@ -27,13 +27,13 @@
(meditations
"Macros are like functions created at compile time"
(= __ (hello "Macros!"))
(= "Hello, Macros!" (hello "Macros!"))
"I can haz infix?"
(= __ (infix (9 + 1)))
(= 10 (infix (9 + 1)))
"Remember, these are nothing but code transformations"
(= __ (macroexpand '(infix (9 + 1))))
(= '(+ 9 1) (macroexpand '(infix (9 + 1))))
"You can do better than that - hand crafting FTW!"
(= '(* 10 2) (macroexpand '(infix-concise (10 * 2))))