Sha256: 4488cc4c752fa5fc92b71b7a15a5733e8322fe8aebd597b43f12f6a3808f9f9e

Contents?: true

Size: 554 Bytes

Versions: 2

Compression:

Stored size: 554 Bytes

Contents

module Enumerable

  # Clusters together adjacent elements into a list of sub-arrays.
  #
  #     [2,2,2,3,3,4,2,2,1].cluster{ |x| x }
  #     => [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
  #
  #     ["dog", "duck", "cat", "dude"].cluster{ |x| x[0] }
  #     => [["dog", "duck"], ["cat"], ["dude"]]
  #
  # @author Oleg K

  def cluster
    cluster = []
    each do |element|
      if cluster.last && yield(cluster.last.last) == yield(element)
        cluster.last << element
      else
        cluster << [element]
      end
    end
    cluster
  end

end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
facets-glimmer-3.2.0 lib/core/facets/enumerable/cluster.rb
facets-3.1.0 lib/core/facets/enumerable/cluster.rb