Class: PatternMatch::PatternSetDeconstructor

Inherits:
PatternDeconstructor show all
Defined in:
lib/pattern-match/experimental.rb

Instance Attribute Summary

Attributes inherited from Pattern

#next, #parent, #prev

Instance Method Summary collapse

Methods inherited from PatternElement

#quantifier?

Methods inherited from Pattern

#!@, #&, #ancestors, #append, #directly_quantified?, #inspect, #quantified?, #quantifier?, #quasibinding, #root, #root?, #to_a, #validate, #vars, #|

Methods included from PatternMatch::Pattern::Backtrackable

#choice_points

Constructor Details

#initialize(klass, *subpatterns) ⇒ PatternSetDeconstructor

Returns a new instance of PatternSetDeconstructor.



140
141
142
143
# File 'lib/pattern-match/experimental.rb', line 140

def initialize(klass, *subpatterns)
  super(*subpatterns)
  @klass = klass
end

Instance Method Details

#match(vals) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/pattern-match/experimental.rb', line 145

def match(vals)
  super do |val|
    next false unless val.kind_of?(@klass)
    members = val.to_a
    next false unless subpatterns.length <= members.length
    members.permutation(subpatterns.length).find do |perm|
      cont = nil
      if callcc {|c| cont = c; perm.zip(subpatterns).all? {|i, pat| pat.match([i]) } }
        save_choice_point(cont)
        true
      else
        false
      end
    end
  end
end