Created
September 24, 2014 10:35
-
-
Save tzach/77580a802b1142d9a4a4 to your computer and use it in GitHub Desktop.
play around with a trivial trace transducer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; play around with a trivial trace transducer | |
;; base on https://gist.github.com/ptaoussanis/e537bd8ffdc943bbbce7#file-transducers-clj | |
(defn trace-tran | |
"print the new input on any iteration. | |
Combine with other transducers for tracing" | |
[reducing-fn] | |
(fn new-reducing-fn | |
([] (reducing-fn)) | |
([accumulation] (reducing-fn accumulation)) | |
([accumulation new-input] | |
(println (str "new-input: " new-input)) ;; side effect! | |
(reducing-fn accumulation new-input)))) | |
;; combine the new trace-tran with other transducers | |
;; notice the position of the trace-trans in the function composition | |
(def start-trans (comp trace-tran (map #(* % 2)) (map inc))) | |
(def mid-trans (comp (map #(* % 2)) trace-tran (map inc))) | |
(def end-trans (comp (map #(* % 2)) (map inc) trace-tran)) | |
(into [] start-trans (range 5)) | |
(into [] mid-trans (range 5)) | |
(into [] end-trans (range 5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment