Skip to content

Instantly share code, notes, and snippets.

@visibletrap
Created March 29, 2018 16:39
Show Gist options
  • Save visibletrap/f35bb88cf682f95d82896bf040034638 to your computer and use it in GitHub Desktop.
Save visibletrap/f35bb88cf682f95d82896bf040034638 to your computer and use it in GitHub Desktop.
(ns playground.transducer)
(def f1 (comp #(map str %) #(filter even? %) #(map inc %))) ; right -> left
(def nums [4 7 8 9 5 3])
(f1 nums)
(defn f2 [x]
(map str (filter even? (map inc x))))
(f2 nums)
(def x1 (comp (map inc) (filter even?) (map str))) ; left -> right
(sequence x1 nums)
(defn f3 [x]
(#(map str %) (#(filter even? %) (#(map inc %) x))))
(f3 nums)
(defn x2 [x]
((map inc) ((filter even?) ((map str) x))))
(map inc) ; rf -> rf
(defn x2 [rf]
((map inc) ((filter even?) ((map str) rf))))
(sequence x2 nums)
(reduce (x1 conj) [] nums)
(reduce (x2 conj) [] nums)
(transduce x1 conj nums)
(transduce x2 conj nums)
(defn x3 [rf]
(let [str-rf
(fn
([] (rf))
([result] (rf result))
([result input]
(rf result (str input))))
even-rf
(fn
([] (str-rf))
([result] (str-rf result))
([result input]
(if (even? input)
(str-rf result input)
result)))
inc-rf
(fn
([] (even-rf))
([result] (even-rf result))
([result input]
(even-rf result (inc input))))]
inc-rf))
(sequence x3 nums)
(reduce (x3 conj) [] nums)
(transduce x3 conj nums)
(require '[clojure.core.reducers :as r])
(set! *print-length* 10)
(r/fold (x2 conj) (range 10000000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment