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
Conversation
Implement sequence in terms of traverse.
Implement and test map, flatMap, orElse and map2.
Either.sequence is implemented in terms of Either.traverse, so testing the former is enough to test the latter.
} | ||
|
||
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] = | ||
map(f) match { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
Using @jonas suggestion: #3 (comment)
@@ -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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice :-)
@yawaramin looks good - my+1 is conditional on @gpoirier 's though, as he has open question(s) |
MPN-922: complete exercises for Functional Programming in Scala, Chapter 4
No description provided.