Re: Lambda functions in D

2015-05-09 Thread Timon Gehr via Digitalmars-d-learn
On 05/09/2015 05:52 PM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 14:15:21 UTC, Ali Çehreli wrote: On 05/09/2015 04:59 AM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int x)=x?x*__traits(parent,{})(x-1):1)(10)==3628800);

Re: Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 9 May 2015 at 21:48:05 UTC, Timon Gehr wrote: Well, it is much slower due to all the allocated closures, owed to the fact that the implementations of 'fix' on that page are expected to mirror a particular famous implementation in untyped lambda calculus. In case you have a use

Re: Lambda functions in D

2015-05-09 Thread tcak via Digitalmars-d-learn
On Saturday, 9 May 2015 at 11:20:10 UTC, Dennis Ritchie wrote: Hi, Can lambda functions or delegates in D to call themselves? Can I write something like this: - import std.stdio; void main() { auto fact = function (int x) = x * { if (x) fact(x - 1); }; assert(fact(10) ==

Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn
Hi, Can lambda functions or delegates in D to call themselves? Can I write something like this: - import std.stdio; void main() { auto fact = function (int x) = x * { if (x) fact(x - 1); }; assert(fact(10) == 3628800); }

Re: Lambda functions in D

2015-05-09 Thread Manfred Nowak via Digitalmars-d-learn
Dennis Ritchie wrote: auto fact = function (int x) = x * { if (x) fact(x - 1); }; int fact (int x) { return x * ( x1 ? fact(x - 1): 1); }; -manfred

Re: Lambda functions in D

2015-05-09 Thread Timon Gehr via Digitalmars-d-learn
On 05/09/2015 01:20 PM, Dennis Ritchie wrote: Hi, Can lambda functions or delegates in D to call themselves? Can I write something like this: - import std.stdio; void main() { auto fact = function (int x) = x * { if (x) fact(x - 1); }; assert(fact(10) == 3628800); }

Re: Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int x)=x?x*__traits(parent,{})(x-1):1)(10)==3628800); Thanks. Yes, it is similar to what I wanted :)

Re: Lambda functions in D

2015-05-09 Thread Dennis Ritchie via Digitalmars-d-learn
On Saturday, 9 May 2015 at 14:15:21 UTC, Ali Çehreli wrote: On 05/09/2015 04:59 AM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int x)=x?x*__traits(parent,{})(x-1):1)(10)==3628800); Thanks. Yes, it is similar to what I wanted :)

Re: Lambda functions in D

2015-05-09 Thread John Colvin via Digitalmars-d-learn
On Saturday, 9 May 2015 at 14:47:21 UTC, Russel Winder wrote: On Sat, 2015-05-09 at 07:15 -0700, Ali Çehreli via Digitalmars-d-learn wrote: On 05/09/2015 04:59 AM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int

Re: Lambda functions in D

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn
On 05/09/2015 07:47 AM, Russel Winder via Digitalmars-d-learn wrote: Of course none of the implementation can calculate factorial(24) as they are using hardware values which are bounded and cannot store reasonable numbers. Could use iota. Oh no we can't as BigNums are not integral. I don't

Re: Lambda functions in D

2015-05-09 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2015-05-09 at 07:15 -0700, Ali Çehreli via Digitalmars-d-learn wrote: On 05/09/2015 04:59 AM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int x)=x?x*__traits(parent,{})(x-1):1)(10)==3628800); Thanks. Yes, it is similar

Re: Lambda functions in D

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn
On 05/09/2015 04:59 AM, Dennis Ritchie wrote: On Saturday, 9 May 2015 at 11:49:48 UTC, Timon Gehr wrote: assert((function int(int x)=x?x*__traits(parent,{})(x-1):1)(10)==3628800); Thanks. Yes, it is similar to what I wanted :) Also interesting: http://rosettacode.org/wiki/Y_combinator#D

Re: Lambda functions in D

2015-05-09 Thread Ali Çehreli via Digitalmars-d-learn
On 05/09/2015 10:45 AM, Russel Winder via Digitalmars-d-learn wrote: On Sat, 2015-05-09 at 09:49 -0700, Ali Çehreli via Digitalmars-d-learn wrote: […] BigInt factorial(size_t n) { return bigInts(1).take(n).reduce!((a, b) = a *= b); } I wonder if that should be a * b rather than a *=

Re: Lambda functions in D

2015-05-09 Thread Russel Winder via Digitalmars-d-learn
On Sat, 2015-05-09 at 09:49 -0700, Ali Çehreli via Digitalmars-d-learn wrote: […] BigInt factorial(size_t n) { return bigInts(1).take(n).reduce!((a, b) = a *= b); } I wonder if that should be a * b rather than a *= b? It turns out that 2.067 fixes the integrality of BigInts so: