Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Meta via Digitalmars-d-learn
On Thursday, 11 September 2014 at 21:28:59 UTC, Colin wrote: Using the "alias x this" solution would work, but my actual struct is not a simple struct, so the comparison isn't exactly (a.x < b.x). You could always override opCmp as well: http://dlang.org/operatoroverloading.html#compare

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Colin via Digitalmars-d-learn
On Thursday, 11 September 2014 at 14:49:03 UTC, bearophile wrote: Daniel Kozak: You can just use min: import std.stdio, std.algorithm; struct Thing { uint x; alias x this; } alias minimum = reduce!min; void main() { immutable ar1 = [10, 20, 30, 40, 50]; ar1.minimum.wr

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Meta via Digitalmars-d-learn
On Thursday, 11 September 2014 at 14:49:03 UTC, bearophile wrote: void main() { //... immutable ar2 = [Thing(10), Thing(20), Thing(40)]; ar2.minimum.writeln; } Bye, bearophile Even better: void main { immutable(Thing)[] ar2 = [10, 20, 40]; ar2.minimum.write

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 11 September 2014 at 14:56:00 UTC, Daniel Kozak via Digitalmars-d-learn wrote: V Thu, 11 Sep 2014 14:49:02 + bearophile via Digitalmars-d-learn napsáno: Daniel Kozak: You can just use min: import std.stdio, std.algorithm; struct Thing { uint x; alias x this; }

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread monarch_dodra via Digitalmars-d-learn
On Thursday, 11 September 2014 at 15:29:18 UTC, Daniel Kozak wrote: On Thursday, 11 September 2014 at 15:07:03 UTC, Daniel Kozak wrote: or use alias minimum = reduce!"a < b"; ;) ok this one does not work Yeah, it's actually reduce!"a < b ? a : b"

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Thursday, 11 September 2014 at 15:07:03 UTC, Daniel Kozak wrote: or use alias minimum = reduce!"a < b"; ;) ok this one does not work

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Thursday, 11 September 2014 at 14:56:00 UTC, Daniel Kozak via Digitalmars-d-learn wrote: V Thu, 11 Sep 2014 14:49:02 + bearophile via Digitalmars-d-learn napsáno: Daniel Kozak: You can just use min: import std.stdio, std.algorithm; struct Thing { uint x; alias x this; }

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Daniel Kozak via Digitalmars-d-learn
V Thu, 11 Sep 2014 14:49:02 + bearophile via Digitalmars-d-learn napsáno: > Daniel Kozak: > > You can just use min: > > import std.stdio, std.algorithm; > > struct Thing { > uint x; > alias x this; > } > > alias minimum = reduce!min; > > void main() { > immutable ar1 = [1

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread bearophile via Digitalmars-d-learn
Daniel Kozak: You can just use min: import std.stdio, std.algorithm; struct Thing { uint x; alias x this; } alias minimum = reduce!min; void main() { immutable ar1 = [10, 20, 30, 40, 50]; ar1.minimum.writeln; immutable ar2 = [Thing(10), Thing(20), Thing(40)];

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Thursday, 11 September 2014 at 14:39:53 UTC, Daniel Kozak wrote: On Thursday, 11 September 2014 at 14:18:31 UTC, Colin wrote: Ah ok. I get it. Thanks daniel! a quiet better version: import std.stdio; import std.algorithm; struct Thing { uint x; alias x this; } void main

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Thursday, 11 September 2014 at 14:18:31 UTC, Colin wrote: Ah ok. I get it. Thanks daniel! a quiet better version: import std.stdio; import std.algorithm; struct Thing { uint x; alias x this; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.red

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Colin via Digitalmars-d-learn
On Thursday, 11 September 2014 at 13:27:39 UTC, Daniel Kozak wrote: On Thursday, 11 September 2014 at 13:06:05 UTC, Colin wrote: I have this test code: struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) => a < b); writefln("%s", min1);

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread via Digitalmars-d-learn
On Thursday, 11 September 2014 at 13:28:37 UTC, Marc Schütz wrote: On Thursday, 11 September 2014 at 13:06:05 UTC, Colin wrote: I have this test code: struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) => a < b); writefln("%s", min1);

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread Daniel Kozak via Digitalmars-d-learn
On Thursday, 11 September 2014 at 13:06:05 UTC, Colin wrote: I have this test code: struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) => a < b); writefln("%s", min1); // prints 1 as expected Thing[] ar2 = [Thing(1), Thing(2),

Re: std.algorithm.reduce on an array of structs

2014-09-11 Thread via Digitalmars-d-learn
On Thursday, 11 September 2014 at 13:06:05 UTC, Colin wrote: I have this test code: struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) => a < b); writefln("%s", min1); // prints 1 as expected Thing[] ar2 = [Thing(1), Thing(2),

std.algorithm.reduce on an array of structs

2014-09-11 Thread Colin via Digitalmars-d-learn
I have this test code: struct Thing { uint x; } void main(){ uint[] ar1 = [1, 2, 3, 4, 5]; auto min1 = ar1.reduce!((a,b) => a < b); writefln("%s", min1); // prints 1 as expected Thing[] ar2 = [Thing(1), Thing(2), Thing(4)]; auto min2 = ar2.reduce!((a,b) => a.x < b.x);