Hi,

I was trying to write a transducer and the 0-arity part of it never got 
called, which was unexpected. I did some searching and found this post: 
https://groups.google.com/forum/#!msg/clojure/uVKP4_0KMwQ/-oUJahvUarIJ. 
What Dan is proposing in that post would essentially solve my problem, but 
it doesn't look like his proposal has gotten much traction...

Specifically I was trying to implement scan 
<http://reactivex.io/documentation/operators/scan.html>.

(defn scan
  ([f] (scan f (f)))
  ([f init]
   (fn [xf]
     (let [state (volatile! init)]
       (fn
         ([] (xf (xf) init))
         ([result] (xf result))
         ([result input]
          (let [next-state (f @state input)]
            (vreset! state next-state)
            (xf result next-state))))))))

Which results in the following:
(require '[clojure.core.reducers :as r])
(r/reduce ((scan + 3) conj) [1 2 3])
=> [3 4 6 9]
(transduce (scan + 3) conj [1 2 3])
=> [4 6 9]
(transduce (scan + 3) conj (((scan + 3) conj)) [1 2 3])
=> [3 4 6 9]

My expectation would be that we'd always get the 3 at the front of the 
vector.

I'm actually using core.async and I'm expecting that the initial value be 
available to be taken from the channel.
(require '[clojure.core.async :as a :include-macros true])
(def c (a/chan 1 (scan + 3)))
(a/go (println (a/<! c)))
; expecting 3 to immediately be printed.
(a/>!! c 1)
=> 4

So this is more of a conceptual thing rather than just how transduce is 
implemented.

I'd love to hear other people's thoughts on this. I'm quite new, but Dan's 
proposal definitely feels "correct" and the current implementation 
definitely feels "wrong".

--Patrick


-- 
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