Sum and other algorithm functions

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Sep 10 06:55:51 PDT 2015


On Thursday 10 September 2015 15:48, Namal wrote:

> Hello,
> 
> how can I define the range for the sum function which I want to 
> sum up? For instance how do I sum up the first 3 elements of an 
> array
> 
> int[] a = [1,2,3,4,5,6,7,8,9];
> 
> or the last 3?

First you slice the first/last 3, then you sum them.

first 3: a[0 .. 3].sum
last 3: a[$ - 3 .. $].sum


More information about the Digitalmars-d-learn mailing list