Class: Right
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#flat_map(fn = nil, &block) ⇒ Object
a function which returns an either.
- #fold(seed, fn_left, fn_right) ⇒ Object
-
#initialize(value) ⇒ Right
constructor
A new instance of Right.
- #is_left? ⇒ Boolean
- #is_right? ⇒ Boolean
- #left_value ⇒ Object
- #map(fn = nil, &block) ⇒ Object
- #map_left(fn = nil, &block) ⇒ Object
- #right_value ⇒ Object
- #to_s ⇒ Object
Methods inherited from Either
Constructor Details
#initialize(value) ⇒ Right
Returns a new instance of Right.
104 105 106 |
# File 'lib/totally_lazy/either.rb', line 104 def initialize(value) @value = value end |
Instance Method Details
#<=>(other) ⇒ Object
143 144 145 |
# File 'lib/totally_lazy/either.rb', line 143 def <=>(other) @value <=> other.right_value end |
#flat_map(fn = nil, &block) ⇒ Object
a function which returns an either
134 135 136 137 |
# File 'lib/totally_lazy/either.rb', line 134 def flat_map(fn=nil, &block) # a function which returns an either assert_funcs(fn, block_given?) block_given? ? block.call(@value) : fn.(@value) end |
#fold(seed, fn_left, fn_right) ⇒ Object
139 140 141 |
# File 'lib/totally_lazy/either.rb', line 139 def fold(seed, fn_left, fn_right) fn_right.(seed, @value) end |
#is_left? ⇒ Boolean
108 109 110 |
# File 'lib/totally_lazy/either.rb', line 108 def is_left? false end |
#is_right? ⇒ Boolean
112 113 114 |
# File 'lib/totally_lazy/either.rb', line 112 def is_right? true end |
#left_value ⇒ Object
116 117 118 |
# File 'lib/totally_lazy/either.rb', line 116 def left_value raise NoSuchElementException.new end |
#map(fn = nil, &block) ⇒ Object
124 125 126 127 |
# File 'lib/totally_lazy/either.rb', line 124 def map(fn=nil, &block) assert_funcs(fn, block_given?) right(block_given? ? block.call(@value) : fn.(@value)) end |
#map_left(fn = nil, &block) ⇒ Object
129 130 131 132 |
# File 'lib/totally_lazy/either.rb', line 129 def map_left(fn=nil, &block) assert_funcs(fn, block_given?) self end |
#right_value ⇒ Object
120 121 122 |
# File 'lib/totally_lazy/either.rb', line 120 def right_value @value end |
#to_s ⇒ Object
147 148 149 |
# File 'lib/totally_lazy/either.rb', line 147 def to_s "right(#{@value})" end |