Try
1 Try Library
(require try) | package: try |
struct
(struct Try-Failure (exception) #:extra-constructor-name make-Try-Failure) exception : exn?
A computation that has failed with an exception.
struct
(struct Try-Success (result) #:extra-constructor-name make-Try-Success) result : T
A computation that has successfully returned some value.
Type
Try : (U (Try-Failure exn?) (Try-Success T))
The result of a computation that either successfully returned some value or has failed with a thrown exception. }
Procedure
try-failure? : (All (T) (Try T) -> Boolean)
Procedure
try-success? : (All (T) (Try T) -> Boolean)
Procedure
try-flatmap : (All (T U) (Try T) (T -> (Try U)) -> (Try U))
Procedure
try-or-else : (All (T U) (Try T) (T -> U) (exn -> U) -> (Try U))
Procedure
try-filter : (All (T) (Try T) (T -> Boolean) -> (Try T))
Procedure
try-exists? : (All (T) (Try T) (T -> Boolean) -> Boolean)
Procedure
try-invert : (All (T) (Try T) -> (Try exn))
Procedure
: (All (T) (Try T) ((Try-Failure T) -> (Option (Try T))) -> (Try T))
Procedure
: (All (T) (Try T) ((Try-Failure T) -> (Option T)) -> (Try T))
Syntax
with-try : block...
(with-try (do-this) (do-that) (return-with-the-other))
Procedure
try->option : (All (T) (Try T) -> (Option T))
2 Examples
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 51 (scale-and-bump 0 1) - : Exact-Rational 1