Created
August 10, 2023 10:56
-
-
Save jonathonherbert/83b50541ca655794cbc80c5aead9fd7c to your computer and use it in GitHub Desktop.
For comprehension desugaring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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