concurrency: fix lazy evaluation

The 'lazy' word wasn't allowing multiple requests for the lazy value. It now caches the lazy value once it's computed and immediately returns it.
chris.double 2006-08-03 00:23:02 +00:00
parent cda21fa9a6
commit f1e9dc12e6
1 changed files with 4 additions and 10 deletions

View File

@ -423,22 +423,16 @@ C: promise ( -- <promise> )
! ******************************
! Experimental code below
! ******************************
SYMBOL: lazy-quot
: (lazy) ( v -- )
receive over reply (lazy) ;
: lazy ( quot -- lazy )
#! Spawn a process that immediately blocks and return it.
#! When '?lazy' is called on the returned process, call the quotation
#! and return the result. The quotation must have stack effect ( -- X ).
[
[
lazy-quot set
[
[ tagged-message? [ [ drop t ] [ get call ] send-reply ] ]
] recv
] with-scope
] curry spawn ;
[ receive >r call r> over reply (lazy) ] spawn nip ;
: ?lazy ( lazy -- result )
#! Given a process spawned using 'lazy', evaluate it and return the result.
lazy-quot swap send-synchronous ;
f swap send-synchronous ;