]> gitweb.factorcode.org Git - factor.git/commitdiff
promises: fix for syntax change and simplify a little bit
authorSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Mon, 23 Mar 2009 02:42:01 +0000 (21:42 -0500)
committerSlava Pestov <slava@slava-pestovs-macbook-pro.local>
Mon, 23 Mar 2009 02:42:01 +0000 (21:42 -0500)
extra/promises/promises-docs.factor
extra/promises/promises.factor

index 4e8dc9a9a2ed5201673463c7400b4fe0897b4312..d416842ef50d7ece518efdac84c6a29d67fe03f2 100755 (executable)
@@ -1,34 +1,20 @@
 ! Copyright (C) 2006 Chris Double.
 ! See http://factorcode.org/license.txt for BSD license.
-
 USING: help.markup help.syntax ;
 IN: promises
 
 HELP: promise 
 { $values { "quot" { $quotation "( -- 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 promise-with promise-with2 } ;
-
-HELP: promise-with
-{ $values { "value" "an object" } { "quot" { $quotation "( 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" { $quotation "( 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 } ;
+{ $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." } ;
 
 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 promise-with promise-with2 } ;
+{ $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." } ;
 
 HELP: LAZY:
-{ $syntax "LAZY: word definition... ;" } 
+{ $syntax "LAZY: word ( stack -- effect ) definition... ;" } 
 { $values { "word" "a new word to define" } { "definition" "a word definition" } }
 { $description "Creates a lazy word in the current vocabulary. When executed the word will return a " { $link promise } " that when forced, executes the word definition. Any values on the stack that are required by the word definition are captured along with the promise." } 
 { $examples
   { $example "USING: arrays sequences prettyprint promises ;" "IN: scratchpad" "LAZY: zeroes ( -- pair ) 0 zeroes 2array ;" "zeroes force second force first ." "0" }
-}
-{ $see-also force promise-with promise-with2 } ;
+} ;
index 60b4418c3f36de6394a7ce290eece9eb983f025e..c3951f46ba60d927a0e9556684d53f4e41ecf9d6 100755 (executable)
@@ -1,41 +1,22 @@
 ! Copyright (C) 2004, 2006 Chris Double, Matthew Willis.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: arrays kernel sequences math vectors arrays namespaces
-make quotations parser effects stack-checker words accessors ;
+USING: arrays kernel sequences math arrays namespaces
+parser effects generalizations fry words accessors ;
 IN: promises
 
 TUPLE: promise quot forced? value ;
 
-: promise ( quot -- promise )
-  f f \ promise boa ;
-
-: promise-with ( value quot -- promise )
-  curry promise ;
-
-: promise-with2 ( value1 value2 quot -- promise )
-  2curry promise ;
+: promise ( quot -- promise ) f f \ promise boa ;
 
 : force ( promise -- value )
-    #! Force the given promise leaving the value of calling the
-    #! promises quotation on the stack. Re-forcing the promise
-    #! will return the same value and not recall the quotation.
     dup forced?>> [
         dup quot>> call( -- value ) >>value
         t >>forced?
     ] unless
     value>> ;
 
-: stack-effect-in ( quot word -- n )
-  stack-effect [ ] [ infer ] ?if in>> length ;
-
-: make-lazy-quot ( word quot -- quot )
-  [
-    dup ,
-    swap stack-effect-in \ curry <repetition> % 
-    \ promise ,
-  ] [ ] make ;
+: make-lazy-quot ( quot effect -- quot )
+    in>> length '[ _ _ ncurry promise ] ;
 
 SYNTAX: LAZY:
-  CREATE-WORD
-  dup parse-definition
-  make-lazy-quot define ;
+    (:) [ make-lazy-quot ] [ 2nip ] 3bi define-declared ;