Class: Exercism::Processors::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/exercism-analysis/processors/processor.rb

Instance Method Summary collapse

Instance Method Details

#find_exp(exp, *types, &filter) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/exercism-analysis/processors/processor.rb', line 19

def find_exp(exp, *types, &filter)
  filter ||= -> * { true }
  exp.each.find do |node|
    if node.respond_to?(:type) && types.include?(node.type) && filter.call(node)
      node
    elsif node.respond_to?(:each)
      result = find_exp(node.each, *types, &filter)
      return result if result
    end
  end
end

#find_exps(nodes, *types, &filter) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/exercism-analysis/processors/processor.rb', line 6

def find_exps(nodes, *types, &filter)
  filter ||= -> * { true }
  nodes.each.flat_map do |node|
    if node.respond_to?(:type) && types.include?(node.type) && filter.call(node)
      [node] + find_exps(node.each, *types, &filter)
    elsif node.respond_to?(:each)
      find_exps(node.each, *types, &filter)
    else
      []
    end
  end
end