Class: Fear::PatternMatch

Inherits:
Object
  • Object
show all
Defined in:
lib/fear/pattern_match.rb

Overview

Note:

Use this class only to build custom pattern match classes. See Fear::OptionPatternMatch as an example.

Pattern match builder. Provides DSL for building pattern matcher Pattern match is just a combination of partial functions

matcher = Fear.matcher do |m|
  m.case(Integer) { |x| x * 2 }
  m.case(String) { |x| x.to_i(10) * 2 }
end
matcher.is_a?(Fear::PartialFunction) #=> true
matcher.defined_at?(4) #=> true
matcher.defined_at?('4') #=> true
matcher.defined_at?(nil) #=> false

The previous example is the same as:

Fear.case(Integer) { |x| x * ) }
  .or_else(
    Fear.case(String) { |x| x.to_i(10) * 2 }
  )

You can provide else branch, so partial function will be defined on any input:

matcher = Fear.matcher do |m|
  m.else { 'Match' }
end
matcher.call(42) #=> 'Match'

See Also:

  • public interface for building matchers

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(result) ⇒ PatternMatch

Returns a new instance of PatternMatch.

Parameters:

  • result (Fear::EmptyPartialFunction)


71
72
73
# File 'lib/fear/pattern_match.rb', line 71

def initialize(result)
  @result = result
end

Instance Attribute Details

#resultObject



74
75
76
# File 'lib/fear/pattern_match.rb', line 74

def result
  @result
end

Class Method Details

.__new__Object



36
# File 'lib/fear/pattern_match.rb', line 36

alias __new__ new

.mixin(as: :match) ⇒ Module

Creates anonymous module to add ‘#mathing` behaviour to a class

Examples:

class User
  include Fear::PatternMatch.mixin
end

user.match do |m\
  m.case(:admin?) { |u| ... }
  m.else { |u| ... }
end

Parameters:

  • as (Symbol, String) (defaults to: :match)

    (:match) method name

Returns:

  • (Module)


59
60
61
62
63
64
65
66
67
# File 'lib/fear/pattern_match.rb', line 59

def mixin(as: :match)
  matcher_class = self

  Module.new do
    define_method(as) do |&matchers|
      matcher_class.new(&matchers).(self)
    end
  end
end

.new {|builder| ... } ⇒ Fear::PartialFunction

Yields:

  • (builder)

Returns:



39
40
41
42
43
# File 'lib/fear/pattern_match.rb', line 39

def new
  builder = __new__(PartialFunction::Empty)
  yield builder
  builder.result
end

Instance Method Details

#case(*guards, &effect) ⇒ Fear::PatternMatch

This method is syntactic sugar for ‘PartialFunction#or_else`, but rather than passing another partial function as an argument, you pass arguments to build such partial function.

Examples:

This two examples produces the same result

other = Fear.case(Integer) { |x| x * 2 }
this.or_else(other)

this.case(Integer) { |x| x * 2 }

Parameters:

  • guards (<#===>)
  • effect (Proc)

Returns:

See Also:



94
95
96
# File 'lib/fear/pattern_match.rb', line 94

def case(*guards, &effect)
  or_else(Fear.case(*guards, &effect))
end

#else(&effect) ⇒ Object

See Also:

  • Fear::PartialFunction#else


78
79
80
# File 'lib/fear/pattern_match.rb', line 78

def else(&effect)
  or_else(Fear.case(&effect))
end

#or_else(other) ⇒ Fear::PatternMatch



100
101
102
103
# File 'lib/fear/pattern_match.rb', line 100

def or_else(other)
  self.result = result.or_else(other)
  self
end