Re: butlast with reducers

2013-08-23 Thread Jozef Wagner
To continue on my previous post, here is the implementation of map which returns both reducible and (lazy)seqable collection: (defn my-map [f c] (let [coll (clojure.core.reducers/map f c) d (delay (clojure.core/map f c))] (reify clojure.core.protocols/CollReduce

Re: butlast with reducers

2013-08-23 Thread Jozef Wagner
To continue on my previous email, here is the implementation of map which returns both reducible and (lazy) seqable object: (defn my-map [f c] (let [coll (clojure.core.reducers/map f c) d (delay (clojure.core/map f c))] (reify clojure.core.protocols/CollReduce

Re: butlast with reducers

2013-08-23 Thread Jozef Wagner
To continue my previous email, here is the implementation of map which is both reducible and (lazy) seqable: (defn my-map [f c] (let [coll (clojure.core.reducers/map f c) d (delay (clojure.core/map f c))] (reify clojure.core.protocols/CollReduce (coll-redu

Re: butlast with reducers

2013-08-09 Thread Christophe Grand
Gaving slept on it I realize that by adding before popping I allowed the buffer to grow to n+1 thus causing a realloc. (defn drop-last [n coll] (reducer coll (fn [f1] (let [buffer (java.util.ArrayDeque. (int n))] (fn self ([] (f1)) ([ret x] (let

Re: butlast with reducers

2013-08-08 Thread Jozef Wagner
Wow, thank you very much! A perfect solution. At the end, wouldn't be good if the reducers would implement alongside CollReduce also a Seqable interface, so that reducers could be used as a drop in replacement for today's sequence functions (map, filter, ...)? CollReduce implements 'eager' comp

Re: butlast with reducers

2013-08-08 Thread Christophe Grand
ArrayDeque based versions: (defn drop-last [n coll] (reducer coll (fn [f1] (let [buffer (java.util.ArrayDeque. (int n))] (fn self ([] (f1)) ([ret x] (.add buffer x) (if (<= (count buffer) n) ret (f1 ret (.p

Re: butlast with reducers

2013-08-08 Thread Christophe Grand
You need to use a buffer to defer calls to the reduced function (defn drop-last [n coll] (reducer coll (fn [f1] (let [buffer (atom clojure.lang.PersistentQueue/EMPTY)] (fn self ([] (f1)) ([ret x] (let [b (swap! buffer conj x)]

butlast with reducers

2013-08-08 Thread Jozef Wagner
Is it possible to implement efficient butlast (and drop-last, take-last) with reducers? The only solution I can think of needs additional reduce to compute count, which may often be undesirable. Or is it OK to say that reducers are not designed for such cases? JW -- -- You received this mes