Skip to content

Instantly share code, notes, and snippets.

@tzach
Created September 24, 2014 10:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tzach/77580a802b1142d9a4a4 to your computer and use it in GitHub Desktop.
Save tzach/77580a802b1142d9a4a4 to your computer and use it in GitHub Desktop.
play around with a trivial trace transducer
;; 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