range-v3 icon indicating copy to clipboard operation
range-v3 copied to clipboard

transformations of ranges of tuple-like objects

Open rbrugo opened this issue 5 years ago • 7 comments
trafficstars

Hi, I could be wrong, but currently if I want to transform a tuple-like object I have to do something like

auto result1 = range_of_tuples | views::transform([&f](auto && tuple) {
    auto [x, y, z] = tuple;
    return f(x, y, z);
});

auto result2 = range_of_tuples | views::transform([&f](auto && tuple) {
    return std::apply(f, tuple);
});

I think it would be nice if it were possible to write

auto result1 = range_of_tuples | views::transform(f);
auto result2 = range_of_tuples | views::apply(f);

by having an overload for transform_view::operator() which tries to std::apply if it is not possible to INVOKE, or a completely new view if that is not possible

rbrugo avatar Apr 02 '20 10:04 rbrugo

I like this idea.

ericniebler avatar Apr 02 '20 16:04 ericniebler

Until that time, I think this (from Boost HOF) can help.

beojan avatar May 20 '20 08:05 beojan

+1 from transform_apply of some sort.

Some people suggest create wrappers: https://stackoverflow.com/a/65855073, but having this as a ranges "vocabulary" view would be awesome.

Talkless avatar Sep 20 '22 06:09 Talkless

Same for filtering. Currently I have:

    //find first empty slot in the grid, starting from top left, moving in rows to the right
    for (const auto& [row, column]: rv::cartesian_product(rv::indices(gridHeight), rv::indices(gridWidth))
         | rv::filter([this](const auto& tuple){ const auto [row, column] = tuple; return !containsSlot(column, row); })) {
        return QPoint{column, row};
    }

So instead of

rv::filter([this](const auto& tuple){ const auto [row, column] = tuple; return !containsSlot(column, row); }

It would be nice to have something like this:

rv::filter_apply([this](int row, int column){ return !containsSlot(column, row); }

Talkless avatar Sep 22 '22 12:09 Talkless

In light of that, rather than adding N view_apply, it'd be better to just have a predicate wrapper applied(f) that does std::apply(f, e).

JohelEGP avatar Sep 22 '22 15:09 JohelEGP

Until that time, I think this (from Boost HOF) can help.

Not apply, unpack.

brevzin avatar Sep 22 '22 16:09 brevzin

So it would look like this?

| filter(unpack(mylambda)) | ...

Talkless avatar Sep 23 '22 05:09 Talkless