sortUniq

ixid via Digitalmars-d digitalmars-d at puremagic.com
Sat Jan 24 03:44:03 PST 2015


> import std.stdio, std.algorithm;
>
> auto unique(){
>     bool[int] c;
>     return (int a){
>         if (a in c)
>             return false;
>         else{
>             c[a] = true;
>             return true;
>         }
>     };
> }
>
> void main()
> {
>     [1, 5, 5, 2, 1, 5, 6, 6].filter!(unique()).writeln;
> }

Just add static to the closure.

auto unique(){
     static bool[int] c;
     return (int a){
         if (a in c)
             return false;
         else{
             c[a] = true;
             return true;
         }
     };
}


More information about the Digitalmars-d mailing list