Class: Fear::PatternMatch
- Inherits:
-
Object
- Object
- Fear::PatternMatch
- Defined in:
- lib/fear/pattern_match.rb
Overview
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'
Direct Known Subclasses
Either::PatternMatch, Option::PatternMatch, Try::PatternMatch
Instance Attribute Summary collapse
- #result ⇒ Object readonly
Class Method Summary collapse
- .__new__ ⇒ Object
-
.mixin(as: :match) ⇒ Module
Creates anonymous module to add ‘#mathing` behaviour to a class.
- .new {|builder| ... } ⇒ Fear::PartialFunction
Instance Method Summary collapse
-
#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.
- #else(&effect) ⇒ Object
-
#initialize(result) ⇒ PatternMatch
constructor
A new instance of PatternMatch.
- #or_else(other) ⇒ Fear::PatternMatch
Constructor Details
#initialize(result) ⇒ PatternMatch
Returns a new instance of PatternMatch.
71 72 73 |
# File 'lib/fear/pattern_match.rb', line 71 def initialize(result) @result = result end |
Instance Attribute Details
#result ⇒ Object
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
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
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.
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
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 |