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

Constant Summary collapse

LEFT_EXTRACTOR =

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

:left_value.to_proc
RIGHT_EXTRACTOR =

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

:right_value.to_proc

Instance Attribute Summary

Attributes inherited from PatternMatch

#result

Instance Method Summary collapse

Methods inherited from PatternMatch

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

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


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

def left(*conditions, &effect)
  branch = Fear.case(Fear::Left, &LEFT_EXTRACTOR).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


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

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