Transducers From The Ground Up
source link: https://bsless.github.io/transducers-intro/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Remember this book?
If you've been hanging long enough around programmers, you've probably met one who swears up and down by it. "It changed my life, dude!".
I can't very well argue with that because it changed mine, too, but lest I get biographical, let's have a crack at one of my favorite exercises in the book: implementing reduce
.
We'll only implement foldl here. Skip to thefor an implementation of foldr if you're interested.
(defn -reduce [f init coll] (if-let [s (seq coll)] (recur f (f init (first s)) (rest s)) init)) (-reduce conj [] [1 2 3]) ;; => [1 2 3] (-reduce + 0 [1 2 3]) ;; => 6
In general, the function reduce
takes has the following signature:
(fn rf [accum x] ...) ;; returns new accum
We dub it rf
for reducing function.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK