Sha256: b753649284e3ffc178a215a3b39a7537e97f43dee83286dc48161da30ac0cfa4
Contents?: true
Size: 1.42 KB
Versions: 2
Compression:
Stored size: 1.42 KB
Contents
module Fear # 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 class EitherPatternMatch < Fear::PatternMatch LEFT_EXTRACTOR = :left_value.to_proc RIGHT_EXTRACTOR = :right_value.to_proc # Match against +Fear::Right+ # # @param conditions [<#==>] # @return [Fear::EitherPatternMatch] def right(*conditions, &effect) branch = Fear.case(Fear::Right, &RIGHT_EXTRACTOR).and_then(Fear.case(*conditions, &effect)) or_else(branch) end alias success right # Match against +Fear::Left+ # # @param conditions [<#==>] # @return [Fear::EitherPatternMatch] def left(*conditions, &effect) branch = Fear.case(Fear::Left, &LEFT_EXTRACTOR).and_then(Fear.case(*conditions, &effect)) or_else(branch) end alias failure left end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
fear-1.0.0 | lib/fear/either_pattern_match.rb |
fear-0.11.0 | lib/fear/either_pattern_match.rb |