Last active
December 14, 2015 02:19
-
-
Save ktoso/5012875 to your computer and use it in GitHub Desktop.
Example on why one should bother to learn monads/scalaz => it really cleans up real life code :-)
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
import scalaz._ | |
val failOrBusiness: Validation[Error, Business] = for { | |
business <- doBusiness() | |
_ <- validateBusiness(business) // we can inspect the business here, it's like "case Right(business) =>" | |
} yield business | |
// instead of Either, use monadic Validation | |
val iFeelLikeIt: Boolean = ??? | |
def doBusiness(): Validation[Error, Business] = | |
if (iFeelLikeIt) | |
Business().success | |
else | |
Error("No business :<").fail | |
def validateBusiness: Validation[Error, Unit] = ??? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment