C++ Logo

std-proposals

Advanced search

Curried <functional> function objects

From: Justin Bassett <jbassett271_at_[hidden]>
Date: Tue, 14 Apr 2020 17:40:54 -0700
I use the function objects in <functional> all the time in conjunction with
algorithms. However, sometimes, I find that I really want a curried
function. For example:

find_if(b, e, not_equal_to(0));
transform(b, e, plus(1));
copy_if(b, e, o, less(0))

These are similar to bind_front(not_equal_to(), 0), except the calling
syntax is more readable, and it makes sense to have the operand order
flipped for many or all of these (less(0) ==> "is x less than 0" instead of
"is x greater than 0").

I can think of two ways to implement something like this:

   1. Have a 1-arg constructor for partial specializations of the function
   objects along with deduction guides. Calling syntax is elegant.
   2. Have a 1-arg call operator which does the partial application.
   Calling syntax is less elegant: not_equal_to()(0)

Having implemented this myself—a variation where they are function objects
and not types—I find that these are immensely useful. It's quite frequent
that the lambda I would need to use is something as simple as one of these.

Other considerations:

   - divides(2) is not as clear. Is this saying, "Does n divide 2, i.e. is
   n a divisor of 2?"
   - If minus(2) is x - 2, should there be a way to do 2 - x?

Has something like this been proposed before? Is something like this
reasonable to be proposed?

Thanks,
Justin Bassett

Received on 2020-04-14 19:44:06