diff --git a/chapter02/worksheets/Example2.2.5.6.scala b/chapter02/worksheets/Example2.2.5.6.scala index 523200a..3e09d81 100644 --- a/chapter02/worksheets/Example2.2.5.6.scala +++ b/chapter02/worksheets/Example2.2.5.6.scala @@ -5,15 +5,15 @@ Implement a function toPairs that converts a sequence of type Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given default value of type A is used to fill the last pair. The required type signature and an example test: - */ -// def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ??? +def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ??? -// scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1) -// res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) -// scala> toPairs(Seq("a", "b", "c"), "") -// res1: Seq[(String, String)] = List((a,b), (c,)) +scala> toPairs(Seq("a", "b", "c"), "") +res1: Seq[(String, String)] = List((a,b), (c,)) + */ def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { type Acc = (Seq[(A, A)], Seq[A]) @@ -34,6 +34,11 @@ def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { } } -val a = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) -println("a: %s".format(a)) -// +val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +assert(result == expected) + +val a = toPairs(Seq("a", "b", "c"), "") +val b: Seq[(String, String)] = List(("a","b"), ("c","")) +assert(a == b) + diff --git a/chapter02/worksheets/Example2.2.5.6_object.scala b/chapter02/worksheets/Example2.2.5.6_object.scala index efcb77b..42865f6 100644 --- a/chapter02/worksheets/Example2.2.5.6_object.scala +++ b/chapter02/worksheets/Example2.2.5.6_object.scala @@ -5,6 +5,14 @@ Implement a function toPairs that converts a sequence of type Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given default value of type A is used to fill the last pair. The required type signature and an example test: + +def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ??? + +scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) + +scala> toPairs(Seq("a", "b", "c"), "") +res1: Seq[(String, String)] = List((a,b), (c,)) */ object ToPairsObject { @@ -32,11 +40,10 @@ object ToPairsObject { } } -val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4) -println("a: %s".format(a)) +val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +assert(result == expected) -// scala> :load Example2.2.5.6_object.scala -// :load Example2.2.5.6_object.scala -// a: List((1,2), (3,4)) -// // defined object ToPairsObject -// val a: Seq[(Int, Int)] = List((1,2), (3,4)) +val a = toPairs(Seq("a", "b", "c"), "") +val b: Seq[(String, String)] = List(("a","b"), ("c","")) +assert(a == b) diff --git a/chapter02/worksheets/Example2.2.5.6_object_main.scala b/chapter02/worksheets/Example2.2.5.6_object_main.scala index e016037..423b5ab 100644 --- a/chapter02/worksheets/Example2.2.5.6_object_main.scala +++ b/chapter02/worksheets/Example2.2.5.6_object_main.scala @@ -5,6 +5,14 @@ Implement a function toPairs that converts a sequence of type Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given default value of type A is used to fill the last pair. The required type signature and an example test: + +def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ??? + +scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) + +scala> toPairs(Seq("a", "b", "c"), "") +res1: Seq[(String, String)] = List((a,b), (c,)) */ object ToPairsObject { @@ -36,3 +44,11 @@ object ToPairsObject { println("a: %s".format(a)) } } + +val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +assert(result == expected) + +val a = toPairs(Seq("a", "b", "c"), "") +val b: Seq[(String, String)] = List(("a","b"), ("c","")) +assert(a == b) diff --git a/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala b/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala index 458f89a..203a72e 100644 --- a/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala +++ b/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala @@ -5,6 +5,14 @@ Implement a function toPairs that converts a sequence of type Seq[A] to a sequence of pairs, Seq[(A, A)], by putting together the adjacent elements pairwise. If the initial sequence has an odd number of elements, a given default value of type A is used to fill the last pair. The required type signature and an example test: + +def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = ??? + +scala> toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +res0: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) + +scala> toPairs(Seq("a", "b", "c"), "") +res1: Seq[(String, String)] = List((a,b), (c,)) */ object ToPairsObject { @@ -32,8 +40,16 @@ object ToPairsObject { } def main(args: Array[String]): Unit = { - val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4) + val result = ToPairsObject.toPairs(Seq(1, 2, 3), 4) + assert(result == expected) println("a: %s".format(a)) } } +val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +assert(result == expected) + +val a = toPairs(Seq("a", "b", "c"), "") +val b: Seq[(String, String)] = List(("a","b"), ("c","")) +assert(a == b)