Scala - select elements from ordered list
I am looking for a nice way to remove first N elements which are equal from the ordered list, e.g.
removeSame(list) -> (1,1,1)
Is there a nice way to do it, rather than remove the head of the list and then use takeWhile on the remainder, and finally using dropwhile? I can think of simple non-functional solution but I was wondering whether any functional one also exists
The functional way to avoid the duplication of takeWhile and dropWhile to get a prefix and remainder is using span, i.e.
scala> val list = List(1,1,1,2,3,3) list: List[Int] = List(1, 1, 1, 2, 3, 3) scala> val (prefix, rest) = list span (_ == list.head) prefix: List[Int] = List(1, 1, 1) rest: List[Int] = List(2, 3, 3)