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