Should the completion arity of a stateful transducer reset state?

50 views
Skip to first unread message

David Bürgin

unread,
Jul 22, 2017, 3:43:23 PM7/22/17
to clo...@googlegroups.com
Hello all,

I found an edge case where stateful transducers in core differ in
whether they reset or clear state in the completion arity
(partition-all) or don’t (take).

Given the two transformed reducing functions

(def conj-partitioning-all-3 ((partition-all 3) conj))
(def conj-taking-3 ((take 3) conj))

when performing transduction with those reducing functions twice in a
row, one yields the same result twice, the other doesn’t.

(transduce identity conj-partitioning-all-3 [] (range 10))
;; => [[0 1 2] [3 4 5] [6 7 8] [9]]
;; => [[0 1 2] [3 4 5] [6 7 8] [9]]

(transduce identity conj-taking-3 [] (range 10))
;; => [0 1 2]
;; => []

Question: which behaviour is the one I should emulate in my own
transducers?

Thank you,
David

Timothy Baldridge

unread,
Jul 22, 2017, 4:02:28 PM7/22/17
to clo...@googlegroups.com
Once a transducer is completed it should never be called again. This is why transduce takes both a transducer and a reducing function and combines them internally. The thought here is that it will be harder to shoot yourself in the foot by reusing a stateful reducing function if you don't have to combine the xf with the rf manually. 

Timothy

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
“One of the main causes of the fall of the Roman Empire was that–lacking zero–they had no way to indicate successful termination of their C programs.”
(Robert Firth)

David Bürgin

unread,
Jul 22, 2017, 5:03:49 PM7/22/17
to clo...@googlegroups.com
On 22/07/17 22:02, Timothy Baldridge wrote:
> Once a transducer is completed it should never be called again.
Thank you, Timothy, this was probably obvious but I don't remember
reading it anywhere so this really helped.

David
Reply all
Reply to author
Forward
0 new messages