Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Clojure transducers eager?

In this blog entry, "CSP and transducers in JavaScript", the author states:

First, we have to realise that many array (or other collection) operations like map, filter and reverse can be defined in terms of a reduce.

So then we see a number of implementations of this in Clojure aren't lazy, they are eager:

 user> (defn eager-map [f coll]
        (reduce (fn [acc v] (conj acc (f v)))
        []
        coll))
#'user/eager-map
user> (eager-map inc (range 10))
[1 2 3 4 5 6 7 8 9 10]

My question is, are Clojure transducers eager?

like image 403
hawkeye Avatar asked Sep 04 '14 01:09

hawkeye


1 Answers

Transducers are very simple functions - they don't have a notion of laziness or, in fact, how they're applied at all. That's the beauty of the idea - with transducers, we can separate functions like map and filter from the things on which they operate.

So, yes, they can be used to build lazy sequences, as well as channels and reductions. While the transducer function call itself is eager, it's up to whatever thing you hand the transducer to to call it. Lazy sequences can lazily invoke transducers only as they're consumed while reducers will use them eagerly to spit out the reduction.

You can see in the source where sequence is used to build a lazy sequence over a collection with a transducer.

like image 194
Beyamor Avatar answered Oct 23 '22 02:10

Beyamor