Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add count end pipe #50

Closed
jjcasmar opened this issue Apr 22, 2020 · 3 comments
Closed

add count end pipe #50

jjcasmar opened this issue Apr 22, 2020 · 3 comments

Comments

@jjcasmar
Copy link
Contributor

This is an end pipe which captures an integral type and return in that integral type the number of values that has gone through the pipe. Useful for example to filter a vector, override values directly in place and remove the unfiltered values.

    auto v1Andv2 = ranges::views::zip(v1, v2);
    ranges::actions::sort(v1Andv2);
    int count = 0;
    ranges::views::unique(v1Andv2) >>= pipes::fork(pipes::unzip(pipes::override(v1),   //
                                                                pipes::override(v2)),  //
                                                   pipes::for_each([&count](const auto &v) { count++; }));
    v1.erase(std::begin(v1) + count, std::end(v1));
    v2.erase(std::begin(v2) + count, std::end(v2));

Instead of a for_each performing the count, a specific component would be more verbose.

@joboccara
Copy link
Owner

This is an interesting use case, thanks!
As the purpose of for_each is to allow customisation, rather than adding a component to the library, why not tacking a name on the one you're creating above?

auto counter(int& count)
{
    return pipes::for_each([&count](auto const&){ ++count; });
}

Fyi I've added a test to make sure this works.

@godefv
Copy link

godefv commented Oct 5, 2020

This is a special case of issue #33.
pipes::count would be the same as pipes::transform([](auto const&){return 1;}) >>= pipes::reduce(std::sum)

@joboccara
Copy link
Owner

Nicely spotted. I'll close this issue then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants