Lambda

About advanced scenes, and the Thyme scripting language used in Algodoo.

Lambda

Postby l4m2 » Sat Apr 16, 2016 5:53 pm

In JavaScript, fact=function(x){f=(function(t,s){return t>0?s(t-1,s)*t:1});return f(x,f);};fact(8) can return 40320 but in thyme fact=(x)=>{f=(t,s)=>{t>0?s(t-1,s)*t:1};f(x,f)};fact(8) causes a stack-overflow --- even fact(0) causes one
l4m2
 
Posts: 42
Joined: Fri Jun 27, 2014 6:21 am

Re: Lambda

Postby Kilinich » Sun Apr 17, 2016 7:22 pm

Do you need factorial function?
Code: Select all
scene.my.fact = (i) => {i > 1 ? {scene.my.fact(i-1) * i} : 1}
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Lambda

Postby l4m2 » Sun Apr 17, 2016 8:05 pm

not for a fact function which can be written.as fact=function(x){return x?callee(x-1)*x:1)})
l4m2
 
Posts: 42
Joined: Fri Jun 27, 2014 6:21 am

Re: Lambda

Postby Kilinich » Sun Apr 17, 2016 10:37 pm

So what's the problem?
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Lambda

Postby l4m2 » Mon Apr 18, 2016 2:17 pm

is about why it happen and how to use lambda expr (or impossible)
l4m2
 
Posts: 42
Joined: Fri Jun 27, 2014 6:21 am

Re: Lambda

Postby Kilinich » Mon Apr 18, 2016 2:32 pm

l4m2 wrote:is about why it happen and how to use lambda expr (or impossible)


I see, you just shoul add {} to if-the-else
t > 0 ? s(t-1,s) * t : 1
t > 0 ? {s(t-1,s) * t} : 1

Code: Select all
scene.my.fact := (x) => {
f := (t, s) => {t > 0 ? {s(t-1,s) * t} : 1};
f (x, f);
};
scene.my.fact(8)


Algodoo always calc both result values of if-then-else structure so brackets tells that it is function, not value and it will only be called if result is true.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Lambda

Postby l4m2 » Mon Apr 18, 2016 2:45 pm

Kilinich wrote:Algodoo always calc both result values of if-then-else structure so brackets tells that it is function, not value and it will only be called if result is true.
Yes it is!
l4m2
 
Posts: 42
Joined: Fri Jun 27, 2014 6:21 am

Re: Lambda

Postby Kilinich » Mon Apr 18, 2016 4:17 pm

l4m2 wrote:
Kilinich wrote:Algodoo always calc both result values of if-then-else structure so brackets tells that it is function, not value and it will only be called if result is true.
Yes it is!

So you're satisfied now ? :thumbup:
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia

Re: Lambda

Postby FRA32 » Wed May 11, 2016 8:09 pm

You can easily make code blocks by using {} around your code. This results in everything beeing handled as 1 "code variable" that is inserted wherever you need it. You can even assign these to variables. If you add a function header to it(for example (x,y,z)=>{}), you can make functions that use the variables in the header as arguments
FRA32
 
Posts: 227
Joined: Wed Dec 03, 2014 9:51 pm


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 6 guests

cron