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

solution2.2.6.1 provided #53

Merged
merged 5 commits into from
Mar 13, 2024
Merged

Conversation

andreas-roehler
Copy link
Contributor

No description provided.

Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
@andreas-roehler
Copy link
Contributor Author

False error?:

[error] /home/runner/work/sofp-solutions/sofp-solutions/chapter02/src/test/scala/sofp/unit/Solutions.scala:937:46: (A, A) does not take parameters
[error]   xs.map((x: (A, A)) => (b = b :+ (x: (A, A))(0) :+ (x: (A, A))(1)))
[error]  

@winitzki
Copy link
Owner

(x: (A, A))(0)

That's where the problem is. Remove the type annotation and you get (x)(0), which means to apply x to 0. Let me see how to fix that.


def fromPairs[A](xs: Seq[(A, A)]): Seq[A] = {
var b: Seq[A] = List()
xs.map((x: (A, A)) => (b = b :+ (x: (A, A))(0) :+ (x: (A, A))(1)))
Copy link
Owner

@winitzki winitzki Mar 12, 2024

Choose a reason for hiding this comment

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

The function for extracting the parts from the tuple are _1 and _2. The syntax x(0) will not work with tuples until Scala 3.

Also, the point of this exercise is to use flatMap or flatten for implementing fromPairs, instead of using var b and mutating b in a loop.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, thanks. Should provide the alternative solutions too.

Copy link
Owner

Choose a reason for hiding this comment

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

Will you write a solution using flatMap or flatten in a different PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would prefer that, as comments are easier to read then. But do, as it's convenient for you.

Signed-off-by: Andreas Roehler <andreas.roehler@online.de>
@winitzki winitzki merged commit a32fbb5 into winitzki:master Mar 13, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants