Reducing an array
MattCoder via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sat Apr 19 09:55:32 PDT 2014
On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via
Digitalmars-d-learn wrote:
> Hi there,
>
> I try to remove all equal elements of an array, thus [2,2] -->
> [2].
>
> I thought this maybe would be possible with
> std.algorithm.reduce, but at least the way I tried it doesn't
> work:
>
> arr.reduce( (a,b) => a != b );
>
> Is there a simple solution using Phobos-functions?
Not too fancy, since I'm new in D, but I created this:
import std.stdio;
import std.array;
import std.algorithm;
static if (!is(typeof(writeln)))
alias writefln writeln;
void main(){
int myfilter(int a){
static int[] b;
if(b.find(a) == []){
b.insertInPlace(b.length, a);
return a;
}
return -1;
}
auto arr = [1,1,2,3,2,2,4,5,5,1];
auto arrFiltered = arr.filter!(a => myfilter(a) > 0);
writeln(arrFiltered);
}
Tested: http://dpaste.dzfl.pl/97a1307c7fec
I'm looking for creating something like C# "extensions", maybe
with alias. I'll try later!
Matheus.
More information about the Digitalmars-d-learn
mailing list