Sha256: 5f0fe01ed643792586cf7349fda351db12c482ad47b3f50a293bc1a62526e5e8

Contents?: true

Size: 1.28 KB

Versions: 1

Compression:

Stored size: 1.28 KB

Contents

# frozen_string_literal: true

module Fear
  module Option
    # Option pattern matcher
    #
    # @example
    #   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'
    #
    #  @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
    #
    # @note it has two optimized subclasses +Fear::SomePatternMatch+ and +Fear::NonePatternMatch+
    # @api private
    class PatternMatch < Fear::PatternMatch
      # Match against Some
      #
      # @param conditions [<#==>]
      # @return [Fear::Option::PatternMatch]
      def some(*conditions, &effect)
        branch = Fear.case(Fear::Some, &:get).and_then(Fear.case(*conditions, &effect))
        or_else(branch)
      end

      # Match against None
      #
      # @param effect [Proc]
      # @return [Fear::Option::PatternMatch]
      def none(&effect)
        branch = Fear.case(Fear::None, &effect)
        or_else(branch)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fear-3.0.0 lib/fear/option/pattern_match.rb