Class: Rumonade::Either::RightProjection
- Inherits:
-
Object
- Object
- Rumonade::Either::RightProjection
- Includes:
- Monad
- Defined in:
- lib/rumonade/either.rb
Overview
Projects an Either into a Right.
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 ⇒ RightProjection
Returns the empty
RightProjection. -
.unit(value) ⇒ RightProjection
Returns a
RightProjectionof theRightof the given value.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Returns
trueif other is aRightProjectionwith an equalEithervalue. -
#all?(lam = nil, &blk) ⇒ Boolean
Returns
trueifLeftor returns the result of the application of the given function to theRightvalue. -
#any?(lam = nil, &blk) ⇒ Boolean
Returns
falseifLeftor returns the result of the application of the given function to theRightvalue. -
#bind(lam = nil, &blk) ⇒ Object
Binds the given function across
Right. -
#get ⇒ Object
Returns the value from this
Rightor raisesNoSuchElementExceptionif this is aLeft. -
#get_or_else(val_or_lam = nil, &blk) ⇒ Object
Returns the value from this
Rightor the given argument if this is aLeft. -
#initialize(either_value) ⇒ RightProjection
constructor
A new instance of RightProjection.
-
#inspect ⇒ String
Returns a
Stringcontaining a human-readable representation of this object. -
#map(lam = nil, &blk) ⇒ Either
Maps the function argument through
Right. -
#select(lam = nil, &blk) ⇒ Option
Returns
Noneif this is aLeftor if the given predicate does not hold for theRightvalue, otherwise, returns aSomeofRight. -
#to_opt ⇒ Option
Returns a
Somecontaining theRightvalue if it exists or aNoneif this is aLeft. -
#to_s ⇒ String
Returns a
Stringrepresentation 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) ⇒ RightProjection
Returns a new instance of RightProjection.
256 257 258 |
# File 'lib/rumonade/either.rb', line 256 def initialize(either_value) @either_value = either_value end |
Instance Attribute Details
#either_value ⇒ Object (readonly)
Returns the Either value
261 262 263 |
# File 'lib/rumonade/either.rb', line 261 def either_value @either_value end |
Class Method Details
.empty ⇒ RightProjection
Returns the empty RightProjection
250 251 252 |
# File 'lib/rumonade/either.rb', line 250 def empty self.new(Left(nil)) end |
.unit(value) ⇒ RightProjection
Returns a RightProjection of the Right of the given value
245 246 247 |
# File 'lib/rumonade/either.rb', line 245 def unit(value) self.new(Right(value)) end |
Instance Method Details
#==(other) ⇒ Boolean
Returns true if other is a RightProjection with an equal Either value
264 265 266 |
# File 'lib/rumonade/either.rb', line 264 def ==(other) other.is_a?(RightProjection) && other.either_value == self.either_value end |
#all?(lam = nil, &blk) ⇒ Boolean
Returns true if Left or returns the result of the application of the given function to the Right value.
286 287 288 |
# File 'lib/rumonade/either.rb', line 286 def all?(lam = nil, &blk) !either_value.right? || bind(lam || blk) end |
#any?(lam = nil, &blk) ⇒ Boolean
Returns false if Left or returns the result of the application of the given function to the Right value.
276 277 278 |
# File 'lib/rumonade/either.rb', line 276 def any?(lam = nil, &blk) either_value.right? && bind(lam || blk) end |
#bind(lam = nil, &blk) ⇒ Object
Binds the given function across Right.
269 270 271 |
# File 'lib/rumonade/either.rb', line 269 def bind(lam = nil, &blk) if !either_value.right? then either_value else (lam || blk).call(either_value.right_value) end end |
#get ⇒ Object
Returns the value from this Right or raises NoSuchElementException if this is a Left.
291 292 293 |
# File 'lib/rumonade/either.rb', line 291 def get if either_value.right? then either_value.right_value else raise NoSuchElementError end end |
#get_or_else(val_or_lam = nil, &blk) ⇒ Object
Returns the value from this Right or the given argument if this is a Left.
296 297 298 299 |
# File 'lib/rumonade/either.rb', line 296 def get_or_else(val_or_lam = nil, &blk) v_or_f = val_or_lam || blk if either_value.right? then either_value.right_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.
317 318 319 |
# File 'lib/rumonade/either.rb', line 317 def inspect "RightProjection(#{either_value.inspect})" end |
#map(lam = nil, &blk) ⇒ Either
Returns Maps the function argument through Right.
307 308 309 |
# File 'lib/rumonade/either.rb', line 307 def map(lam = nil, &blk) bind { |v| Right((lam || blk).call(v)) } end |
#select(lam = nil, &blk) ⇒ Option
Returns None if this is a Left or if the given predicate does not hold for the Right value, otherwise, returns a Some of Right.
281 282 283 |
# File 'lib/rumonade/either.rb', line 281 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 Right value if it exists or a None if this is a Left.
302 303 304 |
# File 'lib/rumonade/either.rb', line 302 def to_opt Option(get_or_else(nil)) end |
#to_s ⇒ String
Returns a String representation of this object.
312 313 314 |
# File 'lib/rumonade/either.rb', line 312 def to_s "RightProjection(#{either_value})" end |