hof
hof copied to clipboard
apply - what is the utility of apply?
Oh, I believe I have found why I don't understand. The template parameters TS are not part of the function apply This
template<class F, class... Ts>
constexpr auto apply(F&& f, Ts&&... xs);
should be
template<class F>
constexpr auto apply(F&& f);
The semantics given us
assert(apply(f)(xs...) == f(xs...));
But the usage is incompatible
assert(fit::apply(sum_f(), 1, 2) == 3);
that I believe should be
assert(fit::apply(sum_f())(1, 2) == 3);
Unfortunately the implementation is not inline at all.
Oops, thats a mistake in the documentation. The semantics should be:
assert(apply(f, xs...) == f(xs...));
So the question remains, what is the utility of this function?
That you want to call the first parameter with the rest of the parameters. One example, is to curry the functions parameters:
assert(compress(apply, f)(x, y, z) == f(x)(y)(z));
Ok, I see apply
is the monoid operator on function composition.
Could you add this example or another more user friendly?
Could you add this example or another more user friendly?
Yea I will.
The semantics must be fixed yet.
Oh, should I add that to the "semantics" of apply
? That might be a good idea.