Andrea Fontana:

Something goes wrong by the way. I removed "n" template params and "take(n)" (i can do after distinct() call, isn't it the same?).

I think take() is not the cause.

See this program:



import std.stdio, std.range, std.algorithm, std.traits, std.random;

auto distinct(Range)(Range r) if (isInputRange!Range) {
    bool[ForeachType!Range] mySet;

    return r.filter!((k) {
        if (k in mySet)
            return false;
        mySet[k] = true;
        return true;
    });
}

void main() {
    5.iota.map!((_) {
        auto x = uniform(0, 10);
        write("*");
        return x;
    }).distinct.writeln;
}


It outputs something like:

*[*2*, *0*, *2**, *1]


You expect that output to contain only 5 stars.

Maybe map() or filter() is the cause of this.

Bye,
bearophile

Reply via email to