diff --git a/resources/koans.clj b/resources/koans.clj index 90592db..3bbfd9c 100644 --- a/resources/koans.clj +++ b/resources/koans.clj @@ -275,7 +275,8 @@ 12 [1 2 3]]}] - ["26_transducers" {"__" [[2 4] + ["26_transducers" {"__" ['(2 3 4) + [2 4] [2 4] [2 4] 6]}] diff --git a/src/koans/25_threading_macros.clj b/src/koans/25_threading_macros.clj index 21fd58b..ea7c521 100644 --- a/src/koans/25_threading_macros.clj +++ b/src/koans/25_threading_macros.clj @@ -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" diff --git a/src/koans/26_transducers.clj b/src/koans/26_transducers.clj index 9772deb..8174951 100644 --- a/src/koans/26_transducers.clj +++ b/src/koans/26_transducers.clj @@ -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])))