class Concurrent::Maybe
@see github.com/purescript/purescript-maybe/blob/master/docs/Data.Maybe.md PureScript Data.Maybe
@see hackage.haskell.org/package/base-4.2.0.1/docs/Data-Maybe.html Haskell Data.Maybe
render json: result.or(NullClient.new)
result = ClientService.new(10).find # returns a Maybe
# In a Rails controller…
@example Using Maybe with the Null Object Pattern
result.reason #=> ActiveRecord::RecordNotFound
result.just? #=> false
# – if the record was not found
result.value #=> #<Client id: 10, first_name: “Ryan”>
result.just? #=> true
# – if the record was found
end
Client.find(10) # Client is an ActiveRecord model
result = Concurrent::Maybe.from do
@example Using Maybe with a Block
maybe.value #=> “# Concurrent Rubyn[<br>(erlang.org/doc/reference_manual/data_types.html#id69044)
In this example the standard library function `file:consult` returns a
“`
end.
{error, Reason} -> lager:error(Reason)
{ok, Terms} -> do_something_useful(Terms);
case file:consult(“data.dat”) of
“`erlang
Consider this Erlang code:
Haskell, is to return either a value or an error from a function
A common pattern in languages with pattern matching, such as Erlang and
## Motivation
`reason`.
accessor methods are aliased as `fulfilled?`, `rejected?`, `value`, and
For compatibility with {Concurrent::Concern::Obligation} the predicate and
`Maybe` is a replacement for the use of `nil` with better type checking.
resorting to drastic measures such as exceptions.
`Maybe` is a good way to deal with errors or exceptional cases without
of (represented as `Just`), or it is empty (represented as `Nothing`). Using
A `Maybe` encapsulates an optional value. A `Maybe` either contains a value
def self.from(*args)
-
(ArgumentError)
- when no block given.
Returns:
-
(Maybe)
- The newly created object.
Other tags:
- Yieldparam: args - Zero or more block arguments passed as
Other tags:
- Yield: - The block from which to create a new `Maybe`.
Parameters:
-
args
(Array
) -- Zero or more arguments to pass to the block.
def self.from(*args) raise ArgumentError.new('no block given') unless block_given? begin value = yield(*args) return new(value, NONE) rescue => ex return new(NONE, ex) end end
def self.just(value)
-
(Maybe)
- The newly created object.
Parameters:
-
value
(Object
) -- The value to set for the new `Maybe` object.
def self.just(value) return new(value, NONE) end
def self.nothing(error = '')
-
(Maybe)
- The newly created object.
Parameters:
-
error
(Exception
) -- The reason to set for the new `Maybe` object.
def self.nothing(error = '') if error.is_a?(Exception) nothing = error else nothing = StandardError.new(error.to_s) end return new(NONE, nothing) end
def <=>(other)
-
(Integer)
- 0 if self and other are both `Nothing`;
def <=>(other) if nothing? other.nothing? ? 0 : -1 else other.nothing? ? 1 : just <=> other.just end end
def initialize(just, nothing)
-
(Maybe)
- The new `Maybe`.
Parameters:
-
nothing
(Exception, Object
) -- The exception when `Nothing` else `NONE`. -
just
(Object
) -- The value when `Just` else `NONE`.
def initialize(just, nothing) @just = just @nothing = nothing end
def just?
-
(Boolean)
- True if `Just` or false if `Nothing`.
def just? ! nothing? end
def nothing?
-
(Boolean)
- True if `Nothing` or false if `Just`.
def nothing? @nothing != NONE end
def or(other)
-
(Object)
- The value of self when `Just`; else the given default.
def or(other) just? ? just : other end