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

MPN-922: complete exercises for Functional Programming in Scala, Chapter 4 #3

Merged
merged 10 commits into from Apr 21, 2015

Conversation

yawaramin
Copy link
Owner

No description provided.

}

def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] =
map(f) match {
Copy link

Choose a reason for hiding this comment

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

This could be simplified by not going through map first.

Choose a reason for hiding this comment

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

It's usually easier to implement map through flatMap than the other way around. In this case:

def map[B](f: A => B): Either[E, B] = flatMap(a => Right(f(a)))

Or flatMap(f andThen Right.apply)

Copy link
Owner Author

Choose a reason for hiding this comment

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

Very nice. Funny thing: this is exactly what Martin Odersky talked about in his lecture on monads in the Reactive Programming course.

@@ -1,24 +1,54 @@
package fpinscala.errorhandling


import scala.{Option => _, Either => _, Left => _, Right => _, _} // hide std library `Option` and `Either`, since we are writing our own in this chapter

Choose a reason for hiding this comment

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

Nice :-)

@ldacosta
Copy link

@yawaramin looks good - my+1 is conditional on @gpoirier 's though, as he has open question(s)

yawaramin added a commit that referenced this pull request Apr 21, 2015
MPN-922: complete exercises for Functional Programming in Scala, Chapter 4
@yawaramin yawaramin merged commit 0acf5aa into master Apr 21, 2015
@yawaramin yawaramin deleted the MPN-922 branch April 21, 2015 18:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants