Address changes to PR adding threading forms
This commit is contained in:
parent
ba141d0fc2
commit
3afe01adc7
3 changed files with 19 additions and 15 deletions
|
|
@ -275,7 +275,8 @@
|
|||
12
|
||||
[1 2 3]]}]
|
||||
|
||||
["26_transducers" {"__" [[2 4]
|
||||
["26_transducers" {"__" ['(2 3 4)
|
||||
[2 4]
|
||||
[2 4]
|
||||
[2 4]
|
||||
6]}]
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
(= __
|
||||
(transduce xfms conj [1 2 3]))
|
||||
(sequence example-transducer [1 2 3]))
|
||||
|
||||
"Consider that sequence operations can be composed as transducers"
|
||||
(= __
|
||||
(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])))
|
||||
|
|
|
|||
Loading…
Reference in a new issue