From c9409048134f41548a5cf960f3754243dc706420 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 18 Mar 2024 07:29:25 +0100 Subject: [PATCH 1/3] solution2.2.6.1_flatMap provided Signed-off-by: Andreas Roehler --- .../worksheets/solution2.2.6.1_flatMap.scala | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 chapter02/worksheets/solution2.2.6.1_flatMap.scala diff --git a/chapter02/worksheets/solution2.2.6.1_flatMap.scala b/chapter02/worksheets/solution2.2.6.1_flatMap.scala new file mode 100644 index 0000000..2ca8a9b --- /dev/null +++ b/chapter02/worksheets/solution2.2.6.1_flatMap.scala @@ -0,0 +1,43 @@ +/** + Exercise 2.2.6.1 + + Implement a function fromPairs that performs the inverse + transformation to the toPairs function defined in Example 2.2.5.6. + + The required type signature and a sample test are: + + def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = ??? + + scala> fromPairs(List((1,2), (3,4))) + res1: Seq[Int] = List(1, 2, 3, 4) + + scala> fromPairs(List((a,b), (c,))) + res1: Seq[(String, String)] = List("a", "b", "c", "") + + Hint: This can be done with foldLeft or with flatMap. + */ + +object FromPairs { + def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = { + xs.flatMap { x => x.toList } + } + def main(args: Array[String]) = { + val result: Seq[String] = fromPairs(List(("a","b"), ("c",""))) + val expected: Seq[String] = List("a", "b", "c", "") + println("result: %s".format(result)) + assert(result == expected) + val a: Seq[Int] = fromPairs(List((1, 2), (3, 4))) + val b: Seq[Int] = List(1, 2, 3, 4) + println("a: %s".format(a)) + assert(a == b) + } +} + +FromPairs.main(Array()) + +// scala> :load solution2.2.6.1_flatMap.scala +// :load solution2.2.6.1_flatMap.scala +// result: List(a, b, c, ) +// a: List(1, 2, 3, 4) +// // defined object FromPairs + From efc740dd35385bd875d354439851020e70d9d63e Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 18 Mar 2024 10:22:06 +0100 Subject: [PATCH 2/3] avoid toList Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.2.6.1_flatMap.scala | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/chapter02/worksheets/solution2.2.6.1_flatMap.scala b/chapter02/worksheets/solution2.2.6.1_flatMap.scala index 2ca8a9b..c65bec4 100644 --- a/chapter02/worksheets/solution2.2.6.1_flatMap.scala +++ b/chapter02/worksheets/solution2.2.6.1_flatMap.scala @@ -19,7 +19,10 @@ object FromPairs { def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = { - xs.flatMap { x => x.toList } + + xs.flatMap{ case (x1, x2) => List(x1, x2) } + + } def main(args: Array[String]) = { val result: Seq[String] = fromPairs(List(("a","b"), ("c",""))) From 99eba42a942fb4ddf45b7d0d823b13d012ec566c Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Mon, 18 Mar 2024 10:24:05 +0100 Subject: [PATCH 3/3] formatting Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.2.6.1_flatMap.scala | 3 --- 1 file changed, 3 deletions(-) diff --git a/chapter02/worksheets/solution2.2.6.1_flatMap.scala b/chapter02/worksheets/solution2.2.6.1_flatMap.scala index c65bec4..6fa23a5 100644 --- a/chapter02/worksheets/solution2.2.6.1_flatMap.scala +++ b/chapter02/worksheets/solution2.2.6.1_flatMap.scala @@ -19,10 +19,7 @@ object FromPairs { def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = { - xs.flatMap{ case (x1, x2) => List(x1, x2) } - - } def main(args: Array[String]) = { val result: Seq[String] = fromPairs(List(("a","b"), ("c","")))