rx-ranges icon indicating copy to clipboard operation
rx-ranges copied to clipboard

Why is uniq a sink?

Open Kijewski opened this issue 5 years ago • 1 comments

I would expect it to work like

RX_OPTIONAL<output_type> last_value;

uniq(…) {
    last_value.emplace(input.get());
    input.next();
}

constexpr void next() {
    while (!input.at_end()) {
        auto&& next_value = input.get();
        input.next();
        if (!eq(*last_value, next_value)) {
            last_value.emplace(std::forward<decltype(next_value)>(next_value));
            return;
        }
    }
    last_value.reset();
}

constexpr output_type get() const {
    return last_value;
}

Kijewski avatar Nov 19 '19 03:11 Kijewski

It's a sink because the most common use case for it is sort() | uniq(), and if it was a normal input range, that particular construct would allocate temporary storage.

However, we do have the option to overload operator() for cases when its input is another sink (in which case calling std::unique on the output is the right thing), and when it is part of a chain of input ranges. Might be a nice addition for version 2.1. :-)

simonask avatar Nov 19 '19 21:11 simonask