1 Try Library
| #:extra-constructor-name make-Try-Failure)|
| exception : exn?|
A computation that has failed with an exception.
| #:extra-constructor-name make-Try-Success)|
| result : T|
A computation that has successfully returned some value.
The result of a computation that either successfully returned some value or has failed with a thrown exception.
Is the value of a Try type a Try-Failure. The computation failed with
Is the value of a Try type a Try-Success. The computuation successfully
completed returning a result value.
Gets the value of a Try if the Try succeeded otherwise throws the Try’s failure exception.
Apply the provided function to a Try’s success value otherwise do nothing.
If the application fails the exception is the new value of the Try.
Apply the function to the value of a successful try, otherwise do nothing.
Apply the appropriate function to the Try. The first function if the Try was successful.
The second function of the Try failed. Always be one or the other of the two functions is applied.
If the Try’s success value matches the provided predicate function simply return the Try.
If the Try’s success value fails the predicate return a Try-Failure with an exception indicating
the predicate did not match.
If the Try was already failed then simply return the Try.
Does the value of a successful Try match the provided predicate?
A failed Try never matches the predicate.
If the Try was successful return a failed try with a synthetic exception of exn:fail.
If the Try was failed then return the Try as a success where the result value is the failed exception.
Apply either the provided on-success procedure or the on-failure procedure to the given Try.
The value returned is of the union type of the return type of provided two procedures.
One procedure, either the success procedure or failure procedure is always applied.
Rescue a failed computation if the rescue function is capable of doing so.
If the provided Try is a successful Try value then simply return this Try.
Otherwise we have a failed Try:
If the rescue procedure does return a success Try value for the given failed Try then return that success.
If the rescure procedure does not return a success Try value for the given failed Try then simply return the original failed Try.
Similar to the try-rescue
procedure except the difference in the type signature of the recove/rescue procedure.
try : (All (T) (-> T) -> (Try T))
Evaluate the given closure in a Try that captures the closure successfully returning a value or failing with some exception.
The closure’s success return value is returned as a successful Try value.
The closure’s thrown exception is caught and returned as a failed Try value.
Evaluate a block of statements/expression in a Try context catching any exceptions or returning as successful the value of the last expression.
Converts a Try to an Option. A successful Try value becomes an Option value.
A failed Try value simply becomes a #f Option value.
Note that Typed Racket’s inference may need some hinting.
|(: scale-and-bump (-> Integer Integer Exact-Rational))|
|(define (scale-and-bump scale incr)|
| (try-get ((inst try-map Exact-Rational Exact-Rational)|
| (try-recover (with-try (/ 100 scale)) (λ (ex) 0))|
| (λ ((x : Exact-Rational)) (+ x incr)))))|
|(scale-and-bump 2 1)|
|- : Exact-Rational|
|(scale-and-bump 0 1)|
|- : Exact-Rational|