Class: MonadOxide::Left

Inherits:
Either
  • Object
show all
Defined in:
lib/left.rb

Overview

‘Left’ represents an arbitrary branch of Either. Since Either is supposed to be unbiased, you can assign any bias you like to Left, though in some ecosystems (such as Haskell, where this Either is ultimately inspired from), the Left side is the less common/favorable one, but this is only out of convention.

Instance Method Summary collapse

Methods inherited from Either

#match

Constructor Details

#initialize(data) ⇒ Left

Constructs a ‘Left’ with the data provided.



21
22
23
# File 'lib/left.rb', line 21

def initialize(data)
  @data = data
end

Instance Method Details

#inspect_left(f = nil) { ... } ⇒ Either<A, B>

Applies ‘f` or the block over the data and returns the same `Left`. No changes are applied. This is ideal for logging. @see Either#inspect_left for a higher level understanding how this works with Eithers in general.

Yields:

  • Will yield a block that takes an A the return is ignored. Same as ‘f’ parameter.



34
35
36
37
# File 'lib/left.rb', line 34

def inspect_left(f=nil, &block)
  (f || block).call(@data)
  self
end

#inspect_right(f = nil) { ... } ⇒ Either

Falls through. @see Either#inspect_right for how this is handled in the Either case, and @see Right#inspect_right for how this is handled in the Right case.

Yields:

  • An ignored block.



46
47
48
# File 'lib/left.rb', line 46

def inspect_right(f=nil, &block)
  self
end

#left?Boolean

Identifies that this is a ‘Left`. For counterparts:



57
58
59
# File 'lib/left.rb', line 57

def left?()
  true
end

#left_and_then(f = nil) { ... } ⇒ Left<C> | Right<C>

Invokes ‘f’ or the block with the data and returns the Either returned from that. The return type is enforced.

Yields:

  • Will yield a block that takes an A and returns a Either<C>. Same as ‘f’ parameter.



71
72
73
74
75
76
77
78
# File 'lib/left.rb', line 71

def left_and_then(f=nil, &block)
  either = (f || block).call(@data)
  if either.is_a?(Either)
    either
  else
    raise EitherReturnExpectedError.new(either)
  end
end

#map_left(f = nil) { ... } ⇒ Either<C, B>

Applies ‘f’ or the block over the data and returns a new ‘Left’ with the returned value.

Yields:

  • Will yield a block that takes an A and returns a C. Same as ‘f’ parameter.



90
91
92
# File 'lib/left.rb', line 90

def map_left(f=nil, &block)
  Left.new((f || block).call(@data))
end

#map_right(f = nil) { ... } ⇒ Either<A, B>

This is a no-op for Left. @see Right#map_right.

Yields:

  • A dummy block. Not used.



99
100
101
# File 'lib/left.rb', line 99

def map_right(f=nil, &block)
  self
end

#right?Boolean

Identifies that this is not a ‘Right`. For counterparts:



110
111
112
# File 'lib/left.rb', line 110

def right?()
  false
end

#right_and_then(f = nil) { ... } ⇒ Either<A, B>

The Left equivalent to Right#left_and_then. This is a no-op for Left.

Yields:

  • A dummy block. Not used.

See Also:



120
121
122
# File 'lib/left.rb', line 120

def right_and_then(f=nil, &block)
  self
end

#unwrap_leftA

Dangerously access the ‘Left’ data. If this is a ‘Right’, an ‘UnwrapError` will be raised. It is recommended to use this for tests only.



129
130
131
# File 'lib/left.rb', line 129

def unwrap_left()
  @data
end

#unwrap_rightE

Dangerously access the ‘Right’ data. Since this is a ‘Left’, it will always raise an error. @see Either#unwrap_right. It is recommended to use this for tests only.

Raises:



138
139
140
141
142
143
144
145
# File 'lib/left.rb', line 138

def unwrap_right()
  raise UnwrapError.new(
    "      \#{self.class()} with \#{@data.inspect()} could not be unwrapped as a\n      Right.\n    EOE\n  )\nend\n"