fork download
  1. module Enumerable
  2. def chunq
  3. Enumerator.new do |y|
  4. tmp = self.dup
  5. while 0 < tmp.count
  6. k = yield tmp.first
  7. a = tmp.take_while {|x| k == yield(x)}
  8. y << [k, a]
  9. tmp = tmp.drop a.size
  10. end
  11. end
  12. end
  13. end
  14. a = [1,1,2,2,3,3,3,1]
  15. p a.chunk {|x| x}.map &:first
  16. p a.chunq {|x| x}.map &:first
  17.  
Success #stdin #stdout 0s 28688KB
stdin
Standard input is empty
stdout
[1, 2, 3, 1]
[1, 2, 3, 1]