further to this stuff, a ruby version:
class Array
def group_seq
reduce([]) do |acc, obj|
if acc != [] && (yield obj) == (yield acc.last.first)
acc.last << obj
acc
else
acc << [obj]
end
end
end
end
The (yield obj) == (yield acc.last.first) will mean that things get grouped according to the value of the predicate. If you change the == to &&, then only values for which the predicate is true will be grouped.
It turns out that python already has this in it’s standard library, so that’s easy:
map(lambda (b,i): list(i), groupby([1,2,3,4,5,6,7,8], lambda n: n % 2 == 0))