# THIS IS DEPRECATED IN FAVOR OF ENUMERATOR'S VERSION
require 'enumerator'

=begin

module Enumerable

  # Iterate through slices. If slicing _step_ is not
  # given, the the arity if the block is used.
  #
  #   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] ]
  #
  def each_slice(step=nil, &yld)
    if step
      cnt = step
      n = length / cnt
      n += 1 if length % cnt > 0
      n.times { |i|
        yld.call(*self.slice(i*cnt,cnt))
      }
      return cnt
    else
      cnt = yld.arity.abs
      n = length / cnt
      n += 1 if length % cnt > 0
      n.times { |i|
        yld.call(*self.slice(i*cnt,cnt))
      }
      return cnt
    end
  end

  alias_method( :each_by, :each_slice )

end


class Array

  # Iterates over +n+ elements at a time. If +n+ is
  # not given then the arity of the block determines the
  # slicing quantity.
  #
  #   [1, 2, 3, 4].each_slice(2){ |a,b| ... }
  #
  # This is a specialized version Enumerable#each_slice
  # but optimized specifically for Array.
  def each_slice(n=nil, &yld)
    n = yld.arity.abs unless n
    i=0
    while i < self.length
      yld.call(*self.slice(i,n))
      i+=n
    end
  end

  alias_method( :each_by, :each_slice )

end

=end



#  _____         _
# |_   _|__  ___| |_
#   | |/ _ \/ __| __|
#   | |  __/\__ \ |_
#   |_|\___||___/\__|
#
=begin test

  require 'test/unit'

  class TCEnumerable < Test::Unit::TestCase

    def test_01
      x = []
      [1,2,3,4].each_slice(2){ |a,b| x << [a,b] }
      o = [[1,2],[3,4]]
      assert_equal( o, x )
    end

    def test_02
      x = []
      [1,2,3,4,5,6,7,8].each_slice(4){ |*a| x << a }
      o = [[[1,2,3,4]],[[5,6,7,8]]]
      assert_equal( o, x )
    end

    def test_03
      x = []
      [1,2,3,4,5,6].each_slice(3){ |a| x << a }
      o = [[1,2,3],[4,5,6]]
      assert_equal( o, x )
    end

    def test_04
      a = [1,2,3,4,5,6]
      r = []
      a.each_slice(2){ |x,y| r << [x,y] }
      assert_equal( [[1,2],[3,4],[5,6]], r )
    end

    def test_05
      a = [1,2,3,4,5,6]
      r = []
      a.each_slice(3){ |e| r << e }
      assert_equal( [[1,2,3],[4,5,6]], r )
    end

  end

=end
