Transducers 101

Transducers 101

428a9c6e5b151e618d4db288105bc5f3?s=128

Aleš Roubíček

October 15, 2014
Tweet

Transcript

  1. 2.
  2. 3.

    Data-flow combinators ;; nested calls (reduce + (filter odd? (map

    #(+ 2 %) (range 0 10)))) ;; functional composition (def xform (comp (partial filter odd?) (partial map #(+ 2 %)))) (reduce + (xform (range 0 10))) ;; threading macro (defn xform [xs] (->> xs (map #(+ 2 %)) (filter odd?))) (reduce + (xform (range 0 10)))
  3. 5.

    Transducers ;; transducers (def xform (comp (map #(+ 2 %))

    (filter odd?))) (transduce xform + (range 0 10)) (sequence xform (range 0 10)) (into [] xform (range 0 10)) (chan 1 xform) (r/fold + (xform +) (range 0 10))
  4. 6.

    How it works? Unary function that returns transformer • Initialization

    (Nullary step function variation) (f) • Step function (Binary function variation) (f result input) • Early termination (reduced result) • Completion (Unary step function variation) (f result) Polymorphic CollReduce Can have state (volatile)
  5. 7.

    FAQ Are they lazy? Are they eager? Do they run

    in parallel? Are they faster? How they save my time? Are you drunk?
  6. 8.

    Transducers summary Pure recipes (what to do) w/o knowledge how

    to do it Composable Reusable (seqs, channels, [parallel, observables…]) Efficient (no intermediate results) Natural flow Never call transducers directly!