Skip to content

Commit

Permalink
#14, solution2.1.7.3_SW.scala added
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler committed Nov 26, 2023
1 parent fde3570 commit ab94d2e
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions chapter02/worksheets/solution2.1.7.3_SW.scala
Original file line number Diff line number Diff line change
@@ -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 }

0 comments on commit ab94d2e

Please sign in to comment.