Last active
August 16, 2017 21:50
-
-
Save mattroberts297/05dd9610745917ed078fc416c352ddc4 to your computer and use it in GitHub Desktop.
Playing with cats
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
package object types { | |
type Or[+A, +B] = Either[A, B] | |
object Or { | |
import cats.syntax.either._ | |
import cats.Traverse | |
def left[A, B](a: A): A Or B = Either.left(a) | |
def right[A, B](b: B): A Or B = Either.right(b) | |
def traverse[F[_] : Traverse, A, B](f: F[A Or B]): A Or F[B] = { | |
Traverse[F] | |
.find(f)(o => o.isLeft) | |
.map(o => Or.left(o.left.get)) | |
.getOrElse(Or.right(Traverse[F].map(f)(o => o.right.get))) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment