module Divergent::Try
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.
Public Class Methods
# File lib/divergent/try.rb, line 18 def self.unit() Success.new(yield) rescue => e Failure.new(e) end
Public Instance Methods
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.
# File lib/divergent/try.rb, line 69 def each(&block) raise NotImplementedError end
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
.
# File lib/divergent/try.rb, line 121 def failed raise NotImplementedError end
Returns `true` if the `Try` is a `Failure`, `false` otherwise.
# File lib/divergent/try.rb, line 25 def failure? raise NotImplementedError end
Converts this to a `Failure` if the predicate is not satisfied.
# File lib/divergent/try.rb, line 81 def filter(&block) raise NotImplementedError end
Transforms a nested `Try` into an un-nested `Try`.
# File lib/divergent/try.rb, line 114 def flatten raise NotImplementedError end
Returns the value from this `Success` or throws the exception if this is a `Failure`.
# File lib/divergent/try.rb, line 58 def get raise NotImplementedError end
Returns the value from this `Success` or the given `default` argument if this is a `Failure`.
# File lib/divergent/try.rb, line 36 def get_or_else(default) if success? get else default end end
Maps the given function to the value from this `Success` or returns this if this is a `Failure`.
# File lib/divergent/try.rb, line 76 def map(&block) raise NotImplementedError end
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
.
# File lib/divergent/try.rb, line 48 def or_else(default) if success? self else default end end
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. @param [Array] errors @param [Block] blk @return [Try]
# File lib/divergent/try.rb, line 109 def recover(*errors, &block) raise NotImplementedError end
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. @param [Array] errors @param [Block] blk, this blk should return an instance of Try
@return [Try] block call result
# File lib/divergent/try.rb, line 94 def recover_with(*errors, &block) raise NotImplementedError end
Returns `true` if the `Try` is a `Success`, `false` otherwise.
# File lib/divergent/try.rb, line 30 def success? raise NotImplementedError end
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`.
# File lib/divergent/try.rb, line 128 def transform(s, f) raise NotImplementedError end