Module: Enumerable
- Defined in:
- lib/lite/ruby/enumerable.rb
Instance Method Summary collapse
- #cluster ⇒ Object
- #cluster_by(&block) ⇒ Object
- #deduce(identity = 0, &block) ⇒ Object
- #drop_last(num) ⇒ Object
- #drop_last_if ⇒ Object
- #exactly?(num) ⇒ Boolean
-
#excase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality.
-
#exclude?(object) ⇒ Boolean
rubocop:enable Style/CaseEquality.
- #expand ⇒ Object
- #exponential(identity = 0, &block) ⇒ Object
- #frequency ⇒ Object (also: #occurrences)
-
#incase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality.
-
#interpose(sep, &block) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#many? ⇒ Boolean
rubocop:enable Metrics/MethodLength.
- #modulate(modulo) ⇒ Object
-
#occur(amount = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength.
-
#produce(identity = 0, &block) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength.
- #quotient(identity = 0, &block) ⇒ Object
- #several? ⇒ Boolean
-
#squeeze(*limited_to) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#take_last(num) ⇒ Object
rubocop:enable Metrics/MethodLength.
- #take_last_if ⇒ Object
Instance Method Details
#cluster ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 |
# File 'lib/lite/ruby/enumerable.rb', line 5 def cluster each_with_object([]) do |ele, results| last_res = results.last if last_res && (yield(ele) == yield(last_res.last)) last_res << ele else results << [ele] end end end |
#cluster_by(&block) ⇒ Object
17 18 19 |
# File 'lib/lite/ruby/enumerable.rb', line 17 def cluster_by(&block) group_by(&block).sort.transpose.pop || [] end |
#deduce(identity = 0, &block) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/lite/ruby/enumerable.rb', line 21 def deduce(identity = 0, &block) if block_given? map(&block).deduce(identity) else inject { |acc, val| acc - val } || identity end end |
#drop_last(num) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/lite/ruby/enumerable.rb', line 29 def drop_last(num) collection_size = to_a.size return self if num > collection_size self[0...(collection_size - num)] end |
#drop_last_if ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/lite/ruby/enumerable.rb', line 36 def drop_last_if dropping = true reverse_each.with_object([]) do |val, arr| next if dropping &&= yield(val) arr.unshift(val) end end |
#exactly?(num) ⇒ Boolean
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/lite/ruby/enumerable.rb', line 45 def exactly?(num) found_count = 0 if block_given? each { |*opt| found_count += 1 if yield(*opt) } else each { |opt| found_count += 1 if opt } end found_count > num ? false : num == found_count end |
#excase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality
58 59 60 |
# File 'lib/lite/ruby/enumerable.rb', line 58 def excase?(object) none? { |val| object === val } end |
#exclude?(object) ⇒ Boolean
rubocop:enable Style/CaseEquality
63 64 65 |
# File 'lib/lite/ruby/enumerable.rb', line 63 def exclude?(object) !include?(object) end |
#expand ⇒ Object
67 68 69 |
# File 'lib/lite/ruby/enumerable.rb', line 67 def map { |val| val.is_a?(Enumerable) ? val. : val } end |
#exponential(identity = 0, &block) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/lite/ruby/enumerable.rb', line 71 def exponential(identity = 0, &block) if block_given? map(&block).exponential(identity) else inject { |acc, val| acc**val } || identity end end |
#frequency ⇒ Object Also known as: occurrences
79 80 81 |
# File 'lib/lite/ruby/enumerable.rb', line 79 def frequency each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 } end |
#incase?(object) ⇒ Boolean
rubocop:disable Style/CaseEquality
86 87 88 |
# File 'lib/lite/ruby/enumerable.rb', line 86 def incase?(object) any? { |val| object === val } end |
#interpose(sep, &block) ⇒ Object
rubocop:disable Metrics/MethodLength
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/lite/ruby/enumerable.rb', line 92 def interpose(sep, &block) enum = Enumerator.new do |val| items = each loop do begin val << items.next rescue StopIteration break end begin items.peek rescue StopIteration break else val << sep end end end block ? enum.each(&block) : enum end |
#many? ⇒ Boolean
rubocop:enable Metrics/MethodLength
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/lite/ruby/enumerable.rb', line 117 def many? found_count = 0 if block_given? any? do |val| found_count += 1 if yield(val) found_count > 1 end else any? { (found_count += 1) > 1 } end end |
#modulate(modulo) ⇒ Object
130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/lite/ruby/enumerable.rb', line 130 def modulate(modulo) if modulo == 1 to_a elsif size % modulo != 0 raise ArgumentError, "Invalid modulo: #{modulo.inspect}" else (0...size).each_with_object(Array.new(modulo, [])) do |i, array| array[i % modulo] += [self[i]] end end end |
#occur(amount = nil) ⇒ Object
rubocop:disable Metrics/AbcSize, Metrics/MethodLength
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/lite/ruby/enumerable.rb', line 143 def occur(amount = nil) result = Hash.new { |hash, key| hash[key] = [] } each do |item| key = item result[key] << item end if block_given? result.select! { |_key, values| yield(values.size) } else raise ArgumentError, 'Invalid occur amount' unless amount if amount.is_a?(Range) result.select! { |_key, values| amount.include?(values.size) } else result.select! { |_key, values| values.size == amount } end end result.values.flatten.uniq end |
#produce(identity = 0, &block) ⇒ Object
rubocop:enable Metrics/AbcSize, Metrics/MethodLength
167 168 169 170 171 172 173 |
# File 'lib/lite/ruby/enumerable.rb', line 167 def produce(identity = 0, &block) if block_given? map(&block).produce(identity) else inject { |acc, val| acc * val } || identity end end |
#quotient(identity = 0, &block) ⇒ Object
175 176 177 178 179 180 181 |
# File 'lib/lite/ruby/enumerable.rb', line 175 def quotient(identity = 0, &block) if block_given? map(&block).quotient(identity) else inject { |acc, val| acc / val } || identity end end |
#several? ⇒ Boolean
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/lite/ruby/enumerable.rb', line 183 def several? found_count = 0 if block_given? each { |*opt| found_count += 1 if yield(*opt) } else each { |opt| found_count += 1 if opt } end found_count > 1 end |
#squeeze(*limited_to) ⇒ Object
rubocop:disable Metrics/MethodLength
196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/lite/ruby/enumerable.rb', line 196 def squeeze(*limited_to) first = true current = nil each_with_object([]) do |val, array| if !limited_to.empty? && !limited_to.include?(val) array << val elsif first || current != val array << val first = false current = val end end end |
#take_last(num) ⇒ Object
rubocop:enable Metrics/MethodLength
212 213 214 215 216 217 |
# File 'lib/lite/ruby/enumerable.rb', line 212 def take_last(num) collection_size = to_a.size return self if num > collection_size self[(collection_size - num)..-1] end |
#take_last_if ⇒ Object
219 220 221 222 223 224 225 |
# File 'lib/lite/ruby/enumerable.rb', line 219 def take_last_if reverse_each.with_object([]) do |val, arr| break arr unless yield(val) arr.unshift(val) end end |