Module Enumerable
In: lib/facet/enumerable/none?.rb
lib/facet/enumerable/**.rb
lib/facet/enumerable/graph.rb
lib/facet/enumerable/partition_by.rb
lib/facet/enumerable/collect_with_counter.rb
lib/facet/enumerable/every.rb
lib/facet/enumerable/per.rb
lib/facet/enumerable/one?.rb
lib/facet/enumerable/each_slice.rb
lib/facet/enumerable/to_hash.rb
lib/facet/enumerable/uniq_by.rb
lib/facet/enumerable/cross.rb

Methods

Public Class methods

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]]

Public Instance methods

%()

Alias for every

Operator alias for cross-product.

  require 'facet/enumerable/cross'

  a = [1,2] ** [4,5]
  a  #=> [[1, 4],[1, 5],[2, 4],[2, 5]]
build_hash(&yld)

Alias for graph

build_hash!( &blk )

Alias for graph!

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]
collect_with_index()

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]]
each_by(step=nil, &yld)

Alias for each_slice

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 }

As with map_hash but acts in place.

map_with_counter()
map_with_index()

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.

per()

Alias for every

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 }

Like uniq, but determines uniqueness based on a given block.

  require 'facet/enumerable/uniq_by'

  (-5..5).to_a.uniq_by {|i| i*i }

produces

  [-5, -4, -3, -2, -1, 0]

[Validate]