Class: Rumonade::Either::LeftProjection
- Inherits:
-
Object
- Object
- Rumonade::Either::LeftProjection
- Includes:
- Monad
- Defined in:
- lib/rumonade/either.rb
Overview
Projects an Either into a Left.
Constant Summary
Constants included from Monad
Monad::DEFAULT_METHODS_TO_REPLACE_WITH_MONAD
Instance Attribute Summary collapse
-
#either_value ⇒ Object
readonly
Returns the Either value.
Class Method Summary collapse
-
.empty ⇒ LeftProjection
Returns the empty
LeftProjection
. -
.unit(value) ⇒ LeftProjection
Returns a
LeftProjection
of theLeft
of the given value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns
true
if other is aLeftProjection
with an equalEither
value. -
#all?(lam = nil, &blk) ⇒ Boolean
Returns
true
ifRight
or returns the result of the application of the given function to theLeft
value. -
#any?(lam = nil, &blk) ⇒ Boolean
Returns
false
ifRight
or returns the result of the application of the given function to theLeft
value. -
#bind(lam = nil, &blk) ⇒ Object
Binds the given function across
Left
. -
#get ⇒ Object
Returns the value from this
Left
or raisesNoSuchElementException
if this is aRight
. -
#get_or_else(val_or_lam = nil, &blk) ⇒ Object
Returns the value from this
Left
or the given argument if this is aRight
. -
#initialize(either_value) ⇒ LeftProjection
constructor
A new instance of LeftProjection.
-
#inspect ⇒ String
Returns a
String
containing a human-readable representation of this object. -
#map(lam = nil, &blk) ⇒ Either
Maps the function argument through
Left
. -
#select(lam = nil, &blk) ⇒ Option
Returns
None
if this is aRight
or if the given predicate does not hold for theleft
value, otherwise, returns aSome
ofLeft
. -
#to_opt ⇒ Option
Returns a
Some
containing theLeft
value if it exists or aNone
if this is aRight
. -
#to_s ⇒ String
Returns a
String
representation of this object.
Methods included from Monad
#can_flatten_in_monad?, #each, #flat_map_with_monad, #flatten_with_monad, included, #map_with_monad, #shallow_flatten
Constructor Details
#initialize(either_value) ⇒ LeftProjection
Returns a new instance of LeftProjection.
175 176 177 |
# File 'lib/rumonade/either.rb', line 175 def initialize(either_value) @either_value = either_value end |
Instance Attribute Details
#either_value ⇒ Object (readonly)
Returns the Either value
180 181 182 |
# File 'lib/rumonade/either.rb', line 180 def either_value @either_value end |
Class Method Details
.empty ⇒ LeftProjection
Returns the empty LeftProjection
169 170 171 |
# File 'lib/rumonade/either.rb', line 169 def empty self.new(Right(nil)) end |
.unit(value) ⇒ LeftProjection
Returns a LeftProjection
of the Left
of the given value
164 165 166 |
# File 'lib/rumonade/either.rb', line 164 def unit(value) self.new(Left(value)) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true
if other is a LeftProjection
with an equal Either
value
183 184 185 |
# File 'lib/rumonade/either.rb', line 183 def ==(other) other.is_a?(LeftProjection) && other.either_value == self.either_value end |
#all?(lam = nil, &blk) ⇒ Boolean
Returns true
if Right
or returns the result of the application of the given function to the Left
value.
205 206 207 |
# File 'lib/rumonade/either.rb', line 205 def all?(lam = nil, &blk) !either_value.left? || bind(lam || blk) end |
#any?(lam = nil, &blk) ⇒ Boolean
Returns false
if Right
or returns the result of the application of the given function to the Left
value.
195 196 197 |
# File 'lib/rumonade/either.rb', line 195 def any?(lam = nil, &blk) either_value.left? && bind(lam || blk) end |
#bind(lam = nil, &blk) ⇒ Object
Binds the given function across Left
.
188 189 190 |
# File 'lib/rumonade/either.rb', line 188 def bind(lam = nil, &blk) if !either_value.left? then either_value else (lam || blk).call(either_value.left_value) end end |
#get ⇒ Object
Returns the value from this Left
or raises NoSuchElementException
if this is a Right
.
210 211 212 |
# File 'lib/rumonade/either.rb', line 210 def get if either_value.left? then either_value.left_value else raise NoSuchElementError end end |
#get_or_else(val_or_lam = nil, &blk) ⇒ Object
Returns the value from this Left
or the given argument if this is a Right
.
215 216 217 218 |
# File 'lib/rumonade/either.rb', line 215 def get_or_else(val_or_lam = nil, &blk) v_or_f = val_or_lam || blk if either_value.left? then either_value.left_value else (v_or_f.respond_to?(:call) ? v_or_f.call : v_or_f) end end |
#inspect ⇒ String
Returns a String
containing a human-readable representation of this object.
236 237 238 |
# File 'lib/rumonade/either.rb', line 236 def inspect "LeftProjection(#{either_value.inspect})" end |
#map(lam = nil, &blk) ⇒ Either
Returns Maps the function argument through Left
.
226 227 228 |
# File 'lib/rumonade/either.rb', line 226 def map(lam = nil, &blk) bind { |v| Left((lam || blk).call(v)) } end |
#select(lam = nil, &blk) ⇒ Option
Returns None
if this is a Right
or if the given predicate does not hold for the left
value, otherwise, returns a Some
of Left
.
200 201 202 |
# File 'lib/rumonade/either.rb', line 200 def select(lam = nil, &blk) Some(self).select { |lp| lp.any?(lam || blk) }.map { |lp| lp.either_value } end |
#to_opt ⇒ Option
Returns a Some
containing the Left
value if it exists or a None
if this is a Right
.
221 222 223 |
# File 'lib/rumonade/either.rb', line 221 def to_opt Option(get_or_else(nil)) end |
#to_s ⇒ String
Returns a String
representation of this object.
231 232 233 |
# File 'lib/rumonade/either.rb', line 231 def to_s "LeftProjection(#{either_value})" end |