September 11, 2014 Re: std.algorithm.reduce on an array of structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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
|
September 11, 2014 Re: std.algorithm.reduce on an array of structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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 +0000 > bearophile via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> > 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 = [10, 20, 30, 40, 50]; >> ar1.minimum.writeln; >> >> immutable ar2 = [Thing(10), Thing(20), Thing(40)]; >> ar2.minimum.writeln; >> } >> >> Bye, >> bearophile > > Yep, this look the most idiomatic :). > > Why there is no phobos function for minimum of array(range)? There is: http://dlang.org/phobos/std_algorithm.html#.minPos To note though: minPos will "find" the position of the smallest element, whereas reduce will accumulate and return the lowest value. That said: alias minmax = reduce!(min, max); auto mm = ar1.minmax(); auto min = mm[0]; auto max = mm[1]; Found both in one line of code, and only 1 iteration of ar1. |
September 11, 2014 Re: std.algorithm.reduce on an array of structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Daniel Kozak | 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"
|
September 11, 2014 Re: std.algorithm.reduce on an array of structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.writeln;
}
|
September 11, 2014 Re: std.algorithm.reduce on an array of structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | 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.writeln;
>
> immutable ar2 = [Thing(10), Thing(20), Thing(40)];
> ar2.minimum.writeln;
> }
>
> Bye,
> bearophile
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).
|
September 12, 2014 Re: std.algorithm.reduce on an array of structs | ||||
---|---|---|---|---|
| ||||
Posted in reply to Colin | 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 |
Copyright © 1999-2021 by the D Language Foundation