What I want to do is done pretty easily with C++0x lambdas. I just want to figure out how to do it with bind1st
and bind2nd
only.
I need to find the first element i
in the vector v
, such that (i-1)/p1 == p2
where p1 and p2 are predefined integers.
find_if(v.begin(), v.end(), ???)
I can't figure out how to form the predicate with minus<int>
divides<int>
equal_to<int>
and bind2nd
. Seems like I need some function composition
equal(div(minus(i, 1), p1), p2)
Is it possible?
Please don't suggest workarounds such as writing a freestanding unary function or a struct with operator()
. I am really interested in mechanics of functors in functional along with bind1st
and bind2nd
. This is not for real code, just interest. Thanks :)
Unfortunately, what you're trying to do is not possible with only the current functional
library. You would need to create your own class or function to do the composition.
Boost used to offer a Compose library that was used to compose functions like this, but I think it was removed when Bind and Lambda were introduced.
And just for the sake of completion, it can be done with bind
using something like:
std::bind(std::equal_to<int>(), /* ... == P2 */
std::bind(std::divides<int>(), /* ... / P1 == P2 */
std::bind(std::minus<int>(), _1, 1), /* (x - 1) / P1 == P2 */
p1),
p2);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With