Skip to content

Instantly share code, notes, and snippets.

@jonathonherbert
Created August 10, 2023 10:56
Show Gist options
  • Save jonathonherbert/83b50541ca655794cbc80c5aead9fd7c to your computer and use it in GitHub Desktop.
Save jonathonherbert/83b50541ca655794cbc80c5aead9fd7c to your computer and use it in GitHub Desktop.
For comprehension desugaring
object ExampleObj {
val getFirstThing: () => Either[Throwable, String] = () => Left(new Exception("dsa"))
val getSecondThing: () => Either[Throwable, String] = () => Right("second thing")
val getThirdThing: () => Either[Throwable, String] = () => Right("third thing")
val result = getFirstThing().flatMap(firstThing => {
getSecondThing().flatMap(secondThing => {
getThirdThing().flatMap(thirdThing => {
Right(thirdThing)
})
})
})
val result = for {
firstThing <- getFirstThing()
secondThing <- getSecondThing()
thirdThing <- getThirdThing()
} yield {
thirdThing
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment