Skip to content

Commit

Permalink
Merge pull request #58 from andreas-roehler/master
Browse files Browse the repository at this point in the history
solution2.1.7.3_AR simplified
  • Loading branch information
winitzki authored Mar 18, 2024
2 parents 5e1ae78 + 8cc4f51 commit fc24ac1
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions chapter02/worksheets/solution2.1.7.3_AR.scala
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/** author: Andreas Röhler */

/** 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 result = Seq("Joe", "Bob", "Mary").zip(Seq(true, false, true)).map { case (x, y) => if (y == true) x else () }.filter(_ != (())).map{ case x => x.toString }
assert(result == List("Joe", "Mary"))
val result = Seq("Joe", "Bob", "Mary").zip(Seq(true, false, true))
// .map { case (x, y) => if (y == true) x else () }.filter(_ != (())).map{ case x => x.toString }
.filter { case (x, y) => y }.map { case ( x, y) => x }
assert(result == List("Joe", "Mary"))

// scala> :load solution2.1.7.3_AR.scala
// Loading solution2.1.7.3_AR.scala...
Expand Down

0 comments on commit fc24ac1

Please sign in to comment.