D idom for removing array elements

albert-j via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 28 02:46:29 PST 2017


On Friday, 27 January 2017 at 08:15:56 UTC, Dukc wrote:

> void main()
> {   import std.stdio, std.algorithm, std.range, std.array, 
> std.datetime;
>     int[] a = [1, 2, 3, 4, 5, 6, 7, 4].cycle.take(2000).array;
>     int[] b = [3, 4, 6].cycle.take(2000).array;
>
>     void originalMethod()
>     {   auto c = a.remove!(x => b.canFind(x));
>         assert(c[0 .. 4] == [1, 2, 5, 7]);
>     }
>
>     void WilsonMethod()
>     {   auto c = a.filter!(x => !b.canFind(x)).array;
>         assert(c[0 .. 4] == [1, 2, 5, 7]);
>     }
>
>     void myMethod()
>     {   auto sortedB = sort(b.dup);
>         auto c = a
>         .   filter!(i => !sortedB.contains(i))
>         .   array
>         ;
>         assert(c[0 .. 4] == [1, 2, 5, 7]);
>     }
>
>     auto r = benchmark!(originalMethod, WilsonMethod, 
> myMethod)(1);
>     foreach(result; r) result.writeln;
> }


How to make it work with std.container.array?




More information about the Digitalmars-d-learn mailing list