Thank you for another great technique and concept!.

I've been using reducers a lot lately and I've found that it is in some 
cases better (performance, fold support) to wrap local state in the 
intermediate result, rather than to keep it in an atom. See this *very* 
simplified example for 
comparison https://gist.github.com/wagjo/30000277f6923ac8d88f  

;; state is kept in a ret
(deftype WrapState [ret n])
 
(deftype WrapTake [coll n]
  CollReduce
  (coll-reduce [this f init]
    (let [init-state (->WrapState init n)
          tf (fn [^WrapState state val]
               (let [ret (.-ret state)
                     ni (dec (.-n state))
                     nret (f ret val)]
                 (cond (reduced? nret)
                       (reduced (->WrapState @nret nil))
                       (zero? ni)
                       (reduced (->WrapState nret nil))
                       :else (->WrapState nret ni))))
          ret-state (coll-reduce coll tf init-state)]
      (.-ret ^WrapState ret-state))))

;; state is kept in atom
(deftype AtomTake [coll n]
  CollReduce
  (coll-reduce [this f init]
    (let [na (atom n)
          tf (fn [ret val]
               (swap! na dec)
               (let [ni @na
                     nret (f ret val)]
                 (if (zero? ni) 
                   (reduced nret)
                   nret)))]
      (coll-reduce coll tf init))))


>From what I've understood from the transducers impl, it would no longer be 
able to do such ret wrapping, am I right?

Best,
Jozef


On Wednesday, August 6, 2014 8:01:24 PM UTC+2, Rich Hickey wrote:
>
> I pushed today the initial work on transducers. I describe transducers 
> briefly in this blog post: 
>
> http://blog.cognitect.com/blog/2014/8/6/transducers-are-coming 
>
> This work builds on the work done for reducers, bringing 
> context-independent mapping, filtering etc to other areas, such as 
> core.async. 
>
> This is work in progress. We will be cutting alpha releases to help make 
> it easier to start using core's transducers together with core.async's new 
> support for them. 
>
> I am very excited about this powerful technique and how we all might use 
> it. 
>
> Please have a look. 
>
> Feedback welcome, 
>
> Rich 
>
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to