class Divergent::Maybe

Optional values.

An instance of Some or the object None.

The most idiomatic way to use an Maybe instance is to treat it as a collection or monad and use `map`, `fmap`, `filter`, or `each`.

Public Class Methods

empty() click to toggle source

An Maybe factory which return None.

This always return the same None object.

Example:

Maybe.empty == Maybe.empty
# File lib/divergent/maybe.rb, line 43
def self.empty
  None
end
unit(v) click to toggle source

An factory which creates Some(v) if the argument is not nil, and None if it is nil.

Examples:

Maybe.unit(1)   # => Some(1)
Maybe.unit(nil) # => None
# File lib/divergent/maybe.rb, line 25
def self.unit(v)
  if v.nil?
    None
  else
    Some.new(v)
  end
end

Public Instance Methods

all?() { |v| ... } click to toggle source

Return true if it is empty or the predicate block evals to true when applying to the maybe's value.

Examples:

Maybe.unit(1).all?{ |v| v == 1} #=> true
Maybe.unit(1).all?{ |v| v == 2} #=> false
Maybe.empty.all?{ |v| v == 1} #=> true
# File lib/divergent/maybe.rb, line 173
def all?() # :yields: v
  empty? || yield(get)
end
any?() { |v| ... } click to toggle source

Return true if it is not empty and the predicate block evals to true.

Examples:

Maybe.unit(1).any?{ |v| v == 1} #=> true
Maybe.unit(1).any?{ |v| v == 2} #=> false
Maybe.empty.any?{ |v| v == 1} #=> false
# File lib/divergent/maybe.rb, line 158
def any?() # :yields: v
  !empty? && yield(get)
end
each() { |v| ... } click to toggle source

Apply the given block to the maybe's value if not empty. Otherwise, do nothing.

# File lib/divergent/maybe.rb, line 179
def each() # :yields: v
  yield(get) unless empty?
end
empty?() click to toggle source

Return true if the maybe is None, false otherwise.

# File lib/divergent/maybe.rb, line 71
def empty?
  raise NotImplementedError
end
filter() { |v| ... } click to toggle source

Return this maybe if it is not empty and the predicate block evals to true. Otherwise, return None.

# File lib/divergent/maybe.rb, line 128
def filter() # :yields: v
  fmap do |v|
    yield(v) ? Maybe.unit(v) : None
  end
end
fmap() { |v| ... } click to toggle source

Returns the result of applying block to this Maybe value, if this value is not empty. Return `None` if this value is empty.

Slightly different from `map` in that block is expected to return an instance of `Maybe`.

Examples

Maybe.unit(1).fmap { |v| Maybe.unit(v + 1) } # => Some(2)
some_hash = {}
Maybe.unit(:a).fmap { |v| Maybe.unit(some_hash[v]) } # => None
# File lib/divergent/maybe.rb, line 61
def fmap() # :yields: v
  if empty?
    None
  else
    yield(get)
  end
end
get() click to toggle source

Return the maybe's value.

Notes: the maybe should not be empty.

otherwise, raise Standarderror.

# File lib/divergent/maybe.rb, line 82
def get
  raise NotImplementedError
end
get_or_else(v) click to toggle source

Returns the maybe's value if the maybe is nonempty, otherwise return the `v`.

# File lib/divergent/maybe.rb, line 89
def get_or_else(v)
  if empty?
    v
  else
    get
  end
end
include?(elem) click to toggle source

Test whether the maybe contains a given elem.

Examples:

Maybe.unit(1).include?(1) #=> true
Maybe.unit(1).include?(2) #=> false
Maybe.empty.include?(1) #=> false
# File lib/divergent/maybe.rb, line 144
def include?(elem)
  !empty? && get == elem
end
map() { |v| ... } click to toggle source

Notes: This is similar to flat_map except here, block does not need to wrap its result in a maybe.

# File lib/divergent/maybe.rb, line 120
def map() # :yields: v
  fmap { |v| Maybe.unit(yield v) }
end
or_else(v) click to toggle source

Return this maybe id it is not empty, else return the `v`.

Note: This is similar to Maybe#get_or_else, but the v should be an instance of Maybe.

# File lib/divergent/maybe.rb, line 104
def or_else(v)
  if empty?
    v
  else
    self
  end
end
to_a() click to toggle source

Returns a singleton list containing the maybe's value if it is nonempty, or the empty list if empty.

# File lib/divergent/maybe.rb, line 185
def to_a
  if empty?
    []
  else
    [get]
  end
end