Skip to content

Commit

Permalink
Merge pull request #4 from winitzki/feature/exercise_2.1.7.3
Browse files Browse the repository at this point in the history
add solutions
  • Loading branch information
winitzki authored Nov 23, 2023
2 parents 58cdce0 + cb36cfe commit 4ea8069
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 2 deletions.
2 changes: 1 addition & 1 deletion chapter01/src/test/scala/sofp/unit/Exercises_1_6_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
50 changes: 49 additions & 1 deletion chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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())
}
}

0 comments on commit 4ea8069

Please sign in to comment.