Sha256: 6755084caf6df0ab2ca63b9678cc09f118343c8e2eb16c1c46fe62e6bd853bda

Contents?: true

Size: 411 Bytes

Versions: 10

Compression:

Stored size: 411 Bytes

Contents

require 'facets/enumerable/group_by'

module Enumerable

  # Similar to #group_by but returns an array of the groups.
  # Returned elements are sorted by block.
  #
  #    %w{this is a test}.cluster_by {|x| x[0]}
  #
  # _produces_
  #
  #    [ ['a'], ['is'], ['this', 'test'] ]
  #
  # CREDIT: Erik Veenstra

  def cluster_by(&b)
    group_by(&b).sort.transpose.pop || []   # group_by(&b).values ?
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
facets-2.8.4 lib/core/facets/enumerable/cluster_by.rb
facets-2.8.3 lib/core/facets/enumerable/cluster_by.rb
facets-2.8.2 lib/core/facets/enumerable/cluster_by.rb
facets-2.8.1 lib/core/facets/enumerable/cluster_by.rb
facets-2.8.0 lib/core/facets/enumerable/cluster_by.rb
facets-2.7.0 lib/core/facets/enumerable/cluster_by.rb
facets-2.6.0 lib/core/facets/enumerable/cluster_by.rb
facets-2.5.0 lib/core/facets/enumerable/cluster_by.rb
facets-2.5.1 lib/core/facets/enumerable/cluster_by.rb
facets-2.5.2 lib/core/facets/enumerable/cluster_by.rb