move retry word to continuations

db4
Doug Coleman 2008-12-03 08:32:12 -06:00
parent 3d17175926
commit a329960d7e
2 changed files with 17 additions and 0 deletions

View File

@ -79,6 +79,7 @@ $nl
{ $subsection continue-with }
"Continuations as control-flow:"
{ $subsection attempt-all }
{ $subsection retry }
{ $subsection with-return }
"Reflecting the datastack:"
{ $subsection with-datastack }
@ -237,6 +238,20 @@ HELP: attempt-all
}
} ;
HELP: retry
{ $values
{ "quot" quotation } { "n" null }
}
{ $description "Tries the quotation up to " { $snippet "n" } " times until it returns true. Retries the quotation if an exception is thrown or if the quotation returns " { $link f } ". The quotation is expected to have side effects that may fail, such as generating a random name for a new file until successful." }
{ $examples
{ $unchecked-example "USING: continuations math prettyprint ;"
"[ 5 random 0 = ] retry t"
"t"
}
} ;
{ attempt-all retry } related-words
HELP: return
{ $description "Returns early from a quotation by reifying the continuation captured by " { $link with-return } " ; execution is resumed starting immediately after " { $link with-return } "." } ;

View File

@ -154,6 +154,8 @@ ERROR: attempt-all-error ;
] { } make peek swap [ rethrow ] when
] if ; inline
: retry ( quot: ( -- ? ) n -- ) swap [ drop ] prepose attempt-all ; inline
TUPLE: condition error restarts continuation ;
C: <condition> condition ( error restarts cc -- condition )