Module: Enumerable
- Defined in:
- lib/forest/rails/core_ext/enumerable.rb
Instance Method Summary collapse
-
#cluster ⇒ Object
clumps adjacent elements together >> [2,2,2,3,3,4,2,2,1].cluster{|x| x} => [[2, 2, 2], [3, 3], [4], [2, 2], [1]] apidock.com/rails/Enumerable/group_by#508-Array-clustering.
Instance Method Details
#cluster ⇒ Object
clumps adjacent elements together >> [2,2,2,3,3,4,2,2,1].cluster{|x| x}
> [[2, 2, 2], [3, 3], [4], [2, 2], [1]]
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/forest/rails/core_ext/enumerable.rb', line 6 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 |