Fork me on GitHub
#clojure-dev
<
2020-06-21
>
slipset18:06:48

@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."

slipset18:06:40

The incorrectness lies in that the function returned by completing can invoke f as a zero-arity fn. Is this worth a issue/patch?

slipset18:06:58

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> 

dpsutton18:06:17

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 reduce

dpsutton18:06:47

specifying what a "reducing function" is rather than just a function of two args

slipset18:06:12

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?

seancorfield19:06:06

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.

slipset20:06:44

Isn’t that reducers/monoid?

seancorfield20:06:00

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).