C: promise ( quot -- promise ) [ set-promise-quot ] keep ;
+: promise ( quot -- promise )
+ <promise> ;
+
+: promise-with ( value quot -- promise )
+ curry <promise> ;
+
+: promise-with2 ( value1 value2 quot -- promise )
+ curry curry <promise> ;
+
: force ( promise -- value )
#! Force the given promise leaving the value of calling the
#! promises quotation on the stack. Re-forcing the promise
TUPLE: lazy-cons car cdr ;
: lazy-cons ( car cdr -- promise )
- >r <promise> r> <promise> <lazy-cons>
+ >r promise r> promise <lazy-cons>
T{ promise f f t f } clone [ set-promise-value ] keep ;
M: lazy-cons car ( lazy-cons -- car )
USING: help lazy-lists sequences ;
-HELP: <promise>
+HELP: promise
{ $values { "quot" "a quotation with stack effect ( -- X )" } { "promise" "a promise object" } }
{ $description "Creates a promise to return a value. When forced this quotation is called and the value returned. The value is memorised so that calling " { $link force } " again does not call the quotation again, instead the previous value is returned directly." }
-{ $see-also force } ;
+{ $see-also force promise-with promise-with2 } ;
+
+HELP: promise-with
+{ $values { "value" "an object" } { "quot" "a quotation with stack effect ( value -- X )" } { "promise" "a promise object" } }
+{ $description "Creates a promise to return a value. When forced this quotation is called with the given value on the stack and the result returned. The value is memorised so that calling " { $link force } " again does not call the quotation again, instead the previous value is returned directly." }
+{ $see-also force promise promise-with2 } ;
+
+HELP: promise-with2
+{ $values { "value1" "an object" } { "value2" "an object" } { "quot" "a quotation with stack effect ( value1 value2 -- X )" } { "promise" "a promise object" } }
+{ $description "Creates a promise to return a value. When forced this quotation is called with the given values on the stack and the result returned. The value is memorised so that calling " { $link force } " again does not call the quotation again, instead the previous value is returned directly." }
+{ $see-also force promise promise-with2 } ;
HELP: force
{ $values { "promise" "a promise object" } { "value" "a factor object" } }
{ $description "Calls the quotation associated with the promise if it has not been called before, and returns the value. If the promise has been forced previously, returns the value from the previous call." }
-{ $see-also <promise> } ;
+{ $see-also promise promise-with promise-with2 } ;
HELP: <cons>
{ $values { "car" "the head of the lazy list" } { "cdr" "the tail of the lazy list" } { "cons" "a cons object" } }