class Fear::EitherPatternMatch
Either
pattern matcher
@example
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' @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
@note it has two optimized subclasses Fear::LeftPatternMatch
and Fear::RightPatternMatch
@api private
Constants
- LEFT_EXTRACTOR
- RIGHT_EXTRACTOR
Public Instance Methods
left(*conditions, &effect)
click to toggle source
Match against Fear::Left
@param conditions [<#==>] @return [Fear::EitherPatternMatch]
# File lib/fear/either_pattern_match.rb, line 47 def left(*conditions, &effect) branch = Fear.case(Fear::Left, &LEFT_EXTRACTOR).and_then(Fear.case(*conditions, &effect)) or_else(branch) end
Also aliased as: failure
right(*conditions, &effect)
click to toggle source
Match against Fear::Right
@param conditions [<#==>] @return [Fear::EitherPatternMatch]
# File lib/fear/either_pattern_match.rb, line 37 def right(*conditions, &effect) branch = Fear.case(Fear::Right, &RIGHT_EXTRACTOR).and_then(Fear.case(*conditions, &effect)) or_else(branch) end
Also aliased as: success