Fwd: Call a block inside same block for recursive requirements

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Fwd: Call a block inside same block for recursive requirements

Sean P. DeNigris
Administrator
gerard wrote
Hi.

Excuse me for my poor english.

I need, for recursive requirements, call a block inside the same block. That's possible on Smalltalk?

For example:

"fibonacci function"
 [:fnc :n1 :n2 :limit :numbers | numbers add: n1. (n2 < limit) ifTrue: [ fnc valueWithArguments: {fnc . n2 . (n1 + n2) . limit . numbers}. numbers ]].

The objetive is use that block without the :fnc parameter.


Regards
Cheers,
Sean
Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Call a block inside same block for recursive requirements

Igor Stasenko
On 19 January 2013 03:38, Sean P. DeNigris <[hidden email]> wrote:
> gerard wrote
>> Hi.
>>
>> Excuse me for my poor english.
>>
>> I need, for recursive requirements, call a block inside the same block.
>> That's possible on Smalltalk?
>>

Integer>>benchFib

>> For example:
>>
>> "fibonacci function"
>>  [:fnc :n1 :n2 :limit :numbers | numbers add: n1. (n2 < limit) ifTrue: [
>> fnc valueWithArguments: {fnc . n2 . (n1 + n2) . limit . numbers}. numbers
>> ]].
>>
>> The objetive is use that block without the :fnc parameter.

[: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 thiscontext.

>>
>>
>> Regards
>







--
Best regards,
Igor Stasenko.

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Call a block inside same block for recursive requirements

Benjamin Van Ryseghem (Pharo)
Writing this

block := [:val |
        val >0
                ifTrue: [
                        self logCr: val.
                        block value: val -1 ]
                ifFalse: [ 'foo' ]].

block value: 3

works in a workspace :)

Ben

On Jan 19, 2013, at 8:56 PM, Igor Stasenko wrote:

> On 19 January 2013 03:38, Sean P. DeNigris <[hidden email]> wrote:
>> gerard wrote
>>> Hi.
>>>
>>> Excuse me for my poor english.
>>>
>>> I need, for recursive requirements, call a block inside the same block.
>>> That's possible on Smalltalk?
>>>
>
> Integer>>benchFib
>
>>> For example:
>>>
>>> "fibonacci function"
>>> [:fnc :n1 :n2 :limit :numbers | numbers add: n1. (n2 < limit) ifTrue: [
>>> fnc valueWithArguments: {fnc . n2 . (n1 + n2) . limit . numbers}. numbers
>>> ]].
>>>
>>> The objetive is use that block without the :fnc parameter.
>
> [: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 thiscontext.
>
>>>
>>>
>>> Regards
>>
>
>
>
>
>
>
>
> --
> Best regards,
> Igor Stasenko.
>


Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Call a block inside same block for recursive requirements

Igor Stasenko
On 19 January 2013 21:04, Benjamin <[hidden email]> wrote:

> Writing this
>
> block := [:val |
>         val >0
>                 ifTrue: [
>                         self logCr: val.
>                         block value: val -1 ]
>                 ifFalse: [ 'foo' ]].
>
> block value: 3
>
> works in a workspace :)

well, this is the most trivial example. :)
(frankly i didn't even thought about it as a case, because one of the
requirements
is to not refer to the block via variable)..

in this case, you still referring to it, but just moved variable
declaration outside of the block scope.
But of course, this is most usable form (apart from just using method).

>
> Ben
>
> On Jan 19, 2013, at 8:56 PM, Igor Stasenko wrote:
>
>> On 19 January 2013 03:38, Sean P. DeNigris <[hidden email]> wrote:
>>> gerard wrote
>>>> Hi.
>>>>
>>>> Excuse me for my poor english.
>>>>
>>>> I need, for recursive requirements, call a block inside the same block.
>>>> That's possible on Smalltalk?
>>>>
>>
>> Integer>>benchFib
>>
>>>> For example:
>>>>
>>>> "fibonacci function"
>>>> [:fnc :n1 :n2 :limit :numbers | numbers add: n1. (n2 < limit) ifTrue: [
>>>> fnc valueWithArguments: {fnc . n2 . (n1 + n2) . limit . numbers}. numbers
>>>> ]].
>>>>
>>>> The objetive is use that block without the :fnc parameter.
>>
>> [: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 thiscontext.
>>
>>>>
>>>>
>>>> Regards
>>>
>>
>>
>>
>>
>>
>>
>>
>> --
>> Best regards,
>> Igor Stasenko.
>>
>
>



--
Best regards,
Igor Stasenko.

Reply | Threaded
Open this post in threaded view
|

Re: Fwd: Call a block inside same block for recursive requirements

Sven Van Caekenberghe-2
In reply to this post by Igor Stasenko

On 19 Jan 2013, at 20:56, Igor Stasenko <[hidden email]> 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