Skip to content

Commit

Permalink
Address changes to PR adding threading forms
Browse files Browse the repository at this point in the history
  • Loading branch information
the-frey authored and trptcolin committed Sep 23, 2019
1 parent ba141d0 commit 3afe01a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
3 changes: 2 additions & 1 deletion resources/koans.clj
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,8 @@
12
[1 2 3]]}]

["26_transducers" {"__" [[2 4]
["26_transducers" {"__" ['(2 3 4)
[2 4]
[2 4]
[2 4]
6]}]
Expand Down
10 changes: 3 additions & 7 deletions src/koans/25_threading_macros.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,11 @@
(def a-list-with-maps
'({:a 1} {:a 2} {:a 3}))

(defn function-that-takes-a-map [m a b]
(do
(println (str "Other unused arguments: " a " " b))
(get m :a)))
(defn function-that-takes-a-map [map a b]
(get map :a))

(defn function-that-takes-a-coll [a b coll]
(do
(println (str "Other unused arguments: " a " " b))
(map :a coll)))
(map :a coll))

(meditations
"We can use thread first for more readable sequential operations"
Expand Down
21 changes: 14 additions & 7 deletions src/koans/26_transducers.clj
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
(ns koans.26-transducers
(:require [koan-engine.core :refer :all]))

(def xfms
(def example-transducer
(map inc))

(def transforms
(comp (map inc)
(filter even?)))
(filter even?)))

(meditations
"Consider that sequence operations can be used as transducers"
"A sequence operation with only one argument often returns a transducer"
(= __
(sequence example-transducer [1 2 3]))

"Consider that sequence operations can be composed as transducers"
(= __
(transduce xfms conj [1 2 3]))
(transduce transforms conj [1 2 3]))

"We can do this eagerly"
(= __
(into [] xfms [1 2 3]))
(into [] transforms [1 2 3]))

"Or lazily"
(= __
(sequence xfms [1 2 3]))
(sequence transforms [1 2 3]))

"The transduce function can combine mapping and reduction"
(= __
(transduce xfms + [1 2 3])))
(transduce transforms + [1 2 3])))

0 comments on commit 3afe01a

Please sign in to comment.