functional way doing array stuff/ lambda functions

visitor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Dec 14 07:41:18 PST 2015


On Monday, 14 December 2015 at 11:45:50 UTC, Namal wrote:
> 	
> 	foreach(k;1..100001){
> 		auto t = prim_factors(k,P);
>   		v~= [k,product(t)];
> 	}

it crashes because your first t in the loop is an empty array

because 1 is not a prime ( in "prim_sieve" : T[1] = true)
later when you loop starting with k = 1, prim_factors(1,P) gives 
you back an empty array.
so either loop through 2..100001 or guard against the empty array
if (!t.empty) v~= [k, product(t)];

i don't think you need any ref in your functions parameters, as a 
slice is already a reference
http://dlang.org/spec/arrays.html#slicing


More information about the Digitalmars-d-learn mailing list