Sha256: 3704c891186e9303a4b6261b2d057e95fd76bbf9e20bf6b5a9808669dce0d36c

Contents?: true

Size: 411 Bytes

Versions: 5

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

5 entries across 5 versions & 2 rubygems

Version Path
facets-2.4.2 lib/core/facets/enumerable/cluster_by.rb
facets-2.4.3 lib/core/facets/enumerable/cluster_by.rb
facets-2.4.4 lib/core/facets/enumerable/cluster_by.rb
facets-2.4.5 lib/core/facets/enumerable/cluster_by.rb
mack-facets-0.8.2 lib/gems/facets-2.4.5/lib/core/facets/enumerable/cluster_by.rb