Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Transducers 101

Transducers 101

Aleš Roubíček

October 15, 2014
Tweet

More Decks by Aleš Roubíček

Other Decks in Programming

Transcript

  1. 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)))
  2. 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))
  3. 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)
  4. FAQ Are they lazy? Are they eager? Do they run

    in parallel? Are they faster? How they save my time? Are you drunk?
  5. 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!