lib/lite/ruby/enumerable.rb in lite-ruby-1.0.1 vs lib/lite/ruby/enumerable.rb in lite-ruby-1.0.2
- old
+ new
@@ -12,26 +12,22 @@
results << [ele]
end
end
end
+ def cluster_by(&block)
+ group_by(&block).sort.transpose.pop || []
+ end
+
def deduce(identity = 0, &block)
if block_given?
map(&block).deduce(identity)
else
- inject { |key, val| key - val } || identity
+ inject { |acc, val| acc - val } || identity
end
end
- def divisible(identity = 0, &block)
- if block_given?
- map(&block).divisible(identity)
- else
- inject { |key, val| key / val } || identity
- end
- end
-
def drop_last(num)
collection_size = to_a.size
return self if num > collection_size
self[0...(collection_size - num)]
@@ -74,14 +70,20 @@
def exponential(identity = 0, &block)
if block_given?
map(&block).exponential(identity)
else
- inject { |key, val| key**val } || identity
+ inject { |acc, val| acc**val } || identity
end
end
+ def frequency
+ each_with_object(Hash.new(0)) { |val, hash| hash[val] += 1 }
+ end
+
+ alias occurrences frequency
+
# rubocop:disable Style/CaseEquality
def incase?(object)
any? { |val| object === val }
end
# rubocop:enable Style/CaseEquality
@@ -123,22 +125,63 @@
else
any? { (found_count += 1) > 1 }
end
end
- def multiple(identity = 0, &block)
+ 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
+
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
+ def occur(amount = nil)
+ result = Hash.new { |hash, key| hash[key] = [] }
+
+ each do |item|
+ key = item
+ result[key] << item
+ end
+
if block_given?
- map(&block).multiple(identity)
+ result.select! { |_key, values| yield(values.size) }
else
- inject { |key, val| key * val } || identity
+ 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
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength
- def occurrences
- each_with_object(Hash.new(0)) { |key, hsh| hsh[key] += 1 }
+ def produce(identity = 0, &block)
+ if block_given?
+ map(&block).produce(identity)
+ else
+ inject { |acc, val| acc * val } || identity
+ end
end
+ def quotient(identity = 0, &block)
+ if block_given?
+ map(&block).quotient(identity)
+ else
+ inject { |acc, val| acc / val } || identity
+ end
+ end
+
def several?
found_count = 0
if block_given?
each { |*opt| found_count += 1 if yield(*opt) }
@@ -146,9 +189,26 @@
each { |opt| found_count += 1 if opt }
end
found_count > 1
end
+
+ # rubocop:disable Metrics/MethodLength
+ 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
+ # rubocop:enable Metrics/MethodLength
def take_last(num)
collection_size = to_a.size
return self if num > collection_size