Provides the cross-product of two or more Enumerables. This is the class-level method. The instance method calls on this.
require 'facet/enumerable/cross' Enumerable.cross([1,2], [4], ["apple", "banana"]) #=> [[1, 4, "apple"], [1, 4, "banana"], [2, 4, "apple"], [2, 4, "banana"]] Enumerable.cross([1,2], [3,4]) #=> [[1, 3], [1, 4], [2, 3], [2, 4]]
Operator alias for cross-product.
require 'facet/enumerable/cross' a = [1,2] ** [4,5] a #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
Same as collect but with an iteration counter.
require 'facet/enumerable/collect_with_counter' a = [1,2,3].collect_with_counter{ |e,i| e*i } a #=> [0,2,6]
The instance level cross-product method.
require 'facet/enumerable/cross' a = [] [1,2].cross([4,5]){|elem| a << elem } a #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
Iterate through slices. If slicing step is not given, the the arity if the block is used.
require 'facet/enumerable/each_slice' x = [] [1,2,3,4].each_slice(2){ |a,b| x << [a,b] } x #=> [ [1,2], [3,4] ] x = [] [1,2,3,4,5,6].each_slice(3){ |a| x << a } x #=> [ [1,2,3], [4,5,6] ]
Uses a Functor to provide R-like per element method application.
require 'facet/enumerable/every' a = [1,2,3] b = [4,5] p a.every + 3 => [4,5,6] p a.every + b => [5,7] p a.every.+(b,2) => [[5,7],[3,4,5]]
Note that this method is still undergoing some fine tuning. It is questionable as to whether different sized arrays should be padded with nil (or a parameter) and whether multiple parameters are a good idea (e.g. a.%.+(b,2,…)).
Like map/collect, but it generates a Hash. The block is expected to return two values: the key and the value for the new hash.
require 'facet/enumberable/graph' numbers = (1..3) squares = numbers.graph { |n| [n, n*n] } # { 1=>1, 2=>4, 3=>9 } sq_roots = numbers.graph { |n| [n*n, n] } # { 1=>1, 4=>2, 9=>3 }
Enumerable#none? is the logical opposite of the builtin method Enumerable#any?. It returns true if and only if none of the elements in the collection satisfy the predicate.
If no predicate is provided, Enumerable#none? returns true if and only if none of the elements have a true value (i.e. not nil or false).
require 'facet/enumerable/none?' [].none? # true [nil].none? # true [5,8,9].none? # false (1...10).none? { |n| n < 0 } # true (1...10).none? { |n| n > 0 } # false
Enumerable#one? returns true if and only if exactly one element in the collection satisfies the given predicate.
If no predicate is provided, Enumerable#one? returns true if and only if exactly one element has a true value (i.e. not nil or false).
require 'facet/enumerable/one?' [].one? # false [nil].one? # false [5].one? # true [5,8,9].one? # false (1...10).one? { |n| n == 5 } # true (1...10).one? { |n| n < 5 } # false
See Enumerable#partition for the background. partition_by is best explained by example.
require 'facet/enumerable/partition_by' (1..5).partition_by { |n| n % 3 } #=> { 0 => [3], 1 => [1, 4], 2 => [2,5] } ["I had", 1, "dollar and", 50, "cents"].partition_by { |e| e.class } #=> { String => ["I had","dollar and","cents"], Fixnum => [1,50] }
partition_by is used to group items in a collection by something they have in common. The common factor is the key in the resulting hash, the array of like elements is the value.
Produces a hash from an Array with index for keys.
require 'facet/array/to_hash' a1 = [ :a, :b ] a1.to_hash #=> { 0=>:a, 1=>:b }