functional way doing array stuff/ lambda functions

Namal via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Dec 12 16:36:29 PST 2015


On Sunday, 13 December 2015 at 00:02:11 UTC, cym13 wrote:

> Now that I think about it, it's true that it would make no 
> sense whatsoever to return a range as reduce is typically used 
> to return a single value... At least it makes perfect sense.

Thanks alot, this helped alot. But I have another question

I have two functions:

int[] prim_factors(int n, const ref int[] P){
	
	int[] v;
	
	for(int i; P[i]*P[i]<=n;++i){
		while(n%P[i]==0){
			v~=P[i];
			n/=P[i];
		}
	}
	if(n>1)
		v~=n;
		
	return v.dup.sort.uniq.array;

}


int product(const ref int[] arr){

	int p = 1;
	foreach(i;arr)
		p*=i;
	return p;
}

While vector P contains some primes I get with a prime sieve. So 
if I just try to use those functions like:

writeln(product(prim_factors(10,P)));

I get the error:

function prog.product (ref const(int[]) arr) is not callable 
using argument types (int[])

Why do I have to call it like that first:

	auto v = prim_factors(10,P);
	writeln(product(v));

?



More information about the Digitalmars-d-learn mailing list