I was wondering how to go about adding a \'partitionCount\' method to Lists, e.g.: (not tested, shamelessly based on List.scala):
Do I have to create my own sub-clas
Easy Angel is right, but the method seems pretty useless. You have already count
in order to get the number of "positives", and of course the number of "negatives" is size
minus count
.
However, to contribute something positive, here a more functional version of your original method:
def partitionCount[A](iter: Traversable[A], p: A => Boolean): (Int, Int) =
iter.foldLeft ((0,0)) { ((x,y), a) => if (p(a)) (x + 1,y) else (x, y + 1)}