Uploaded image for project: 'Clojure'
  1. CLJ-1569

transduce does not respect the init arity of transducers

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Minor
    • Resolution: Declined
    • Affects versions: Release 1.7
    • Fix versions: None
    • Labels:

      Description

      Note: I initially raised this issue for discussion on the mailing list
      https://groups.google.com/d/msg/clojure/uVKP4_0KMwQ/-oUJahvUarIJ

      transduce and other transducible processes currently ignore the 'init' arity of transducers. The currently implementation of transduce takes the 'init' from the reducing function before being transformed by the transducer, rather the reducing function after being transformed.

      The current implementation of transduce is equivalent to the following (simplified for exposition purposes):

      Current implementation of transduce
      (defn transduce
        ([xform f coll]
           (transduce xform f (f) coll))
        ([xform f init coll]
           (let [rf (xform f)]
             (rf (reduce rf init coll)))))
      

      The arity 3 case uses (f) to construct the seed value of the reduction. The arity 4 case uses the explicitly provided seed, init.

      I would like to propose an alternate implementation of transduce, one which makes use of the transducer when seeding the reduction.

      Proposed implementation of transduce
      (defn alt-transduce
        ([xform f coll]
           (let [rf (xform f)]
             (rf (reduce rf (rf) coll))))
        ([xform f init coll]
           (let [rf (xform
                     (fn
                       ([] init)
                       ([result] (f result))
                       ([result input] (f result input))))]
             (rf (reduce rf (rf) coll)))))
      

      Now, the arity 3 case uses (xform f) to construct the seed value of the reduction. The arity 4 case combines both f and init into a new reducing function that is given to xform. Both of these ensure that the init arity of the transducer is used.

      As into is implemented in terms of transduce, it is also taken care of. However, sequence is separate, and would also have to be tweaked to respect the init arity.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              alex+import import
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: