-
Notifications
You must be signed in to change notification settings - Fork 0
/
P09.scala
33 lines (29 loc) · 960 Bytes
/
P09.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package s99.p09
object P09 {
def pack[T](input: List[T]): List[List[T]] =
input.foldRight(List(Nil): List[List[T]]) {
(e, accu) => accu match {
case List(Nil) => List(List(e))
case (h :: t) :: o if h == e => (e :: h :: t) :: o
case curList :: o => List(e) :: curList :: o
}
}
// no helper functions used like fold:
def packOld[T](input: List[T]): List[List[T]] = {
def packWithPrevious(remaining: List[T])(previous: List[T]): List[List[T]] =
remaining match {
case List() => List(previous)
case head :: tail =>
val nextIter = packWithPrevious(tail)(_)
previous match {
case List() => nextIter(List(head))
case prevHead :: _ =>
if (head != prevHead)
previous :: nextIter(List(head))
else
nextIter(head :: previous)
}
}
packWithPrevious(input)(List())
}
}