Class: MonadOxide::Right

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

Overview

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

Instance Method Summary collapse

Methods inherited from Either

#match

Constructor Details

#initialize(data) ⇒ Right

Returns a new instance of Right.



18
19
20
# File 'lib/right.rb', line 18

def initialize(data)
  @data = data
end

Instance Method Details

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

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

Parameters:

  • f (Proc) (defaults to: nil)

    Optional Proc - ignored.

Yields:

  • An ignored block.

Returns:



29
30
31
# File 'lib/right.rb', line 29

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

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

Applies ‘f` or the block over the data and returns the same `Right`. No changes are applied. This is ideal for logging. @see Either#inspect_right 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.



43
44
45
46
# File 'lib/right.rb', line 43

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

#left?Boolean

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

Returns:

  • (Boolean)

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

See Also:



55
56
57
# File 'lib/right.rb', line 55

def left?()
  false
end

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

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

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:

  • Lefft#right_and_then.


65
66
67
# File 'lib/right.rb', line 65

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

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

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

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’.



74
75
76
# File 'lib/right.rb', line 74

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

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

Applies ‘f’ or the block over the data and returns a new ‘Right’ 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<A, C>)

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



87
88
89
# File 'lib/right.rb', line 87

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

#right?Boolean

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

Returns:

  • (Boolean)

    ‘true` because this is a `Right`.

See Also:



98
99
100
# File 'lib/right.rb', line 98

def right?()
  true
end

#right_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’.



112
113
114
115
116
117
118
119
# File 'lib/right.rb', line 112

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

#unwrap_leftA

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

Returns:

  • (A)

    The inner data of this ‘Right’.

Raises:



126
127
128
129
130
131
132
133
# File 'lib/right.rb', line 126

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

#unwrap_rightA

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

Returns:

  • (A)

    The inner data of this ‘Right’.



140
141
142
# File 'lib/right.rb', line 140

def unwrap_right()
  @data
end