Class: Divergent::Maybe
- Inherits:
-
Object
- Object
- Divergent::Maybe
- Includes:
- Monad
- Defined in:
- lib/divergent/maybe.rb
Overview
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`.
Direct Known Subclasses
Class Method Summary collapse
-
.empty ⇒ Object
An Maybe factory which return None.
-
.unit(v) ⇒ Object
An factory which creates Some(v) if the argument is not nil, and None if it is nil.
Instance Method Summary collapse
-
#all? ⇒ Boolean
Return true if it is empty or the predicate block evals to true when applying to the maybe’s value.
-
#any? ⇒ Boolean
Return true if it is not empty and the predicate block evals to true.
-
#each {|get| ... } ⇒ Object
Apply the given block to the maybe’s value if not empty.
-
#empty? ⇒ Boolean
Return true if the maybe is None, false otherwise.
-
#filter ⇒ Object
Return this maybe if it is not empty and the predicate block evals to true.
-
#fmap ⇒ Object
Returns the result of applying block to this Maybe value, if this value is not empty.
-
#get ⇒ Object
Return the maybe’s value.
-
#get_or_else(v) ⇒ Object
Returns the maybe’s value if the maybe is nonempty, otherwise return the ‘v`.
-
#include?(elem) ⇒ Boolean
Test whether the maybe contains a given elem.
-
#map ⇒ Object
Notes: This is similar to
flat_map
except here, block does not need to wrap its result in a maybe. -
#or_else(v) ⇒ Object
Return this maybe id it is not empty, else return the ‘v`.
-
#to_a ⇒ Object
Returns a singleton list containing the maybe’s value if it is nonempty, or the empty list if empty.
Methods included from Monad
Class Method Details
Instance Method Details
#all? ⇒ Boolean
173 174 175 |
# File 'lib/divergent/maybe.rb', line 173 def all?() # :yields: v empty? || yield(get) end |
#any? ⇒ Boolean
158 159 160 |
# File 'lib/divergent/maybe.rb', line 158 def any?() # :yields: v !empty? && yield(get) end |
#each {|get| ... } ⇒ Object
Apply the given block to the maybe’s value if not empty. Otherwise, do nothing.
179 180 181 |
# File 'lib/divergent/maybe.rb', line 179 def each() # :yields: v yield(get) unless empty? end |
#empty? ⇒ Boolean
Return true if the maybe is None, false otherwise.
71 72 73 |
# File 'lib/divergent/maybe.rb', line 71 def empty? raise NotImplementedError end |
#filter ⇒ Object
Return this maybe if it is not empty and the predicate block evals to true. Otherwise, return None.
128 129 130 131 132 |
# File 'lib/divergent/maybe.rb', line 128 def filter() # :yields: v fmap do |v| yield(v) ? Maybe.unit(v) : None end end |
#fmap ⇒ Object
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
61 62 63 64 65 66 67 |
# File 'lib/divergent/maybe.rb', line 61 def fmap() # :yields: v if empty? None else yield(get) end end |
#get ⇒ Object
Return the maybe’s value.
Notes: the maybe should not be empty.
otherwise, raise Standarderror.
82 83 84 |
# File 'lib/divergent/maybe.rb', line 82 def get raise NotImplementedError end |
#get_or_else(v) ⇒ Object
Returns the maybe’s value if the maybe is nonempty, otherwise return the ‘v`.
89 90 91 92 93 94 95 |
# File 'lib/divergent/maybe.rb', line 89 def get_or_else(v) if empty? v else get end end |
#include?(elem) ⇒ Boolean
144 145 146 |
# File 'lib/divergent/maybe.rb', line 144 def include?(elem) !empty? && get == elem end |
#map ⇒ Object
Notes: This is similar to flat_map
except here, block does not need to wrap its result in a maybe.
120 121 122 |
# File 'lib/divergent/maybe.rb', line 120 def map() # :yields: v fmap { |v| Maybe.unit(yield v) } end |
#or_else(v) ⇒ Object
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.
104 105 106 107 108 109 110 |
# File 'lib/divergent/maybe.rb', line 104 def or_else(v) if empty? v else self end end |
#to_a ⇒ Object
Returns a singleton list containing the maybe’s value
if it is nonempty, or the empty list if empty.
185 186 187 188 189 190 191 |
# File 'lib/divergent/maybe.rb', line 185 def to_a if empty? [] else [get] end end |