add try-find for random.windows

release
Doug Coleman 2010-01-28 22:44:31 -06:00
parent 6cdf4de4eb
commit 46f2515ba8
2 changed files with 285 additions and 274 deletions

View File

@ -48,6 +48,10 @@ $nl
recover
ignore-errors
}
"Word for mapping over a sequence with a quotation until an element doesn't throw an exception:"
{ $subsections
try-find
}
"Syntax sugar for defining errors:"
{ $subsections POSTPONE: ERROR: }
"Unhandled errors are reported in the listener and can be debugged using various tools. See " { $link "debugger" } "."
@ -181,6 +185,10 @@ HELP: recover
{ $values { "try" quotation } { "recovery" { $quotation "( error -- )" } } }
{ $description "Calls the " { $snippet "try" } " quotation. If an exception is thrown in the dynamic extent of the " { $snippet "try" } " quotation, restores the data stack and calls the " { $snippet "recovery" } " quotation to handle the error." } ;
HELP: try-find
{ $values { "seq" sequence } { "try" quotation } { "result" "the first non-false, non-exception result of the quotation" } { "elt" "the first matching element, or " { $link f } } }
{ $description "Applies the quotation to each element of the sequence, until the quotation outputs a true value, and attempts the next element if an exception is thrown. If the quotation ever yields a result which is not " { $link f } ", then the value is output, along with the element of the sequence which yielded this." } ;
HELP: ignore-errors
{ $values { "quot" quotation } }
{ $description "Calls the quotation. If an exception is thrown in the dynamic extent of the quotation, restores the data stack and returns." } ;

View File

@ -128,6 +128,9 @@ SYMBOL: thread-error-hook
: cleanup ( try cleanup-always cleanup-error -- )
[ compose [ dip rethrow ] curry recover ] [ drop ] 2bi call ; inline
: try-find ( seq try -- result elt )
[ curry [ drop f ] recover ] curry map-find ; inline
ERROR: attempt-all-error ;
: attempt-all ( seq quot -- obj )