Last active
January 5, 2017 16:25
-
-
Save wheaties/e59fde66b2b69bc409425be1ecd10094 to your computer and use it in GitHub Desktop.
Implicit existence based Either
This file contains hidden or 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
| //Either based on the existence of a typeclass. | |
| sealed trait Exists[TC[_], N, Y]{ | |
| def apply(no: N, yes: Y): Either[N, Y] | |
| } | |
| object Exists extends LowPriorityExists{ | |
| def apply[TC[_]] = new{ | |
| def apply[N, Y](n: N, y: Y)(implicit exists: Exists[TC, N, Y]) = exists(n, y) | |
| } | |
| implicit def yes[TC[_], N, Y](implicit tc: TC[Y]) = new Exists[TC, N, Y]{ | |
| def apply(no: N, yes: Y): Either[N, Y] = Right(yes) | |
| } | |
| } | |
| trait LowPriorityExists{ | |
| implicit def no[TC[_], N, Y] = new Exists[TC, N, Y]{ | |
| def apply(no: No, yes: Y): Either[N, Y] = Left(no) | |
| } | |
| } |
This file contains hidden or 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
| Exists[Monoid](that :: Nil, that).fold( | |
| other :: _, | |
| implicitly[Monoid[That]].append(_, other) | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment