Folds.jl icon indicating copy to clipboard operation
Folds.jl copied to clipboard

Add single-argument version of Folds.reduce?

Open oschulz opened this issue 4 years ago • 2 comments

Would be handy to be able to write

Filter(isodd) ⨟ MapCat(x -> 1:x^2) ⨟ Folds.reduce(+)

instead of

Filter(isodd) ⨟ MapCat(x -> 1:x^2) ⨟ (x -> Folds.reduce(+, x))

Or would a single-argument Folds.reduce break some concepts behind Folds?

oschulz avatar Feb 05 '21 21:02 oschulz

Thanks for the suggestion! There's actually a "secrete" function Transducers.fold that does this. But I'm not sure about the API so it's currently not a public API.

The main reasons why I'm a bit hesitant for implementing this are that:

  1. If I go to this direction, I'd like to support xs |> Folds.reduce(op, executor) for Folds.reduce(op, xs, executor), too.
  2. But, if I do it, it's hard to decide what Folds.reduce(a, b) would do without knowing the type of b.

Maybe the best way forward to add reducewith(op, [executor]) function, like mergewith in Base.

tkf avatar Feb 08 '21 20:02 tkf

Oh, yes.

Just an idea - how about using Folds.reduce(op, executor = default_exec)(xs) as a syntax instead, so that Folds.reduce(op, executor = default_exec) becomes a functional, semantically, just like Folds.sum?

Actually, how does one currently set the executor for Folds.sum & friends?

oschulz avatar Feb 08 '21 21:02 oschulz