Module: RMTools::ValueTraversal

Included in:
Dir, ValueTraversable
Defined in:
lib/rmtools/enumerable/traversal.rb

Overview

It’s included into Dir and could also be included into Enumerable Though we don’t do latter automatically, because Enumerable as it is overloaded of a bunch of questionable methods

Instance Method Summary collapse

Instance Method Details

#depth_first_find(&b) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rmtools/enumerable/traversal.rb', line 35

def depth_first_find(&b)
  to_traversable.each {|e| 
    if v.respond_to? :depth_first_find
      if res = e.depth_first_find(&b)
        return res
      end
    else
      return e if b[e]
    end
  }
  nil
end

#depth_first_map(&b) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/rmtools/enumerable/traversal.rb', line 56

def depth_first_map(&b)
  to_traversable.map {|e| 
    if e.respond_to? :depth_map
      e.depth_map &b
    else
      b[e]
    end
  }
end

#depth_first_selectObject



52
53
54
# File 'lib/rmtools/enumerable/traversal.rb', line 52

def depth_first_select
  res = []; depth_first_traverse {|e| res << e if yield(e)}; res
end

#depth_first_traverse(&b) ⇒ Object



19
20
21
# File 'lib/rmtools/enumerable/traversal.rb', line 19

def depth_first_traverse(&b)
  to_traversable.flatten.each &b
end

#preorder_find(&b) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rmtools/enumerable/traversal.rb', line 23

def preorder_find(&b)
  next_level = RMTools::ValueTraversable.new
  to_traversable.each {|e|
    if e.respond_to? :preorder_find
      next_level += e
    else
      return e if b[e]
    end
  }
  next_level.preorder_find(&b) if next_level.any?
end

#preorder_selectObject



48
49
50
# File 'lib/rmtools/enumerable/traversal.rb', line 48

def preorder_select
  res = []; preorder_traverse {|e| res << e if yield(e)}; res
end

#preorder_traverse(&b) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rmtools/enumerable/traversal.rb', line 7

def preorder_traverse(&b)
  next_level = RMTools::ValueTraversable.new
  to_traversable.each {|e|
    if e.respond_to? :preorder_traverse
      next_level += e
    else
      b[e]
    end
  }
  next_level.preorder_traverse(&b) if next_level.any?
end