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)


69
70
71
# File 'lib/fear/pattern_match.rb', line 69

def initialize(result)
  @result = result
end

Instance Attribute Details

#resultObject



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

def result
  @result
end

Class Method Details

.__new__Object



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

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)


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

def mixin(as: :match)
  matcher_class = self

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

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

Yields:

  • (builder)

Returns:



37
38
39
40
41
# File 'lib/fear/pattern_match.rb', line 37

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

Instance Method Details

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

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:



92
93
94
# File 'lib/fear/pattern_match.rb', line 92

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

#else(&effect) ⇒ Object

See Also:

  • Fear::PartialFunction#else


76
77
78
# File 'lib/fear/pattern_match.rb', line 76

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

#or_else(other) ⇒ Object



107
108
109
110
# File 'lib/fear/pattern_match.rb', line 107

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

#xcase(pattern, *guards, &effect) ⇒ Fear::PartialFunction

Parameters:

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

Returns:

See Also:



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

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