Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Transducers
Search
Alex Wheeler
November 15, 2017
0
66
Transducers
Clojure Transducers
Alex Wheeler
November 15, 2017
Tweet
Share
More Decks by Alex Wheeler
See All by Alex Wheeler
Running Rings Around Rack
alexwheeler
0
84
Golang Concurrency
alexwheeler
0
88
Rewriting Rack: A Functional Approach
alexwheeler
2
140
Man Computer Symbiosis
alexwheeler
0
120
Flipper
alexwheeler
0
130
Character Encodings
alexwheeler
0
120
Featured
See All Featured
Designing Experiences People Love
moore
142
24k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
6
680
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3k
KATA
mclloyd
29
14k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.6k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
Done Done
chrislema
184
16k
Gamification - CAS2011
davidbonilla
81
5.3k
Rails Girls Zürich Keynote
gr2m
94
14k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Transcript
Transducers
• recast sequence processing functions (map, filter, etc.) as process
transformations
processes • succession of steps • each step ingests an
input
Reducing functions • whatever, input -> whatever
• sources/sinks are irrelevant/unspecified
• sum, number -> sum • (+ 1 2) =>
3
• person, attribute -> person (into {:name "Alex"} {:company “VTS”})
=> {:name "Alex" :company "VTS"}
• vector, input -> vector • (conj [1] 2) =>
[1, 2]
• transducers are: 1. reusable 2. avoid intermediary collections 3.
composable
reusable • every new collection defines own version of map,
filter, etc. • MyCollection->MyCollection (append list 1) • Channel->Channel (>! chan 1) • Sequence->Sequence (conj [] 1)
intermediate collections (1..10).map { |x| x * 2 } .select
{ |x| x > 20 } .reduce { |sum, x| sum + x }
composable (1..10).transduce(&method(:push))
composable (1..10).transduce(&method(:merge))
• Clojure 101
None
(def name "Alex")
(println "Hello " name) => Hello Alex
(fn [x] (* x x))
(defn square [x] (* x x))
((comp inc inc) 1) (inc (inc 1))
(def plus-two (comp inc inc))
• Transducers
None
(map inc [1 2 3 4 5 6]) => (2
3 4 5 7) (filter even? [1 2 3 4 5 6]) => (2 4 6)
• write map, filter in terms of reduce?
map (reduce (fn [result input] (conj result (inc input))) []
[1 2 3 4])
filter (reduce (fn [result input] (if (even? input) (conj result
input) result)) [] [1 2 3 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])
(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])
map (reduce (fn [result input] (conj result (inc input))) []
[1 2 3 4])
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])
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])
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])
(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])
(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])
None
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])
(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])
(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])
(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])
(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])
(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])
(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])
(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))))
(defn mapping [f] ;; fn to apply to each input
(fn [step] (fn [result input] (step result (f input)))))
(defn mapping [f] (fn [step] ;; reducing fn that moves
reduction forward (fn [result input] (step result (f input)))))
(defn mapping [f] (fn [step] (fn [result input] ;; reducing
function (step result (f input)))))
• mapping/filtering return transducers • Transducers are functions that accept
a reducing (step) function and return a reducing function
• Note to future self: Alex. Do not go to
the next slide yet. Open vim and show the repl examples.
• 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