From ab94d2e7e9d1a1ab6fdc47faf7d760d43912a334 Mon Sep 17 00:00:00 2001 From: Andreas Roehler Date: Sun, 26 Nov 2023 17:46:16 +0100 Subject: [PATCH] #14, solution2.1.7.3_SW.scala added Signed-off-by: Andreas Roehler --- chapter02/worksheets/solution2.1.7.3_SW.scala | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 chapter02/worksheets/solution2.1.7.3_SW.scala diff --git a/chapter02/worksheets/solution2.1.7.3_SW.scala b/chapter02/worksheets/solution2.1.7.3_SW.scala new file mode 100644 index 0000000..558c77c --- /dev/null +++ b/chapter02/worksheets/solution2.1.7.3_SW.scala @@ -0,0 +1,13 @@ +/** author: Sergei Winitzki */ + +/** Exercise 2.1.7.3 +Given two sequences p: Seq[String] and q: Seq[Boolean] of equal length, compute a +Seq[String] with those elements of p for which the corresponding element of q is true. +Hint: use zip, map, filter. */ + +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 } +