: random-name ( -- string )
unique-length get [ random-ch ] "" replicate-as ;
+: retry ( quot: ( -- ? ) n -- )
+ swap [ drop ] prepose attempt-all ; inline
+
: (make-unique-file) ( path prefix suffix -- path )
'[
_ _ _ random-name glue append-path
{ $subsection continue-with }
"Continuations as control-flow:"
{ $subsection attempt-all }
-{ $subsection retry }
{ $subsection with-return }
"Continuations serve as the building block for a number of higher-level abstractions, such as " { $link "errors" } " and " { $link "threads" } "."
{ $subsection "continuations.private" } ;
}
} ;
-HELP: retry
-{ $values
- { "quot" quotation } { "n" integer }
-}
-{ $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
- "Try to get a 0 as a random number:"
- { $unchecked-example "USING: continuations math prettyprint random ;"
- "[ 5 random 0 = ] 5 retry"
- "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 } "." } ;
] { } 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 )
: random-url ( -- string )
1 6 [a,b] random [ letter-bank random ] "" replicate-as ;
+: retry ( quot: ( -- ? ) n -- )
+ swap [ drop ] prepose attempt-all ; inline
+
: insert-short-url ( short-url -- short-url )
'[ _ dup random-url >>short insert-tuple ] 10 retry ;