Welcome! Please see the About page for a little more info on how this works.

0 votes
in Clojure by

This is a real corner case, so probably not worth much attention. I was experimenting with writing a version of tree-seq using reduction rather than sequences. Here's what I came up with:

`
(defn tree-producer [branch? children node]
(let [recurse #(tree-producer branch? children %)]

(reify clojure.lang.IReduceInit
  (reduce [this rf rval]
    (let [rval (rf rval node)]
      (cond
        (reduced? rval) @rval
        (branch? node) (transduce (mapcat recurse) rf rval (children node))
        :else rval))))))

`

However, using it resulted in an exception:

user=> (into [] (tree-producer seq? identity '((1 2 (3)) (4)))) Execution error (ArityException) at temp$tree_producer$reify__7693/reduce (temp.clj:129). Wrong number of args (1) passed to: clojure.core/preserving-reduced/fn--8743

The exception is not thrown if preserving-reduced is given a 1 argument arity, ie:

`
(defn ^:private preserving-reduced
[rf]
(fn ( [rval] rval)

  ( [rval x]
    (let [ret (rf rval x)]
      (if (reduced? ret)
        (reduced ret)
        ret)))))

`

1 Answer

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-2489 (reported by glchapman)
...