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.

Parameters:

  • data (Object)

    The inner data this Either encapsulates.



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.

Parameters:

  • f (Proc<A, _>) (defaults to: nil)

    The function to call. Could be a block instead. Takes an ‘A` the return is ignored.

Yields:

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

Returns:

  • (Either<A, B>)

    returns self.



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.

Parameters:

  • f (Proc) (defaults to: nil)

    Optional Proc - ignored.

Yields:

  • An ignored block.

Returns:



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:

Returns:

  • (Boolean)

    ‘true` because this is a `Left`.

See Also:



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.

Parameters:

  • f (Proc<A, Result<C>>) (defaults to: nil)

    The function to call. Could be a block instead. Takes an [A=Object] and must return a [Either<C, B>].

Yields:

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

Returns:

  • (Left<C> | Right<C>)

    A new Result from ‘f’ or the block. If the return value is a non-Either, this this will raise ‘EitherReturnExpectedError’.



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.

Parameters:

  • f (Proc<A, C>) (defaults to: nil)

    The function to call. Could be a block instead. Takes an [A=Object] and returns a C.

Yields:

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

Returns:

  • (Either<C, B>)

    A new ‘Left<C>’ whose ‘C’ is the return of ‘f’ or the block.



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.

Parameters:

  • f (Proc<A, C>) (defaults to: nil)

    A dummy function. Not used.

Yields:

  • A dummy block. Not used.

Returns:

  • (Either<A, B>)

    This ‘Left’.



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:

Returns:

  • (Boolean)

    ‘false` because this is not a `Right`.

See Also:



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.

Parameters:

  • f (Proc<A, B>) (defaults to: nil)

    A dummy function. Not used.

Yields:

  • A dummy block. Not used.

Returns:

  • (Either<A, B>)

    This ‘Either’.

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.

Returns:

  • (A)

    The inner data of this ‘Left’.



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.

Returns:

  • (E)

    The data of this ‘Right’.

Raises:



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

def unwrap_right()
  raise UnwrapError.new(
    <<~EOE
      #{self.class()} with #{@data.inspect()} could not be unwrapped as a
      Right.
    EOE
  )
end