Skip to content

Instantly share code, notes, and snippets.

@fmnoise
Forked from richhickey/transducerfun.clj
Created October 27, 2017 18:29
Show Gist options
  • Save fmnoise/083297584c5e675116bf4565cab4dd9f to your computer and use it in GitHub Desktop.
Save fmnoise/083297584c5e675116bf4565cab4dd9f to your computer and use it in GitHub Desktop.
(require '[clojure.core.async :as a])
(def xform (comp (map inc)
(filter even?)
(dedupe)
(flatmap range)
(partition-all 3)
(partition-by #(< (apply + %) 7))
(flatmap 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
(iteration 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