Re: *** GMX Spamverdacht *** Re: Reducing an array

2014-04-19 Thread Tim Holzschuh via Digitalmars-d-learn
Am 18.04.2014 22:27, schrieb Steven Schveighoffer via Digitalmars-d-learn: arr.sort(); arr = arr.uniq.array(); -Steve Thanks, also for the explanation! - Tim

Re: Reducing an array

2014-04-19 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 22:11:17 UTC, bearophile wrote: monarch_dodra: Out of curiosity, if the requirement was to *also* preserve ordering (eg: remove all non-first elements), how would you go at it? [2, 1, 1, 3, 2, 3] = [2, 1, 3]; Maybe std.algorithm's `makeIndex` would help here?

Re: Reducing an array

2014-04-19 Thread MattCoder via Digitalmars-d-learn
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(

Re: Reducing an array

2014-04-19 Thread Ali Çehreli via Digitalmars-d-learn
On 04/19/2014 09:55 AM, MattCoder wrote: On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via Digitalmars-d-learn wrote: void main(){ int myfilter(int a){ static int[] b; That static variable makes this solution non-reentrant. To see an effect of this, replace the

Re: Reducing an array

2014-04-19 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 22:11:17 UTC, bearophile wrote: This preserves ordering and it's in-place. Not tested much: void main() { import std.stdio, std.traits; auto data = [2, 1, 1, 3, 2, 3]; bool[ForeachType!(typeof(data))] seen; size_t pos = 0; foreach (immutable i;

Re: Reducing an array

2014-04-19 Thread MattCoder via Digitalmars-d-learn
On Saturday, 19 April 2014 at 17:12:10 UTC, Ali Çehreli wrote: On 04/19/2014 09:55 AM, MattCoder wrote: On Friday, 18 April 2014 at 20:02:41 UTC, Tim Holzschuh via Digitalmars-d-learn wrote: void main(){ int myfilter(int a){ static int[] b; That static variable makes this

Re: Reducing an array

2014-04-18 Thread Steven Schveighoffer via Digitalmars-d-learn
On Thu, 17 Apr 2014 09:46:25 -0400, Tim Holzschuh via Digitalmars-d-learn digitalmars-d-learn@puremagic.com 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

Re: Reducing an array

2014-04-18 Thread monarch_dodra via Digitalmars-d-learn
On Friday, 18 April 2014 at 20:27:20 UTC, Steven Schveighoffer wrote: Note also that what it returns is not an array, but a lazily iterated range over the array. If you want another array, this is the code I would use: arr.sort(); arr = arr.uniq.array(); -Steve Out of curiosity, if the

Re: Reducing an array

2014-04-18 Thread bearophile via Digitalmars-d-learn
monarch_dodra: Out of curiosity, if the requirement was to *also* preserve ordering (eg: remove all non-first elements), how would you go at it? [2, 1, 1, 3, 2, 3] = [2, 1, 3]; Maybe std.algorithm's `makeIndex` would help here? Bonus points for doing it inplace. This preserves ordering