Scala - select elements from ordered list -
i looking nice way remove first n elements equal ordered list, e.g.
list(1,1,1,2,3,3) should return
removesame(list) -> (1,1,1) is there nice way it, rather remove head of list , use takewhile on remainder, , using dropwhile? can think of simple non-functional solution wondering whether functional 1 exists
the functional way avoid duplication of takewhile , dropwhile prefix , remainder 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)
Comments
Post a Comment