Class: Qo::PatternMatchers::PatternMatch
- Inherits:
-
Object
- Object
- Qo::PatternMatchers::PatternMatch
- Includes:
- Branching
- Defined in:
- lib/qo/pattern_matchers/pattern_match.rb
Overview
Classic Pattern Match. This matcher uses when
and else
branches and
is meant to be a more powerful variant of the case statement
Direct Known Subclasses
Class Method Summary collapse
-
.create(branches: []) ⇒ Class
Allows for the creation of an anonymous PatternMatcher based on this parent class.
-
.mixin(destructure: false, as: :match) ⇒ Module
Allows for the injection of a pattern matching function into a type class for direct access, rather than yielding an instance of that class to a pattern matcher.
Instance Method Summary collapse
-
#call(value) ⇒ Any?
(also: #===, #[])
Calls the pattern matcher, yielding the target value to the first matching branch it encounters.
-
#initialize(destructure: false) {|_self| ... } ⇒ Qo::PatternMatchers::PatternMatch
constructor
Creates a new instance of a pattern matcher.
-
#to_proc ⇒ Proc[Any] => Any
Procified version of
call
.
Methods included from Branching
Constructor Details
#initialize(destructure: false) {|_self| ... } ⇒ Qo::PatternMatchers::PatternMatch
Creates a new instance of a pattern matcher
25 26 27 28 29 30 31 |
# File 'lib/qo/pattern_matchers/pattern_match.rb', line 25 def initialize(destructure: false, &fn) @matchers = [] @default = nil @destructure = destructure yield(self) if block_given? end |
Class Method Details
.create(branches: []) ⇒ Class
Allows for the creation of an anonymous PatternMatcher based on this parent class. To be used by people wishing to make their own pattern matchers with variant branches and other features not included in the defaultly provided ones
44 45 46 47 48 |
# File 'lib/qo/pattern_matchers/pattern_match.rb', line 44 def self.create(branches: []) Class.new(Qo::PatternMatchers::PatternMatch) do branches.each { |branch| register_branch(branch.new) } end end |
.mixin(destructure: false, as: :match) ⇒ Module
Allows for the injection of a pattern matching function into a type class for direct access, rather than yielding an instance of that class to a pattern matcher.
This is typically done for monadic types that need to match
. When
combined with extractor type branches it can be very handy for dealing
with container types.
124 125 126 127 128 129 130 131 132 |
# File 'lib/qo/pattern_matchers/pattern_match.rb', line 124 def self.mixin(destructure: false, as: :match) create_self = -> &function { new(destructure: destructure, &function) } Module.new do define_method(as) do |&function| create_self.call(&function).call(self) end end end |
Instance Method Details
#call(value) ⇒ Any? Also known as: ===, []
Calls the pattern matcher, yielding the target value to the first matching branch it encounters.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/qo/pattern_matchers/pattern_match.rb', line 145 def call(value) @matchers.each do |matcher| status, return_value = matcher.call(value) return return_value if status end if @default _, return_value = @default.call(value) return_value else nil end end |
#to_proc ⇒ Proc[Any] => Any
Procified version of call
165 166 167 |
# File 'lib/qo/pattern_matchers/pattern_match.rb', line 165 def to_proc -> target { self.call(target) } end |