std.algorithm.reduce on an array of structs

Colin via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 11 14:28:57 PDT 2014


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).



More information about the Digitalmars-d-learn mailing list