generalized takingWhile