From ec0cc256f15d35cac1ec62530de53c15f12de387 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 4 Mar 2024 10:23:51 +0100 Subject: [PATCH 1/4] Example2.2.5.6 added Signed-off-by: Andreas Roehler --- chapter02/worksheets/Example2.2.5.6.scala | 39 +++++++++++++++++++ .../worksheets/Example2.2.5.6_object.scala | 34 ++++++++++++++++ .../Example2.2.5.6_object_main.scala | 30 ++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 chapter02/worksheets/Example2.2.5.6.scala create mode 100644 chapter02/worksheets/Example2.2.5.6_object.scala create mode 100644 chapter02/worksheets/Example2.2.5.6_object_main.scala diff --git a/chapter02/worksheets/Example2.2.5.6.scala b/chapter02/worksheets/Example2.2.5.6.scala new file mode 100644 index 0000000..523200a --- /dev/null +++ b/chapter02/worksheets/Example2.2.5.6.scala @@ -0,0 +1,39 @@ +/** +Example 2.2.5.6 + +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,)) + +def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { + type Acc = (Seq[(A, A)], Seq[A]) + // Type alias, for brevity. + def init: Acc = (Seq(), Seq()) + def updater(acc: Acc, x: A): Acc = acc match { + case (result, Seq()) + => (result, Seq(x)) + case (result, Seq(prev)) => (result :+ ((prev, x)), Seq()) + } + val (result, holdover) = xs.foldLeft(init)(updater) + holdover match { + // May need to append the last element to the result. + case Seq() + => result + case Seq(x) + => result :+ ((x, default)) + } +} + +val a = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +println("a: %s".format(a)) +// diff --git a/chapter02/worksheets/Example2.2.5.6_object.scala b/chapter02/worksheets/Example2.2.5.6_object.scala new file mode 100644 index 0000000..011e09a --- /dev/null +++ b/chapter02/worksheets/Example2.2.5.6_object.scala @@ -0,0 +1,34 @@ + +object ToPairsObject { + def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { + type Acc = (Seq[(A, A)], Seq[A]) + + // Type alias, for brevity. + def init: Acc = (Seq(), Seq()) + + def updater(acc: Acc, x: A): Acc = acc match { + case (result, Seq()) + => (result, Seq(x)) + case (result, Seq(prev)) => (result :+ ((prev, x)), Seq()) + + } + + val (result, holdover) = xs.foldLeft(init)(updater) + holdover match { + // May need to append the last element to the result. + case Seq() + => result + case Seq(x) + => result :+ ((x, default)) + } + } +} + +val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4) +println("a: %s".format(a)) + +// 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)) diff --git a/chapter02/worksheets/Example2.2.5.6_object_main.scala b/chapter02/worksheets/Example2.2.5.6_object_main.scala new file mode 100644 index 0000000..8978c1a --- /dev/null +++ b/chapter02/worksheets/Example2.2.5.6_object_main.scala @@ -0,0 +1,30 @@ + +object ToPairsObject { + def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { + type Acc = (Seq[(A, A)], Seq[A]) + + // Type alias, for brevity. + def init: Acc = (Seq(), Seq()) + + def updater(acc: Acc, x: A): Acc = acc match { + case (result, Seq()) + => (result, Seq(x)) + case (result, Seq(prev)) => (result :+ ((prev, x)), Seq()) + + } + + val (result, holdover) = xs.foldLeft(init)(updater) + holdover match { + // May need to append the last element to the result. + case Seq() + => result + case Seq(x) + => result :+ ((x, default)) + } + + } + def main(args: Array[String]): Unit = { + val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4) + println("a: %s".format(a)) + } +} From d56afb62de5ab4e1e394ff1a16e4b4c16b4a1df5 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 4 Mar 2024 13:00:56 +0100 Subject: [PATCH 2/4] Example2.2.5.6 again Signed-off-by: Andreas Roehler --- .../worksheets/Example2.2.5.6_object.scala | 8 ++++ .../Example2.2.5.6_object_main.scala | 8 ++++ .../Example2.2.5.6_object_main_privat.scala | 39 +++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 chapter02/worksheets/Example2.2.5.6_object_main_privat.scala diff --git a/chapter02/worksheets/Example2.2.5.6_object.scala b/chapter02/worksheets/Example2.2.5.6_object.scala index 011e09a..efcb77b 100644 --- a/chapter02/worksheets/Example2.2.5.6_object.scala +++ b/chapter02/worksheets/Example2.2.5.6_object.scala @@ -1,3 +1,11 @@ +/** +Example 2.2.5.6 + +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: + */ object ToPairsObject { def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { diff --git a/chapter02/worksheets/Example2.2.5.6_object_main.scala b/chapter02/worksheets/Example2.2.5.6_object_main.scala index 8978c1a..e016037 100644 --- a/chapter02/worksheets/Example2.2.5.6_object_main.scala +++ b/chapter02/worksheets/Example2.2.5.6_object_main.scala @@ -1,3 +1,11 @@ +/** +Example 2.2.5.6 + +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: + */ object ToPairsObject { def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { diff --git a/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala b/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala new file mode 100644 index 0000000..458f89a --- /dev/null +++ b/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala @@ -0,0 +1,39 @@ +/** +Example 2.2.5.6 + +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: + */ + +object ToPairsObject { + private def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] = { + type Acc = (Seq[(A, A)], Seq[A]) + + // Type alias, for brevity. + def init: Acc = (Seq(), Seq()) + + def updater(acc: Acc, x: A): Acc = acc match { + case (result, Seq()) + => (result, Seq(x)) + case (result, Seq(prev)) => (result :+ ((prev, x)), Seq()) + + } + + val (result, holdover) = xs.foldLeft(init)(updater) + holdover match { + // May need to append the last element to the result. + case Seq() + => result + case Seq(x) + => result :+ ((x, default)) + } + + } + def main(args: Array[String]): Unit = { + val a = ToPairsObject.toPairs(Seq(1, 2, 3), 4) + println("a: %s".format(a)) + } +} + From 03dc29fda7c0fe5386338f1d445ab49f40a107b2 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 4 Mar 2024 20:59:02 +0100 Subject: [PATCH 3/4] add assertions Signed-off-by: Andreas Roehler --- chapter02/worksheets/Example2.2.5.6.scala | 23 +++++++++++-------- .../worksheets/Example2.2.5.6_object.scala | 21 +++++++++++------ .../Example2.2.5.6_object_main.scala | 16 +++++++++++++ .../Example2.2.5.6_object_main_privat.scala | 18 ++++++++++++++- 4 files changed, 61 insertions(+), 17 deletions(-) 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) From 7ddd570018d28c8d4644c3a951584a2c50b085ef Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Tue, 5 Mar 2024 08:29:54 +0100 Subject: [PATCH 4/4] Example2.2.5.6.scala fixed Signed-off-by: Andreas Roehler --- chapter02/worksheets/Example2.2.5.6.scala | 7 +++++++ .../worksheets/Example2.2.5.6_object.scala | 12 ++++++++++-- .../worksheets/Example2.2.5.6_object_main.scala | 12 ++++++++++-- .../Example2.2.5.6_object_main_privat.scala | 17 ++++++++--------- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/chapter02/worksheets/Example2.2.5.6.scala b/chapter02/worksheets/Example2.2.5.6.scala index 3e09d81..1f317aa 100644 --- a/chapter02/worksheets/Example2.2.5.6.scala +++ b/chapter02/worksheets/Example2.2.5.6.scala @@ -42,3 +42,10 @@ val a = toPairs(Seq("a", "b", "c"), "") val b: Seq[(String, String)] = List(("a","b"), ("c","")) assert(a == b) +// scala> :load Example2.2.5.6.scala +// :load Example2.2.5.6.scala +// def toPairs[A](xs: Seq[A], default: A): Seq[(A, A)] +// val result: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +// val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +// val a: Seq[(String, String)] = List((a,b), (c,)) +// val b: Seq[(String, String)] = List((a,b), (c,)) diff --git a/chapter02/worksheets/Example2.2.5.6_object.scala b/chapter02/worksheets/Example2.2.5.6_object.scala index 42865f6..db62bfb 100644 --- a/chapter02/worksheets/Example2.2.5.6_object.scala +++ b/chapter02/worksheets/Example2.2.5.6_object.scala @@ -40,10 +40,18 @@ object ToPairsObject { } } -val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +val result = ToPairsObject.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 a = ToPairsObject.toPairs(Seq("a", "b", "c"), "") val b: Seq[(String, String)] = List(("a","b"), ("c","")) assert(a == b) + +// scala> :load Example2.2.5.6_object.scala +// :load Example2.2.5.6_object.scala +// // defined object ToPairsObject +// val result: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +// val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +// val a: Seq[(String, String)] = List((a,b), (c,)) +// val b: Seq[(String, String)] = List((a,b), (c,)) diff --git a/chapter02/worksheets/Example2.2.5.6_object_main.scala b/chapter02/worksheets/Example2.2.5.6_object_main.scala index 423b5ab..6c88f1d 100644 --- a/chapter02/worksheets/Example2.2.5.6_object_main.scala +++ b/chapter02/worksheets/Example2.2.5.6_object_main.scala @@ -45,10 +45,18 @@ object ToPairsObject { } } -val result = toPairs(Seq(1, 2, 3, 4, 5, 6), -1) +val result = ToPairsObject.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 a = ToPairsObject.toPairs(Seq("a", "b", "c"), "") val b: Seq[(String, String)] = List(("a","b"), ("c","")) assert(a == b) + +// scala> :load Example2.2.5.6_object_main.scala +// :load Example2.2.5.6_object_main.scala +// // defined object ToPairsObject +// val result: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +// val expected: Seq[(Int, Int)] = List((1,2), (3,4), (5,6)) +// val a: Seq[(String, String)] = List((a,b), (c,)) +// val b: Seq[(String, String)] = List((a,b), (c,)) 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 203a72e..d267601 100644 --- a/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala +++ b/chapter02/worksheets/Example2.2.5.6_object_main_privat.scala @@ -40,16 +40,15 @@ object ToPairsObject { } def main(args: Array[String]): Unit = { - val result = ToPairsObject.toPairs(Seq(1, 2, 3), 4) - assert(result == expected) + 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) 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)