>>361と同様に、(再帰の代わりに)「畳み込み」を用いた関数型プログラミング作法をRubyで
[1, 3, 4, 5, 7, 9, 10, 11, 13].inject([]) { |acc, x|
if acc.empty?
[[x]]
else
if acc.last.last.succ == x
acc[0 .. -2] + [acc.last + [x]]
else
acc + [[x]]
end
end
}
# => [[1], [3, 4, 5], [7], [9, 10, 11], [13]]