sortUniq

zeljkog via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 24 11:30:14 PST 2015


On 24.01.15 19:47, Andrei Alexandrescu wrote:
>>
>> But here there is potential problem with shared state after save?
> 
> It's not a problem - save saves the state of the iteration, not the data 
> being iterated. -- Andrei
> 

If you pass stateful object with opCall or closure it becomes part of "state of the iteration"?

Consider:

import std.stdio, std.algorithm;

struct Uniq{
     bool[int] c;
     bool opCall(int a){
         if (a in c)
             return false;
         else{
             c[a] = true;
             return true;
         }
     }
} 

void main()
{
    Uniq a;
    auto arr = [1, 5, 5, 2, 1, 5, 6, 6];
    auto r = arr.filter!a;
    auto r1 = r.save();
    r.writeln;
    r1.writeln;
}

output:

[1, 2, 6]
[5]







More information about the Digitalmars-d mailing list