Class: Fear::Option::PatternMatch Private

Inherits:
PatternMatch show all
Defined in:
lib/fear/option/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::SomePatternMatch and Fear::NonePatternMatch

Option pattern matcher

@example the same matcher may be defined using block syntax
  Option::PatternMatch.new do |m|
     m.some(Integer) { |x| x * 2 }
     m.some(String) { |x| x.to_i * 2 }
     m.none { 'NaN' }
     m.else { 'error '}
  end

Examples:

pattern_match =
  Option::PatternMatch.new
    .some(Integer) { |x| x * 2 }
    .some(String) { |x| x.to_i * 2 }
    .none { 'NaN' }
    .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

#none(&effect) ⇒ Fear::Option::PatternMatch

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 None

Parameters:

  • effect (Proc)

Returns:



41
42
43
44
# File 'lib/fear/option/pattern_match.rb', line 41

def none(&effect)
  branch = Fear.case(Fear::None, &effect)
  or_else(branch)
end

#some(*conditions, &effect) ⇒ Fear::Option::PatternMatch

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 Some

Parameters:

  • conditions (<#==>)

Returns:



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

def some(*conditions, &effect)
  branch = Fear.case(Fear::Some, &:get).and_then(Fear.case(*conditions, &effect))
  or_else(branch)
end