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 fo

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); Thank

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:

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 d

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 :) Als

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 > > x)=>x?x*__traits

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,

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 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 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); } assert((f

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 * ( x>1 ? fact(x - 1): 1); }; -manfred

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) ==