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

Transducers

Alex Wheeler
November 15, 2017
52

 Transducers

Clojure Transducers

Alex Wheeler

November 15, 2017
Tweet

Transcript

  1. reusable • every new collection defines own version of map,

    filter, etc. • MyCollection->MyCollection (append list 1) • Channel->Channel (>! chan 1) • Sequence->Sequence (conj [] 1)
  2. intermediate collections (1..10).map { |x| x * 2 } .select

    { |x| x > 20 } .reduce { |sum, x| sum + x }
  3. (map inc [1 2 3 4 5 6]) => (2

    3 4 5 7) (filter even? [1 2 3 4 5 6]) => (2 4 6)
  4. (reduce (fn [result input] (conj result (inc input))) [] [1

    2 3 4]) (reduce (fn [result input] (if (even? input) (conj result input) result)) [] [1 2 3 4])
  5. (reduce (fn [result input] (conj result (inc input))) [] [1

    2 3 4]) (reduce (fn [result input] (if (even? input) (conj result input) result)) [] [1 2 3 4])
  6. map (reduce (fn [result input] (conj result (inc input))) []

    [1 2 3 4]) (reduce ((fn [f] (fn [result input] (conj result (f input)))) inc) [] [1 2 3 4])
  7. map (reduce (fn [result input] (conj result (inc input))) []

    [1 2 3 4]) (reduce ((fn [f] (fn [result input] (conj result (f input)))) dec) [] [1 2 3 4])
  8. filter (reduce (fn [result input] (if (even? input) (conj result

    input) result)) [] [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) result))) even?) [] [1 2 3 4])
  9. (reduce (fn [result input] (if (even? input) (conj result input)

    result)) [] [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) result))) odd?) [] [1 2 3 4])
  10. (reduce (fn [result input] (if (even? input) (conj result input)

    result)) [] [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) result)))(fn [x] (< x 10))) [] [1 2 3 4])
  11. what’s the problem? (reduce ((fn [f] (fn [result input] (conj

    result (f input))) inc) [] [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) even?) result))) [] [1 2 3 4])
  12. (reduce ((fn [f] (fn [result input] (conj result (f input)))

    inc) [] [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) even?) result))) [] [1 2 3 4])
  13. (reduce ((fn [f] (fn [result input] (conj result (f input)))

    inc) [] [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) even?) result))) [] [1 2 3 4])
  14. (reduce ((fn [f] (fn [result input] (conj result (f input)))

    inc) 0 [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) even?) result))) 0 [1 2 3 4])
  15. (reduce ((fn [f] (fn [result input] (conj result (f input)))

    inc) stream [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) even?) result))) stream [1 2 3 4])
  16. (reduce ((fn [f] (fn [result input] (conj result (f input)))

    inc) chan [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (conj result input) even?) result))) chan [1 2 3 4])
  17. (reduce ((fn [f] (fn [result input] (step result (f input)))

    inc) file [1 2 3 4]) (reduce ((fn [predicate] (fn [result input] (if (predicate input) (step result input) even?) result))) file [1 2 3 4])
  18. (defn mapping [f] (fn [step] (fn [result input] (step result

    (f input))))) (defn filtering [predicate] (fn [step] (fn [result input] (if (predicate input) (step result input) result))))
  19. (defn mapping [f] ;; fn to apply to each input

    (fn [step] (fn [result input] (step result (f input)))))
  20. (defn mapping [f] (fn [step] ;; reducing fn that moves

    reduction forward (fn [result input] (step result (f input)))))
  21. • mapping/filtering return transducers • Transducers are functions that accept

    a reducing (step) function and return a reducing function
  22. • Note to future self: Alex. Do not go to

    the next slide yet. Open vim and show the repl examples.
  23. • map, cat • mapcat, filter • remove, take •

    take-while, take-nth • drop, drop-while • replace, partition-by • partition-all, keep • keep-indexed, map-indexed • distinct, interpose • dedupe random-sample