# -*- coding: iso-8859-2 -*-
# Wojciech Muła
# $Id: group_elements.py,v 1.2 2007-03-14 21:37:15 wojtek Exp $

def group_elements(seq, value=lambda x: x):
	"""
	Groups adjecent elements that has some value.
	Groups is a pair: common value, list of elements.
	"""
	def aux((vp, L), curr):
		vc = value(curr)
		if vc == vp:
			L[-1][1].append(curr)
		else:
			L.append( (vc, [curr]) )
		return (vc, L)

	return reduce(aux, seq, (aux, []))[1]

