Sha256: 203243ac656011973e717fb09425a935bb11a1b063f3ee10ab4d845b85f6b051

Contents?: true

Size: 1.45 KB

Versions: 1

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

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

1 entries across 1 versions & 1 rubygems

Version Path
fear-1.1.0 lib/fear/either_pattern_match.rb