Class: Maybe
Class Method Summary collapse
Instance Method Summary collapse
- #bind ⇒ Object
- #filter ⇒ Object
- #map ⇒ Object
- #or_else(other = nil) ⇒ Object
- #or_nil ⇒ Object
- #or_value(v = nil) ⇒ Object
Methods included from ADT
Class Method Details
.from_nil(v) ⇒ Object
34 35 36 |
# File 'lib/data/maybe.rb', line 34 def self.from_nil(v) v.nil? ? nothing : just(v) end |
Instance Method Details
#bind ⇒ Object
14 15 16 |
# File 'lib/data/maybe.rb', line 14 def bind fold(proc { self }, proc { |v| yield v }) end |
#filter ⇒ Object
18 19 20 |
# File 'lib/data/maybe.rb', line 18 def filter fold(proc { self }, proc { |v| yield(v) ? self : Maybe.nothing }) end |
#map ⇒ Object
10 11 12 |
# File 'lib/data/maybe.rb', line 10 def map fold(proc { self }, proc { |v| self.class.just(yield v) }) end |
#or_else(other = nil) ⇒ Object
22 23 24 |
# File 'lib/data/maybe.rb', line 22 def or_else(other = nil) fold(proc { other || yield }, proc { |v| self }) end |
#or_nil ⇒ Object
30 31 32 |
# File 'lib/data/maybe.rb', line 30 def or_nil fold(proc { nil }, proc { |v| v }) end |
#or_value(v = nil) ⇒ Object
26 27 28 |
# File 'lib/data/maybe.rb', line 26 def or_value(v = nil) fold(proc { v || yield }, proc { |v| v }) end |