Skip to content

Instantly share code, notes, and snippets.

@mattroberts297
Last active August 16, 2017 21:50
Show Gist options
  • Save mattroberts297/05dd9610745917ed078fc416c352ddc4 to your computer and use it in GitHub Desktop.
Save mattroberts297/05dd9610745917ed078fc416c352ddc4 to your computer and use it in GitHub Desktop.
Playing with cats
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