summaryrefslogtreecommitdiffstats
path: root/Scala/sandbox
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2013-05-10 01:02:36 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2016-11-10 18:03:24 +0100
commit5c5087a4a7f8342946ffe0ccbf1a244d7d4790dc (patch)
treeb433ddf494716e4c8ce66b87eda2ac09fc830e34 /Scala/sandbox
parentb9149e400c09d1f7d4bcf607d8e7b6b2c04b33d4 (diff)
downloadcoursera-5c5087a4a7f8342946ffe0ccbf1a244d7d4790dc.zip
coursera-5c5087a4a7f8342946ffe0ccbf1a244d7d4790dc.tar.gz
Scala : add 11-pack to sandbox
Diffstat (limited to 'Scala/sandbox')
-rw-r--r--Scala/sandbox/0-main.scala1
-rw-r--r--Scala/sandbox/11-pack.scala19
2 files changed, 20 insertions, 0 deletions
diff --git a/Scala/sandbox/0-main.scala b/Scala/sandbox/0-main.scala
index 1061f50..4414f12 100644
--- a/Scala/sandbox/0-main.scala
+++ b/Scala/sandbox/0-main.scala
@@ -13,5 +13,6 @@ object Main extends App {
PatternMatching.run
Lists.run
MergeSort.run
+ Pack.run
}
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))
+ }
+}