@seancorfield mentioned the other day that the doc-string for completing
is incorrect:
"Takes a reducing function f of 2 args and returns a fn suitable for
transduce by adding an arity-1 signature that calls cf (default -
identity) on the result argument."
The incorrectness lies in that the function returned by completing
can invoke f
as a zero-arity fn. Is this worth a issue/patch?
user> (def f (completing (fn needs-completness [x y] 'yay)))
;; => #'user/f
user> (f)
Execution error (ArityException) at user/eval19641550 (form-init1830575926447082095.clj:2408).
Wrong number of args (0) passed to: user/needs-completness--19641548
user>
If coll contains no
items, f must accept no arguments as well, and reduce returns the
result of calling f with no arguments.
from the docstring of reducespecifying what a "reducing function" is rather than just a function of two args
Jepp. So technically not wrong. But then why specify that it's a two args reducing fn, since that's also covered in the docstring of reduce?
Simply removing "of 2 args" would remove the confusion. I wonder if it's worth adding a similar function that takes a function of 2 args and a value, and returns a reducing function that returns that value for the 0-arity call?
(defn reducing
"Takes a function f of 2 args and a value and returns a reducing
function that returns the value if called with no arguments or
calls f if called with two arguments."
[f v]
(fn
([] v)
([x y] (f x y))))
Then you can have (reducing max Long/MIN_VALUE)
and completing
would compose with that.Isn’t that reducers/monoid?
That takes two functions, so it isn't as easy to use, and it's not in clojure.core
like completing
so it's harder to "discover", and its name doesn't tie in to reducing functions (in the same way that completing
ties into how transducers work).