Class: Rumonade::Either Abstract
- Inherits:
-
Object
- Object
- Rumonade::Either
- Defined in:
- lib/rumonade/either.rb,
lib/rumonade/either.rb
Overview
Defined Under Namespace
Classes: LeftProjection, RightProjection
Constant Summary collapse
- DEFAULT_CONCAT =
Default concatenation function used by #+
lambda { |a,b| a + b }
Instance Method Summary collapse
-
#+(other, opts = {}) {|right_value| ... } ⇒ Either
(also: #concat)
If both are
Right
, returnsRight
withright_value
‘s concatenated, otherwise aLeft
withleft_value
’s concatenated. -
#fold(function_of_left_value, function_of_right_value) ⇒ Object
Returns the results of applying the function.
-
#left ⇒ LeftProjection
Projects this Either as a Left.
-
#left? ⇒ Boolean
Returns
true
if this is a Left,false
otherwise. -
#lift(monad_class) ⇒ Either
Returns an Eitherof the same type, with the
left_value
orright_value
lifted intomonad_class
. -
#lift_to_a ⇒ Either
Returns an
Either
of the same type, with theleft_value
orright_value
lifted into anArray
. -
#right ⇒ RightProjection
Projects this Either as a Right.
-
#right? ⇒ Boolean
Returns
true
if this is a Right,false
otherwise. -
#swap ⇒ Boolean
If this is a Left, then return the left value in Right or vice versa.
Instance Method Details
#+(other, opts = {}) {|right_value| ... } ⇒ Either Also known as: concat
Returns if both are Right
, returns Right
with right_value
‘s concatenated, otherwise a Left
with left_value
’s concatenated.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/rumonade/either.rb', line 58 def +(other, opts = {}) opts = { :concat_left => DEFAULT_CONCAT, :concat_right => DEFAULT_CONCAT }.merge(opts) result = case self when Left case other when Left then Left(opts[:concat_left].call(self.left_value, other.left_value)) when Right then Left(self.left_value) end when Right case other when Left then Left(other.left_value) when Right then Right(opts[:concat_right].call(self.right_value, other.right_value)) end end if block_given? then result.right.map { |right_values| yield right_values } else result end end |
#fold(function_of_left_value, function_of_right_value) ⇒ Object
Returns the results of applying the function
33 34 35 |
# File 'lib/rumonade/either.rb', line 33 def fold(function_of_left_value, function_of_right_value) if left? then function_of_left_value.call(left_value) else function_of_right_value.call(right_value) end end |
#left ⇒ LeftProjection
Returns Projects this Either as a Left.
38 39 40 |
# File 'lib/rumonade/either.rb', line 38 def left LeftProjection.new(self) end |
#left? ⇒ Boolean
Returns true
if this is a Left, false
otherwise.
16 17 18 |
# File 'lib/rumonade/either.rb', line 16 def left? is_a?(Left) end |
#lift(monad_class) ⇒ Either
Returns an Eitherof the same type, with the left_value
or right_value
lifted into monad_class
86 87 88 |
# File 'lib/rumonade/either.rb', line 86 def lift(monad_class) fold(lambda {|l| Left(monad_class.unit(l)) }, lambda {|r| Right(monad_class.unit(r))}) end |
#lift_to_a ⇒ Either
Returns an Either
of the same type, with the left_value
or right_value
lifted into an Array
79 80 81 |
# File 'lib/rumonade/either.rb', line 79 def lift_to_a lift(Array) end |
#right ⇒ RightProjection
Returns Projects this Either as a Right.
43 44 45 |
# File 'lib/rumonade/either.rb', line 43 def right RightProjection.new(self) end |