Re: functional way doing array stuff/ lambda functions

2015-12-14 Thread Namal via Digitalmars-d-learn
On Sunday, 13 December 2015 at 01:01:07 UTC, cym13 wrote: That's because you want to modify it in product passing it by ref. Hmm, that seems different to c++. On Sunday, 13 December 2015 at 11:37:50 UTC, cym13 wrote: As cryptic as it is this means that the range you passed to reduce is

Re: functional way doing array stuff/ lambda functions

2015-12-14 Thread visitor via Digitalmars-d-learn
On Monday, 14 December 2015 at 11:45:50 UTC, Namal wrote: foreach(k;1..11){ 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" :

Re: functional way doing array stuff/ lambda functions

2015-12-13 Thread cym13 via Digitalmars-d-learn
On Sunday, 13 December 2015 at 03:08:33 UTC, Namal wrote: On Saturday, 12 December 2015 at 23:50:55 UTC, Xinok wrote: [...] I tried this, it compiles, but crashes when I try to run it: object.Exception@/usr/include/dmd/phobos/std/algorithm/iteration.d(2481): Enforcement failed

Re: functional way doing array stuff/ lambda functions

2015-12-13 Thread visitor via Digitalmars-d-learn
On Sunday, 13 December 2015 at 03:08:33 UTC, Namal wrote: This works for me : import std.stdio, std.algorithm, std.range; int[] prim_factors(int n, const int[] P) { int[] v; P.filter!( x => x*x <= n).each!( (i) { while (n % i == 0) { v ~= i; n /= i;

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread Namal via Digitalmars-d-learn
On Saturday, 12 December 2015 at 23:50:55 UTC, Xinok wrote: On Saturday, 12 December 2015 at 23:36:43 UTC, cym13 wrote: ... So, in your example: int product(const ref int[] arr) { import std.array: array; import std.algorithm: reduce; arr = arr.reduce!((p, i) => p*i).array; }

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread Namal via Digitalmars-d-learn
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

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread cym13 via Digitalmars-d-learn
On Sunday, 13 December 2015 at 00:36:29 UTC, Namal wrote: 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

functional way doing array stuff/ lambda functions

2015-12-12 Thread Namal via Digitalmars-d-learn
Hello guys, I am still uncertain how to do it right when it comes to lambda functions. For instance: how do I multiply all the elements in an array ? int product(const ref int[] arr){ int p = 1; foreach(i;arr) p*=i; return p; }

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread cym13 via Digitalmars-d-learn
On Saturday, 12 December 2015 at 23:10:21 UTC, Namal wrote: Hello guys, I am still uncertain how to do it right when it comes to lambda functions. If you are looking for the functionnal way I'd advise that you start by looking up three functions in a language-agnostic way: filter, map and

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread Xinok via Digitalmars-d-learn
On Saturday, 12 December 2015 at 23:36:43 UTC, cym13 wrote: ... So, in your example: int product(const ref int[] arr) { import std.array: array; import std.algorithm: reduce; arr = arr.reduce!((p, i) => p*i).array; } A good post overall but you got reduce wrong. In D, reduce

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread cym13 via Digitalmars-d-learn
On Saturday, 12 December 2015 at 23:50:55 UTC, Xinok wrote: On Saturday, 12 December 2015 at 23:36:43 UTC, cym13 wrote: ... So, in your example: int product(const ref int[] arr) { import std.array: array; import std.algorithm: reduce; arr = arr.reduce!((p, i) => p*i).array; }

Re: functional way doing array stuff/ lambda functions

2015-12-12 Thread cym13 via Digitalmars-d-learn
On Saturday, 12 December 2015 at 23:59:01 UTC, cym13 wrote: On Saturday, 12 December 2015 at 23:50:55 UTC, Xinok wrote: On Saturday, 12 December 2015 at 23:36:43 UTC, cym13 wrote: ... So, in your example: int product(const ref int[] arr) { import std.array: array; import