Class: Maybe
- Inherits:
-
Object
- Object
- Maybe
- Defined in:
- lib/indubitably.rb
Overview
base class for Some and None
Constant Summary collapse
- @@none =
nil
Class Method Summary collapse
- .concat(list, default = none) ⇒ Object
- .empty_value?(value) ⇒ Boolean
- .join?(value) ⇒ Boolean
- .none ⇒ Object
- .seq(list, default = none) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #or_nil ⇒ Object
- #to_ary ⇒ Object (also: #to_a)
Class Method Details
.concat(list, default = none) ⇒ Object
27 28 29 30 31 32 33 |
# File 'lib/indubitably.rb', line 27 def self.concat(list, default = none) if default == none list.select(&:is_some?).map(&:get) else list.map { |x| x.or_else(default) } end end |
.empty_value?(value) ⇒ Boolean
35 36 37 |
# File 'lib/indubitably.rb', line 35 def self.empty_value?(value) value.nil? || (value.respond_to?(:length) && value.length == 0) end |
.join?(value) ⇒ Boolean
39 40 41 42 |
# File 'lib/indubitably.rb', line 39 def self.join?(value) return value if value.is_a?(Maybe) Maybe(value) end |
.none ⇒ Object
44 45 46 |
# File 'lib/indubitably.rb', line 44 def self.none @@none ||= None.new end |
.seq(list, default = none) ⇒ Object
48 49 50 |
# File 'lib/indubitably.rb', line 48 def self.seq(list, default = none) Maybe(concat(list, default)) end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
18 19 20 |
# File 'lib/indubitably.rb', line 18 def ==(other) other.class == self.class end |
#or_nil ⇒ Object
23 24 25 |
# File 'lib/indubitably.rb', line 23 def or_nil or_else(nil) end |
#to_ary ⇒ Object Also known as: to_a
13 14 15 |
# File 'lib/indubitably.rb', line 13 def to_ary __enumerable_value end |