fold vs transients

213 views
Skip to first unread message

Christophe Grand

unread,
Jan 28, 2013, 6:19:27 AM1/28/13
to clojure-dev
Hi,

The fact that when calling (fold combinef reducef coll):
* (combinef) is directly used as the init for the group reduce,
* and the return value of the group reduce is directly passed to combinef.

preclude using fold instead of a reduce which uses transients (eg #'frequencies).

To fix this issue there should be a way to specify a "cleanup" function to call on the return of reduce.
A potential design choice would be to use the 1-arg arity of combine (that would thus acts as the unit function mapping to the monoid domain)

(r/fold
  (fn 
    ([] (transient {}))
    ([tm] (persistent! tm))
    ([a b] (merge-with + a b)))
  (fn [counts x]
    (assoc! counts x (inc (get counts x 0))))
  coll)

This would be a breaking change for current combinef functions whose 1-arg arity is not the identity.

Is there any interest in this line of work? Or what's the suggested work around?

Thanks,

Christophe

--
On Clojure http://clj-me.cgrand.net/
Clojure Programming http://clojurebook.com
Training, Consulting & Contracting http://lambdanext.eu/

Christophe Grand

unread,
Jan 31, 2013, 7:07:10 AM1/31/13
to clojure-dev
Reply all
Reply to author
Forward
0 new messages