Performance optimization for minElement and maxElement

JG someone at somewhere.com
Mon Sep 12 17:07:18 UTC 2022


On Monday, 12 September 2022 at 17:05:44 UTC, JG wrote:
> On Monday, 12 September 2022 at 16:26:01 UTC, Paul Backus wrote:
>> [...]
>
> Could build it like this (not sure performance is great).
> ```d
> import std;
>
> auto stoppingFold(alias stopCond, alias op, R)(R r) {
>     return 
> r.cumulativeFold!op.until!(stopCond)(No.openRight).fold!((a,b)=>b);
> }
>
> void main() {
>     auto x = [1,2,4,3];
>     assert(x.stoppingFold!(x=>x==0,(a,b)=>min(a,b))==1);
>     x = [1,0,4,3];
>     assert(x.stoppingFold!(x=>x==0,(a,b)=>min(a,b))==1);
> }
>
> ```

Oops last assert is wrong... should be ==0


More information about the Digitalmars-d mailing list