Skip to content

Commit

Permalink
solution2.2.6.2_main provided
Browse files Browse the repository at this point in the history
Signed-off-by: Andreas Roehler <[email protected]>
  • Loading branch information
andreas-roehler committed Mar 20, 2024
1 parent 586aa1b commit 52f67f1
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions chapter02/worksheets/solution2.2.6.2_main.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
Exercise 2.2.6.2
Implement the flatten method for sequences by using foldLeft.
The required type signature and a sample test are:
def flatten[A](xxs: Seq[Seq[A]]): Seq[A] = ???
scala> flatten(Seq(Seq(1, 2, 3), Seq(), Seq(4)))
res0: Seq[Int] = List(1, 2, 3, 4)
scala> flatten(Seq(Seq("a","b"), Seq(), (Seq("c","<nothing>"))))
val res1: Seq[String] = List(a, b, c, <nothing>)
*/

object Flatten {
def flatten[A](xxs: Seq[Seq[A]]): Seq[A] = {
var init: Seq[A] = Seq()
xxs.foldLeft(init){ (x, y) => y.foreach( k => (init = init :+ k)); init}
}
def main(args: Array[String]): Unit = {

val result = flatten(Seq(Seq(1, 2, 3), Seq(), Seq(4)))
val expected: Seq[Int] = List(1, 2, 3, 4)
println("result: %s".format(result))
println("expected: %s".format(expected))
assert(result == expected)

val a = flatten(Seq(Seq("a","b"), Seq(), (Seq("c","<nothing>"))))
val b: Seq[String] = List("a", "b", "c", "<nothing>")
assert(a == b)
}
Flatten.main(Array())
}

Flatten.main(Array())

// scala> :load solution2.2.6.2_main.scala
// :load solution2.2.6.2_main.scala
// result: List(1, 2, 3, 4)
// expected: List(1, 2, 3, 4)
// // defined object Flatten

0 comments on commit 52f67f1

Please sign in to comment.