diff --git a/chapter01/src/test/scala/sofp/unit/Exercises_1_6_1.scala b/chapter01/src/test/scala/sofp/unit/Exercises_1_6_1.scala index 60b1e38..b3f0245 100644 --- a/chapter01/src/test/scala/sofp/unit/Exercises_1_6_1.scala +++ b/chapter01/src/test/scala/sofp/unit/Exercises_1_6_1.scala @@ -6,7 +6,7 @@ import munit.FunSuite class Exercises_1_6_1 extends FunSuite { - test("exercise 1.6.1.1") { + test("Exercise 1.6.1.1") { def staggeredFactorial(n: Int): Int = (n to 1 by -2).product diff --git a/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala b/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala index 92d3adb..ce20fc2 100644 --- a/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala +++ b/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala @@ -5,7 +5,7 @@ import munit.FunSuite class Exercises_2_1_7 extends FunSuite { - test("exercise 2.1.7.1") { + test("Exercise 2.1.7.1") { val allPairs = (0 to 9).flatMap { i => @@ -19,4 +19,52 @@ class Exercises_2_1_7 extends FunSuite { expect(filteredPairs.length == 64) } + test("Exercise 2.1.7.3 solved by Andreas Röhler") { + val names: List[String] = List("Joe", "Bob", "Mary") + val a: List[Boolean] = List(true, false, true) + val b: List[(String, Boolean)] = names.zip(a) + val c: List[Any] = b.map { case (x, y) => if (y == true) x } + val d: List[Any] = c.filter(_ != (())) + expect(d == List("Joe", "Mary")) + } + + test("Exercise 2.1.7.3") { + val names: List[String] = List("Joe", "Bob", "Mary") + val a: List[Boolean] = List(true, false, true) + val b: List[(String, Boolean)] = names.zip(a) + val c: List[(String, Boolean)] = b.filter { case (x, y) => y } + val d: List[String] = c.map { case (x, y) => x } + expect(d == List("Joe", "Mary")) + } + + test("Exercise 2.1.7.3 as function") { + def selectTrue(names: List[String], flags: List[Boolean]): List[String] = + names.zip(flags).filter { case (name, flag) => flag }.map { case (name, flag) => name } + + def selectTrueShorter(names: List[String], flags: List[Boolean]): List[String] = + names.zip(flags).filter(_._2).map(_._1) + + def selectTrueOperatorSyntax(names: List[String], flags: List[Boolean]): List[String] = + names zip flags filter (_._2) map (_._1) + + val names = List("Joe", "Bob", "Mary") + val flags = List(true, false, true) + val expected = List("Joe", "Mary") + + expect(selectTrue(names, flags) == expected) + expect(selectTrueShorter(names, flags) == expected) + expect(selectTrueOperatorSyntax(names, flags) == expected) + + // If all flags are true, the initial list remains unchanged. + val allTrue = List(true, true, true) + expect(selectTrue(names, allTrue) == names) + expect(selectTrueShorter(names, allTrue) == names) + expect(selectTrueOperatorSyntax(names, allTrue) == names) + + // If all flags are false, the result is an empty list. + val allFalse = List(false, false, false) + expect(selectTrue(names, allFalse) == List()) + expect(selectTrueShorter(names, allFalse) == List()) + expect(selectTrueOperatorSyntax(names, allFalse) == List()) + } }