]> gitweb.factorcode.org Git - factor.git/blob - extra/promises/promises.factor
5d63406e78db4172cd592d08b2f81a6a9698d082
[factor.git] / extra / promises / promises.factor
1 ! Copyright (C) 2004 Chris Double.
2 ! See http://factorcode.org/license.txt for BSD license.
3 !
4 ! Updated by Matthew Willis, July 2006
5 ! Updated by Chris Double, September 2006
6
7 USING: arrays kernel sequences math vectors arrays namespaces
8 quotations parser effects stack-checker words accessors ;
9 IN: promises
10
11 TUPLE: promise quot forced? value ;
12
13 : promise ( quot -- promise )
14   f f \ promise boa ;
15
16 : promise-with ( value quot -- promise )
17   curry promise ;
18
19 : promise-with2 ( value1 value2 quot -- promise )
20   2curry promise ;
21
22 : force ( promise -- value )
23     #! Force the given promise leaving the value of calling the
24     #! promises quotation on the stack. Re-forcing the promise
25     #! will return the same value and not recall the quotation.
26     dup forced?>> [
27         dup quot>> call >>value
28         t >>forced?
29     ] unless
30     value>> ;
31
32 : stack-effect-in ( quot word -- n )
33   stack-effect [ ] [ infer ] ?if in>> length ;
34
35 : make-lazy-quot ( word quot -- quot )
36   [
37     dup ,
38     swap stack-effect-in \ curry <repetition> % 
39     \ promise ,
40   ] [ ] make ;
41
42 : LAZY:
43   CREATE-WORD
44   dup parse-definition
45   make-lazy-quot define ; parsing