On 19 Jan 2013, at 20:56, Igor Stasenko <[email protected]> wrote:
> [:param |
> param >0 ifTrue: [ thisContext closure value: param - 1 ]
>
> ] value: 5
>
> but this will be much slower than just use recursion with methods
> because of this context.
This is so cool, Igor. I didn't know about this trick.
All in all, writing factorial like this is pretty elegant:
[ :n |
n < 2
ifTrue: [ 1 ]
ifFalse: [ n * (thisContext closure value: n - 1) ] ] value: 5
AFAIK, this is impossible in Lisp like languages.
Thanks for teaching us !
Sven
--
Sven Van Caekenberghe
http://stfx.eu
Smalltalk is the Red Pill