Module: Divergent::Try
Overview
The ‘Try` type represents a computation that may either result in an exception, or return a successfully computed value. It’s similar to, but semantically different from Either.
Instances of Try, are either an instance of Success or Failure.
For example, ‘Try` can be used to perform division on a user-defined input, without the need to do explicit exception-handling in all of the places that an exception might occur.
Class Method Summary collapse
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Applies the given block if this is a ‘Success`, otherwise returns `Unit` if this is a `Failure`.
-
#failed ⇒ Object
Inverts this ‘Try`.
-
#failure? ⇒ Boolean
Returns ‘true` if the `Try` is a `Failure`, `false` otherwise.
-
#filter(&block) ⇒ Object
Converts this to a ‘Failure` if the predicate is not satisfied.
-
#flatten ⇒ Object
Transforms a nested ‘Try` into an un-nested `Try`.
-
#get ⇒ Object
Returns the value from this ‘Success` or throws the exception if this is a `Failure`.
-
#get_or_else(default) ⇒ Object
Returns the value from this ‘Success` or the given `default` argument if this is a `Failure`.
-
#map(&block) ⇒ Object
Maps the given function to the value from this ‘Success` or returns this if this is a `Failure`.
-
#or_else(default) ⇒ Object
Returns this ‘Try` if it’s a ‘Success` or the given `default` argument if this is a `Failure`.
-
#recover(*errors, &block) ⇒ Try
Recover the given error classes by applying the block, and leave other error case unrecoverd.
-
#recover_with(*errors, &block) ⇒ Try
Applies the given block if this is a ‘Failure`, otherwise returns this if this is a `Success`.
-
#success? ⇒ Boolean
Returns ‘true` if the `Try` is a `Success`, `false` otherwise.
-
#transform(s, f) ⇒ Object
Completes this ‘Try` by applying the function `f` to this if this is of type `Failure`, or conversely, by applying `s` if this is a `Success`.
Methods included from Monad
Class Method Details
Instance Method Details
#each(&block) ⇒ Object
Applies the given block if this is a ‘Success`, otherwise returns `Unit` if this is a `Failure`.
Notes:
If block throws, then this method may throw an exception.
69 70 71 |
# File 'lib/divergent/try.rb', line 69 def each(&block) raise NotImplementedError end |
#failed ⇒ Object
Inverts this ‘Try`. If this is a `Failure`, returns its exception wrapped in a `Success`. If this is a `Success`, returns a `Failure` containing an UnSupportedOperationError.
121 122 123 |
# File 'lib/divergent/try.rb', line 121 def failed raise NotImplementedError end |
#failure? ⇒ Boolean
Returns ‘true` if the `Try` is a `Failure`, `false` otherwise.
25 26 27 |
# File 'lib/divergent/try.rb', line 25 def failure? raise NotImplementedError end |
#filter(&block) ⇒ Object
Converts this to a ‘Failure` if the predicate is not satisfied.
81 82 83 |
# File 'lib/divergent/try.rb', line 81 def filter(&block) raise NotImplementedError end |
#flatten ⇒ Object
Transforms a nested ‘Try` into an un-nested `Try`.
114 115 116 |
# File 'lib/divergent/try.rb', line 114 def flatten raise NotImplementedError end |
#get ⇒ Object
Returns the value from this ‘Success` or throws the exception if this is a `Failure`.
58 59 60 |
# File 'lib/divergent/try.rb', line 58 def get raise NotImplementedError end |
#get_or_else(default) ⇒ Object
Returns the value from this ‘Success` or the given `default` argument if this is a `Failure`.
36 37 38 39 40 41 42 |
# File 'lib/divergent/try.rb', line 36 def get_or_else(default) if success? get else default end end |
#map(&block) ⇒ Object
Maps the given function to the value from this ‘Success` or returns this if this is a `Failure`.
76 77 78 |
# File 'lib/divergent/try.rb', line 76 def map(&block) raise NotImplementedError end |
#or_else(default) ⇒ Object
Returns this ‘Try` if it’s a ‘Success` or the given `default` argument if this is a `Failure`.
Notes: the ‘default` value should be an instance of Try.
48 49 50 51 52 53 54 |
# File 'lib/divergent/try.rb', line 48 def or_else(default) if success? self else default end end |
#recover(*errors, &block) ⇒ Try
Recover the given error classes by applying the block, and leave other error case unrecoverd. If no class is given, recover it anyway. The error instance(if this is a Failure) will be passed into the block. And, error raised from the block call will not rescued by it! Otherwise returns this if this is a ‘Success`.
This is like ‘map` for the exception.
109 110 111 |
# File 'lib/divergent/try.rb', line 109 def recover(*errors, &block) raise NotImplementedError end |
#recover_with(*errors, &block) ⇒ Try
Applies the given block if this is a ‘Failure`, otherwise returns this if this is a `Success`.
Notes: block call should return an instance of Try. And, error raised from the block call will not rescued by it! This is like ‘fmap` for the exception.
94 95 96 |
# File 'lib/divergent/try.rb', line 94 def recover_with(*errors, &block) raise NotImplementedError end |
#success? ⇒ Boolean
Returns ‘true` if the `Try` is a `Success`, `false` otherwise.
30 31 32 |
# File 'lib/divergent/try.rb', line 30 def success? raise NotImplementedError end |
#transform(s, f) ⇒ Object
Completes this ‘Try` by applying the function `f` to this if this is of type `Failure`, or conversely, by applying `s` if this is a `Success`.
128 129 130 |
# File 'lib/divergent/try.rb', line 128 def transform(s, f) raise NotImplementedError end |