Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there something for function composition in <functional>?

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

like image 914
Armen Tsirunyan Avatar asked Oct 10 '22 11:10

Armen Tsirunyan


1 Answers

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);
like image 68
Collin Dauphinee Avatar answered Oct 13 '22 10:10

Collin Dauphinee