Class: Fear::Either::PatternMatch Private

Inherits:
PatternMatch show all
Defined in:
lib/fear/either/pattern_match.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Note:

it has two optimized subclasses Fear::Left::PatternMatch and Fear::Right::PatternMatch

Either pattern matcher

@example the same matcher may be defined using block syntax
  EitherPatternMatch.new 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

Examples:

pattern_match =
  EitherPatternMatch.new
    .right(Integer, ->(x) { x > 2 }) { |x| x * 2 }
    .right(String) { |x| x.to_i * 2 }
    .left(String) { :err }
    .else { 'error '}

pattern_match.call(42) => 'NaN'

Instance Attribute Summary

Attributes inherited from PatternMatch

#result

Instance Method Summary collapse

Methods inherited from PatternMatch

__new__, #case, #else, #initialize, mixin, new, #or_else

Constructor Details

This class inherits a constructor from Fear::PatternMatch

Instance Method Details

#left(*conditions, &effect) ⇒ Fear::Either::PatternMatch Also known as: failure

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Match against Fear::Left

Parameters:

  • conditions (<#==>)

Returns:



42
43
44
45
# File 'lib/fear/either/pattern_match.rb', line 42

def left(*conditions, &effect)
  branch = Fear.case(Fear::Left, &:left_value).and_then(Fear.case(*conditions, &effect))
  or_else(branch)
end

#right(*conditions, &effect) ⇒ Fear::Either::PatternMatch Also known as: success

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Match against Fear::Right

Parameters:

  • conditions (<#==>)

Returns:



32
33
34
35
# File 'lib/fear/either/pattern_match.rb', line 32

def right(*conditions, &effect)
  branch = Fear.case(Fear::Right, &:right_value).and_then(Fear.case(*conditions, &effect))
  or_else(branch)
end