Skip to content

Instantly share code, notes, and snippets.

@no-longer-on-githu-b
Last active August 29, 2015 14:05
Show Gist options
  • Save no-longer-on-githu-b/47033a41be64a9f44a19 to your computer and use it in GitHub Desktop.
Save no-longer-on-githu-b/47033a41be64a9f44a19 to your computer and use it in GitHub Desktop.
import invalid._
sealed abstract class UserValidationError
case object FirstNameEmpty extends UserValidationError
case object LastNameEmpty extends UserValidationError
def createUser(saveUser: User => UserID)(firstName: String, lastName: String) =
validate(
firstName.isEmpty -> FirstNameEmpty,
lastName.isEmpty -> LastNameEmpty
) { User(firstName, lastName) } map saveUser
import scalaz.{\/, \/-, -\/}
package object invalid {
def validate[E, R](pairs: (Boolean, E)*)(otherwise: => R): Set[E] \/ R =
if (pairs.exists(_._1)) -\/(pairs.map(_._2).toSet) else \/-(otherwise)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment