--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at http://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.
non-starter for the goals of transducers
Efficiency by removing intermediate allocation.
(defn transduce [xform f init coll] (let [vacc (volatile! init) p (fn ([]) ([x] (reduced? (vswap! vacc f x)))) p (xform p)] (feed! p coll) ; transducers are now process -> process (p) (f (let [acc @vacc] (if (reduced? acc) @acc acc)))))
Where feed!
is the iteration primitive – for exposition it can be implemented using reduce
but it’s backwards: reduce should be implemented on top of feed!
.
(defn feed! "Returns true if the process can't process more input." [p coll] (reduce #(and (p %2) (reduced true)) false coll))
(defn map [f] (fn [p1] (fn ([] (p1)) ([x] (p1 (f x))))))
(transduce (map inc) conj (range 10))
Currently in Clojure 1.7, this code has no side-effects, and no stateful transducers. It's pure functional programming. True this isn't the case for take and partition, but like I said it seems a bit odd to say that since a few things need state, everything has to be side-effecting.