Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor fixes and solution2.2.6.1_foldLeft #55

Merged
merged 5 commits into from
Mar 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified chapter02/worksheets/Example2.2.5.6.scala
100644 → 100755
Empty file.
12 changes: 10 additions & 2 deletions chapter02/worksheets/solution2.2.6.1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ signature and a sample test are:

def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = ???

scala> fromPairs(List((1,2), (3,4)))
res0: Seq[Int] = List(1, 2, 3, 4)

scala> fromPairs(List((a,b), (c,<nothing>)))
res1: Seq[(String, String)] = List("a", "b", "c", "<nothing>")
res1: Seq[String] = List("a", "b", "c", "<nothing>")

Hint: This can be done with foldLeft or with flatMap.
*/
Expand All @@ -18,11 +21,16 @@ def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = {

val result = fromPairs(List(("a","b"), ("c","<nothing>")))
val expected = List("a", "b", "c", "<nothing>")

assert(result == expected)

val a: Seq[Int] = this.fromPairs(List((1, 2), (3, 4)))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is another this.fromPairs - maybe remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - 'grep' seems to be on strike here today :)

val c: Seq[Int] = List(1, 2, 3, 4)
assert(a == c)

// scala> :load solution2.2.6.1.scala
// :load solution2.2.6.1.scala
// def fromPairs[A](xs: Seq[(A, A)]): Seq[A]
// val result: Seq[String] = List(a, b, c, <nothing>)
// val expected: List[String] = List(a, b, c, <nothing>)
// val a: Seq[Int] = List(1, 2, 3, 4)
// val c: Seq[Int] = List(1, 2, 3, 4)
46 changes: 46 additions & 0 deletions chapter02/worksheets/solution2.2.6.1_foldLeft.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
Exercise 2.2.6.1

Implement a function fromPairs that performs the inverse
transformation to the toPairs function defined in Example 2.2.5.6.

The required type signature and a sample test are:

def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = ???

scala> fromPairs(List((1,2), (3,4)))
res0: Seq[Int] = List(1, 2, 3, 4)

scala> fromPairs(List(("a","b"), ("c",<nothing>)))
res1: Seq[String] = List(a, b, c, <nothing>)

Hint: This can be done with foldLeft or with flatMap.
*/

object FromPairs {
def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = {
// Type alias, for brevity.
type Acc = (Seq[A])
def init: Acc = Seq()
xs.foldLeft(init){ (x, y) => x :+ y._1 :+ y._2 }
}
def main(args: Array[String]) = {
val result: Seq[String] = this.fromPairs(List(("a","b"), ("c","<nothing>")))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this and just write val result = fromPairs ..

val expected: Seq[String] = List("a", "b", "c", "<nothing>")
println("result: %s".format(result))
assert(result == expected)
val a: Seq[Int] = this.fromPairs(List((1, 2), (3, 4)))
val b: Seq[Int] = List(1, 2, 3, 4)
println("a: %s".format(a))
assert(a == b)
}
}

FromPairs.main(Array())

// scala> :load solution2.2.6.1_foldLeft.scala
// :load solution2.2.6.1_foldLeft.scala
// result: List(a, b, c, <nothing>)
// a: List(1, 2, 3, 4)
// // defined object FromPairs

Loading