Skip to content

Instantly share code, notes, and snippets.

@sdboyer
Forked from richhickey/transducerfun.clj
Last active July 20, 2020 04:55
Show Gist options
  • Save sdboyer/9fca652f492257f35a41 to your computer and use it in GitHub Desktop.
Save sdboyer/9fca652f492257f35a41 to your computer and use it in GitHub Desktop.
(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(dedupe)
(mapcat range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(mapcat flatten)
(random-sample 1.0)
(take-nth 1)
(keep #(when (odd? %) (* % %)))
(keep-indexed #(when (even? %1) (* %1 %2)))
(replace {2 "two" 6 "six" 18 "eighteen"})
(take 11)
(take-while #(not= 300 %))
(drop 1)
(drop-while string?)
(remove string?)))
(def data (vec (interleave (range 18) (range 20))))
;; lazily transform the data
(sequence xform data)
;; reduce with a transformation (no laziness)
(transduce xform + 0 data)
;;build one collection from a transformation of another, again no laziness
(into [] xform data)
;;create a recipe for a transformation, which can be subsequently sequenced, iterated or reduced
(eduction xform data)
;;transform everything that goes through a channel - same transducer stack!
(let [c (a/chan 1 xform)]
(a/thread (a/onto-chan c data))
(a/<!! (a/into [] c)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment