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
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk

Reply via email to