Why the variables? All you really need for closures is curry, and
possibly 2curry and 3curry. Here's your example { x } [ { y } [ x> y>
+ ] lambda ] lambda translated into that form: [ [ + ] curry ]. For an
example of use: 1 [ [ + ] curry ] call => [ 1 + ]; 1 2 [ [ + ] curry ]
call call => 3. It's much simpler and more readable and doesn't affect
the scope of the words that you call.
Dan
On 2/14/07, Eduardo Cavazos <[EMAIL PROTECTED]> wrote:
> Hello,
>
> Below is an implementation of closures with parameters. Some examples are
> below.
>
> The example transcript was made with my listener mod. So after each line of
> input, you'll see the output which is laid out like this:
>
> <<<output to stdio>>>
> ----------
> <<<data stack>>>
> ----------
> <<<retain stack>>>
>
> ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> scratchpad 20 10 { x } [ { y } [ x> y> + ] lambda ] lambda
> ----------
> 20
> 10
> T{ closure f # # # }
> ----------
> scratchpad call-closure
> ----------
> 20
> T{ closure f # # # }
> ----------
> scratchpad call-closure
> ----------
> 30
> ----------
> scratchpad
>
> ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> ! >>x is is a new vars convention and is defined as x set*.
> ! set* is a word that searches up the namestack for the first namespace that
> ! contains the variable and sets it there.
>
> scratchpad 10 { x } [ { } [ x> 1+ >>x x> ] lambda ] lambda call-closure
> ----------
> T{ closure f # # # }
> ----------
> scratchpad dup call-closure .
> 11
> ----------
> T{ closure f # # # }
> ----------
> scratchpad dup call-closure .
> 12
> ----------
> T{ closure f # # # }
> ----------
> scratchpad dup call-closure .
> 13
> ----------
> T{ closure f # # # }
> ----------
> scratchpad
>
> ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
> ! Implementation
> ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> USING: kernel namespaces sequences ;
>
> IN: closures
>
> ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
>
> TUPLE: closure namestack parameters quot ;
>
> : set-parameters ( seq -- ) reverse [ set ] each ;
>
> : call-closure ( closure -- )
> namestack >r
> dup closure-namestack set-namestack
> [ dup >r closure-parameters set-parameters r> closure-quot call ] with-scope
> r> set-namestack ;
>
> : lambda ( parameters quot -- closure ) namestack -rot <closure> ;
>
> -------------------------------------------------------------------------
> Take Surveys. Earn Cash. Influence the Future of IT
> Join SourceForge.net's Techsay panel and you'll get the chance to share your
> opinions on IT & business topics through brief surveys-and earn cash
> http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
> _______________________________________________
> Factor-talk mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/factor-talk
>
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Factor-talk mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/factor-talk