]> gitweb.factorcode.org Git - factor.git/commitdiff
lazy-lists: add promise, promise-with and promise-with2
authorchris.double <chris.double@double.co.nz>
Fri, 6 Oct 2006 03:12:29 +0000 (03:12 +0000)
committerchris.double <chris.double@double.co.nz>
Fri, 6 Oct 2006 03:12:29 +0000 (03:12 +0000)
contrib/lazy-lists/lists.factor
contrib/lazy-lists/lists.facts

index e9e53ee9b7affd1370006ec3fc89b1603e885b62..99bc70db8e426fbe570f5ac9cc0db7dd17bdecb9 100644 (file)
@@ -17,6 +17,15 @@ TUPLE: promise quot forced? value ;
 
 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
@@ -79,7 +88,7 @@ M: cons list? ( object -- bool )
 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 )
index 69bf9697b9cd146eea76dfa46b88d0ebad5c301a..c7b4d4de7dfc4cf1283b889f1ae98319c96f2680 100644 (file)
@@ -3,15 +3,25 @@
 
 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" } }