summaryrefslogtreecommitdiffstats
path: root/Scala/sandbox/11-pack.scala
diff options
context:
space:
mode:
Diffstat (limited to 'Scala/sandbox/11-pack.scala')
-rw-r--r--Scala/sandbox/11-pack.scala19
1 files changed, 19 insertions, 0 deletions
diff --git a/Scala/sandbox/11-pack.scala b/Scala/sandbox/11-pack.scala
new file mode 100644
index 0000000..d593efd
--- /dev/null
+++ b/Scala/sandbox/11-pack.scala
@@ -0,0 +1,19 @@
+object Pack
+{
+
+ def pack[T](xs: List[T]): List[List[T]] = xs match {
+ case Nil => Nil
+ case x :: xs1 =>
+ val (first, last) = xs span(y => y == x) // takeWhile + dropWhile
+ first :: pack(last)
+ }
+
+ def encode[T](xs: List[T]): List[(T,Int)] = pack(xs) map (ys => (ys.head, ys.length))
+
+ def run = {
+ println("Pack")
+ val l = List("a", "a", "a", "b", "c", "c", "a")
+ println(pack(l))
+ println(encode(l))
+ }
+}