Module: Enumerable

Defined in:
lib/modalsupport/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#build_hash(res = {}) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/modalsupport/enumerable.rb', line 117

def build_hash(res={})
  each do |x|
    pair = block_given? ? yield(x) : x
    res[pair.first] = pair.last if pair
  end
  res
end

#cross_array(*enumerables) ⇒ Object

equivalent to cross_each(*enumerables).to_a, but more efficient



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/modalsupport/enumerable.rb', line 94

def cross_array(*enumerables)
  # return to_a.product(*enumerables.map{|e| e.to_a})
  enumerables.unshift self
  result = [[]]
  while !enumerables.empty?
    t, result = result, []
    b, *enumerables = enumerables
    t.each do |a|
      b.each do |n|
        result << a + [n]
      end
    end
  end
  result
end

#cross_each(*enumerables) ⇒ Object

Cross-product iteration through multiple enumerable objects

 (1..4).cross_each([:a,:b,:c],(11..12)).to_a
 => [[1, :a, 11], [1, :a, 12], [1, :b, 11], [1, :b, 12], [1, :c, 11], [1, :c, 12],
     [2, :a, 11], [2, :a, 12], [2, :b, 11], [2, :b, 12], [2, :c, 11], [2, :c, 12],
     [3, :a, 11], [3, :a, 12], [3, :b, 11], [3, :b, 12], [3, :c, 11], [3, :c, 12],
     [4, :a, 11], [4, :a, 12], [4, :b, 11], [4, :b, 12], [4, :c, 11], [4, :c, 12]]

e.cross_each(*enums) = e.to_a.product(*enums.map{|enum| enum.to_a}).each


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/modalsupport/enumerable.rb', line 76

def cross_each(*enumerables)
  if block_given?
    enumerable, *rest = enumerables
    self.each do |e|
      if enumerables.empty?
        yield [e]
      else
        enumerable.cross_each(*rest) do |rest_e|
          yield [e]+rest_e
        end
      end
    end
  else
    enum_for(:cross_each, *enumerables)
  end
end

#each_pair(&blk) ⇒ Object



20
21
22
# File 'lib/modalsupport/enumerable.rb', line 20

def each_pair(&blk)
  each_slice(2, &blk)
end

#each_product_pair(other) ⇒ Object

cartesian product

note that for Ruby >= 1.8.7 a.each_product_pair(b).to_a == a.to_a.product(b.to_a)


33
34
35
36
37
38
39
40
41
42
43
# File 'lib/modalsupport/enumerable.rb', line 33

def each_product_pair(other)
  if block_given?
    self.each do |this|
      other.each do |that|
        yield [this, that]
      end
    end
  else
    enum_for(:each_product_pair, other)
  end
end

#grep_each(pattern) ⇒ Object



3
4
5
# File 'lib/modalsupport/enumerable.rb', line 3

def grep_each(pattern)
  grep(pattern){|str| yield(Regexp.last_match)}
end

#map_hash(res = {}) ⇒ Object



125
126
127
128
129
130
131
# File 'lib/modalsupport/enumerable.rb', line 125

def map_hash(res={})
  each do |x|
    v = yield x
    res[x] = v
  end
  res
end

#map_with_index(res = []) ⇒ Object



112
113
114
115
# File 'lib/modalsupport/enumerable.rb', line 112

def map_with_index(res=[])
  each_with_index {|x, i| res << yield(x, i)}
  res
end

#paralell_array(*enums) ⇒ Object

Equivalent to paralell_each(*enums).to_a, but more efficient



64
65
66
# File 'lib/modalsupport/enumerable.rb', line 64

def paralell_array(*enums)
  zip(*enums)
end

#paralell_each(*enums, &blk) ⇒ Object

Paralell iteration through multiple enumerable objects

(1..3).paralell_each([:a,:b,:c],(10..12)).to_a
 => [[1, :a, 10], [2, :b, 11], [3, :c, 12]]

e.paralell_each(*enums) = e.zip(*enums).each


50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/modalsupport/enumerable.rb', line 50

def paralell_each(*enums, &blk)
  # Note that to implement a lazy iterator we'd need
  # external iterators which don't work in Rubinius, are
  # implemented differently  in Ruby < 1.8.7 and are slow.
  # So, for the time being we generate an array and then
  # iterate it.
  if block_given?
    zip(*enums).each(&blk)
  else
    enum_for(:paralell_each, *enums)
  end
end

#restObject



133
134
135
# File 'lib/modalsupport/enumerable.rb', line 133

def rest
  self[1..-1] || []
end

#to_pairsObject

Convert to pairs [[e1,e2], [e3,e4], …] Note that for a Hash, this is equivalent to hash.to_a.to_pairs which may not be what’s intended; hash.to_a is an array of key-value pairs; to_pairs is an array of pairs of key-value pairs.



27
28
29
# File 'lib/modalsupport/enumerable.rb', line 27

def to_pairs
  each_pair.to_a
end