Module: Fear::Either

Included in:
Left, Right
Defined in:
lib/fear/either.rb

Overview

Represents a value of one of two possible types (a disjoint union.) An instance of Either is either an instance of Left or Right.

A common use of Either is as an alternative to Option for dealing with possible missing values. In this usage, None is replaced with a Left which can contain useful information. Right takes the place of Some. Convention dictates that Left is used for failure and Right is used for Right.

For example, you could use Either<String, Fixnum> to select_or_else whether a received input is a String or an Fixnum.

Either is right-biased, which means that Right is assumed to be the default case to operate on. If it is Left, operations like #map, #flat_map, … return the Left value unchanged:


Examples:

in = Readline.readline('Type Either a string or an Int: ', true)
result = begin
  Fear.right(Integer(in))
rescue ArgumentError
  Fear.left(in)
end

result.match do |m|
  m.right do |x|
    "You passed me the Int: #{x}, which I will increment. #{x} + 1 = #{x+1}"
  end

  m.left do |x|
    "You passed me the String: #{x}"
  end
end

See Also:

Defined Under Namespace

Modules: Mixin

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.matcher {|| ... } ⇒ Fear::PartialFunction

Build pattern matcher to be used later, despite off Either#match method, id doesn’t apply matcher immanently, but build it instead. Unusually in sake of efficiency it’s better to statically build matcher and reuse it later.

Examples:

matcher =
  Either.matcher do |m|
    m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    m.right(String) { |x| x.to_i * 2 }
    m.left(String) { :err }
    m.else { 'error '}
  end
matcher.call(Fear.right(42))

Yield Parameters:

Returns:



301
302
303
# File 'lib/fear/either.rb', line 301

def matcher(&matcher)
  EitherPatternMatch.new(&matcher)
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Any)

Returns:

  • (Boolean)


266
267
268
# File 'lib/fear/either.rb', line 266

def ==(other)
  other.is_a?(self.class) && value == other.value
end

#any? {|value| ... } ⇒ Boolean

Returns false if Left or returns the result of the application of the given predicate to the Right value.

Examples:

Fear.right(12).any?( |v| v > 10)         #=> true
Fear.right(7).any?( |v| v > 10)          #=> false
Fear.left('undefined').any?( |v| v > 10) #=> false

Yield Parameters:

  • value (any)

Yield Returns:

  • (Boolean)

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#deconstruct<any>

Returns:

  • (<any>)


279
280
281
# File 'lib/fear/either.rb', line 279

def deconstruct
  [value]
end

#each {|value| ... } ⇒ Fear::Either

Performs the given block if this is a Right.

Examples:

Fear.right(17).each do |value|
  puts value
end #=> prints 17

Fear.left('undefined').each do |value|
  puts value
end #=> does nothing

Yield Parameters:

  • value (any)

Yield Returns:

  • (void)

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#flat_map {|value| ... } ⇒ Fear::Either

Returns the given block applied to the value from this Right or returns this if this is a Left.

Examples:

Fear.right(42).flat_map { |v| Fear.right(v/2) }         #=> Fear.right(21)
Fear.left('undefined').flat_map { |v| Fear.right(v/2) } #=> Fear.left('undefined')

Yield Parameters:

  • value (any)

Yield Returns:

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#get_or_else(&default) ⇒ any #get_or_else(default) ⇒ any

Returns the value from this Right or evaluates the given default argument if this is a Left.

Overloads:

  • #get_or_else(&default) ⇒ any

    Examples:

    Fear.right(42).get_or_else { 24/2 }         #=> 42
    Fear.left('undefined').get_or_else { 24/2 } #=> 12

    Yield Returns:

    • (any)

    Returns:

    • (any)
  • #get_or_else(default) ⇒ any

    Examples:

    Fear.right(42).get_or_else(12)         #=> 42
    Fear.left('undefined').get_or_else(12) #=> 12

    Returns:

    • (any)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#include?(other_value) ⇒ Boolean

Returns true if Right has an element that is equal (as determined by ==) to other_value, false otherwise.

Examples:

Fear.right(17).include?(17)         #=> true
Fear.right(17).include?(7)          #=> false
Fear.left('undefined').include?(17) #=> false

Parameters:

  • (any)

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#initialize(value) ⇒ Object



257
258
259
# File 'lib/fear/either.rb', line 257

def initialize(value)
  @value = value
end

#inspectString Also known as: to_s

Returns:

  • (String)


271
272
273
# File 'lib/fear/either.rb', line 271

def inspect
  "#<#{self.class} value=#{value.inspect}>"
end

#join_rightEither

Note:

This method, and join_right, are analogous to Option#flatten

Joins an Either through Left. This method requires that the left side of this Either is itself an Either type.

Examples:

Fear.left(Fear.right("flower")).join_left   #=> Fear.right("flower")
Fear.left(Fear.left(12)).join_left          #=> Fear.left(12)
Fear.right("daisy").join_left               #=> Fear.right("daisy")
Fear.right(Fear.left("daisy")).join_left    #=> Fear.right(Fear.left("daisy"))

Returns:

Raises:

  • (TypeError)

    if it does not contain Either.



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#left?Boolean

Note:

this method is also aliased as #failure?

Returns true if this is a Left, false otherwise.

Examples:

Fear.right(42).left?   #=> false
Fear.left('err').left? #=> true

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#map {|value| ... } ⇒ Object

Maps the given block to the value from this Right or returns this if this is a Left.

Examples:

Fear.right(42).map { |v| v/2 }          #=> Fear.right(21)
Fear.left('undefined').map { |v| v/2 }  #=> Fear.left('undefined')

Yield Parameters:

  • value (any)

Yield Returns:

  • (any)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#match(&matcher) ⇒ Object

Pattern match against this Either

Examples:

either.match do |m|
  m.right(Integer) do |x|
   x * 2
  end

  m.right(String) do |x|
    x.to_i * 2
  end

  m.left { |x| x }
  m.else { 'something unexpected' }
end


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#or_else(&alternative) ⇒ Either

Returns this Right or the given alternative if this is a Left.

Examples:

Fear.right(42).or_else { Fear.right(21) }           #=> Fear.right(42)
Fear.left('unknown').or_else { Fear.right(21) }     #=> Fear.right(21)
Fear.left('unknown').or_else { Fear.left('empty') } #=> Fear.left('empty')

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#reduce(reduce_left, reduce_right) ⇒ any

Applies reduce_left if this is a Left or reduce_right if this is a Right.

Examples:

result = possibly_failing_operation()
log(
  result.reduce(
    ->(ex) { "Operation failed with #{ex}" },
    ->(v) { "Operation produced value: #{v}" },
  )
)

Parameters:

  • reduce_left (Proc)

    the Proc to apply if this is a Left

  • reduce_right (Proc)

    the Proc to apply if this is a Right

Returns:

  • (any)

    the results of applying the Proc



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#reject {|value| ... } ⇒ Either

Returns Left of value if the given predicate holds for the right value, otherwise, returns Right.

Examples:

Fear.right(12).reject(&:even?) #=> Fear.left(12)
Fear.right(7).reject(&:even?)  #=> Fear.right(7)
Fear.left(12).reject(&:even?)  #=> Fear.left(12)
Fear.left(7).reject(&:even?)   #=> Fear.left(7)

Yield Parameters:

  • value (Object)

Yield Returns:

  • (Boolean)

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#right?Boolean

Note:

this method is also aliased as #success?

Returns true if this is a Right, false otherwise.

Examples:

Fear.right(42).right?   #=> true
Fear.left('err').right? #=> false

Returns:

  • (Boolean)


246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#select {|value| ... } ⇒ Either

Returns Left of value if the given predicate does not hold for the right value, otherwise, returns Right.

Examples:

Fear.right(12).select(&:even?) #=> Fear.right(12)
Fear.right(7).select(&:even?)  #=> Fear.left(7)
Fear.left(12).select(&:even?)  #=> Fear.left(12)
Fear.left(7).select(&:even?)   #=> Fear.left(7)

Yield Parameters:

  • value (Object)

Yield Returns:

  • (Boolean)

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#select_or_else(default) {|value| ... } ⇒ Either

Returns Left of the default if the given predicate does not hold for the right value, otherwise, returns Right.

Examples:

Fear.right(12).select_or_else(-1, &:even?)       #=> Fear.right(12)
Fear.right(7).select_or_else(-1, &:even?)        #=> Fear.left(-1)
Fear.left(12).select_or_else(-1, &:even?)        #=> Fear.left(12)
Fear.left(12).select_or_else(-> { -1 }, &:even?) #=> Fear.left(12)

Parameters:

  • default (Object, Proc)

Yield Parameters:

  • value (Object)

Yield Returns:

  • (Boolean)

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#swapEither

If this is a Left, then return the left value in Right or vice versa.

Examples:

Fear.left('left').swap   #=> Fear.right('left')
Fear.right('right').swap #=> Fear.left('left')

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end

#to_optionOption

Returns an Some containing the Right value or a None if this is a Left.

Examples:

Fear.right(42).to_option          #=> Fear.some(42)
Fear.left('undefined').to_option  #=> Fear.none()

Returns:



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/fear/either.rb', line 246

module Either
  # @private
  def left_class
    Left
  end

  # @private
  def right_class
    Right
  end

  def initialize(value)
    @value = value
  end

  attr_reader :value
  protected :value

  # @param other [Any]
  # @return [Boolean]
  def ==(other)
    other.is_a?(self.class) && value == other.value
  end

  # @return [String]
  def inspect
    "#<#{self.class} value=#{value.inspect}>"
  end

  # @return [String]
  alias to_s inspect

  # @return [<any>]
  def deconstruct
    [value]
  end

  class << self
    # Build pattern matcher to be used later, despite off
    # +Either#match+ method, id doesn't apply matcher immanently,
    # but build it instead. Unusually in sake of efficiency it's better
    # to statically build matcher and reuse it later.
    #
    # @example
    #   matcher =
    #     Either.matcher do |m|
    #       m.right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    #       m.right(String) { |x| x.to_i * 2 }
    #       m.left(String) { :err }
    #       m.else { 'error '}
    #     end
    #   matcher.call(Fear.right(42))
    #
    # @yieldparam [Fear::EitherPatternMatch]
    # @return [Fear::PartialFunction]
    def matcher(&matcher)
      EitherPatternMatch.new(&matcher)
    end
  end

  # Include this mixin to access convenient factory methods.
  # @example
  #   include Fear::Either::Mixin
  #
  #   Right('flower') #=> #<Fear::Right value='flower'>
  #   Left('beaf')    #=> #<Fear::Legt value='beaf'>
  #
  module Mixin
    # @param value [any]
    # @return [Fear::Left]
    # @example
    #   Left(42) #=> #<Fear::Left value=42>
    #
    def Left(value)
      Fear.left(value)
    end

    # @param value [any]
    # @return [Fear::Right]
    # @example
    #   Right(42) #=> #<Fear::Right value=42>
    #
    def Right(value)
      Fear.right(value)
    end
  end
end