reading file byLine

anonymous via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 5 13:55:07 PDT 2015


On Saturday 05 September 2015 22:22, Namal wrote:

> import std.stdio, std.algorithm, std.array;
> 
> bool abundant(int n){
[...]
> }
> 
> 
> 
> void main(){
> 	
> 	long sum;
> 	int[] arr;
> 	int[28123] mark;
> 	
> 	foreach(i;1..28124)
> 		if(abundant(i))
> 			arr~=i;
[...]
> }
> 
> How can I generate the array arr the functional way with reduce 
> and the function abundant as filter. Tried it on my own but 
> failed.

import std.range: iota;
int[] arr = iota(1, 28124).filter!abundant.array;

> Also, if I use reduce on an array, like I did for the 
> sum, what does the zero mean in the range?

You mean the 0 in this: `reduce!((a,b)=>a+b)(0,a)`, right? It's used as the 
initial value for the `a` argument to the lambda. That is, the first 
calculation there is 0+a[0]. Then a[1] is added to that, and so on.

Note that there's a specialized `std.algorithm.iteration.sum`.


More information about the Digitalmars-d-learn mailing list