Class: Fear::EitherPatternMatch 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::LeftPatternMatch and Fear::RightPatternMatch

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'

Direct Known Subclasses

LeftPatternMatch, RightPatternMatch

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::EitherPatternMatch 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:



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

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::EitherPatternMatch 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:



33
34
35
36
# File 'lib/fear/either_pattern_match.rb', line 33

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