-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
instances for std Either. closes #117 #125
Changes from 5 commits
4df5e3e
115924b
8232204
47a5d2f
57175e6
66840e0
177c046
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package cats | ||
package std | ||
|
||
trait EitherInstances extends EitherInstances1 { | ||
implicit def eitherInstances[A]: Monad[Either[A, ?]] with Traverse[Either[A, ?]] = | ||
new Monad[Either[A, ?]] with Traverse[Either[A, ?]] { | ||
def pure[B](b: B): Either[A, B] = Right(b) | ||
|
||
def flatMap[B, C](fa: Either[A, B])(f: B => Either[A, C]): Either[A, C] = | ||
fa.right.flatMap(f) | ||
|
||
override def map[B, C](fa: Either[A, B])(f: B => C): Either[A, C] = | ||
fa.right.map(f) | ||
|
||
def traverse[F[_]: Applicative, B, C](fa: Either[A, B])(f: B => F[C]): F[Either[A, C]] = | ||
fa.fold( | ||
a => Applicative[F].pure(Left(a)), | ||
b => Applicative[F].map(f(b))(Right(_)) | ||
) | ||
|
||
def foldLeft[B, C](fa: Either[A, B], c: C)(f: (C, B) => C): C = | ||
fa.fold(_ => c, f(c, _)) | ||
|
||
def foldRight[B, C](fa: Either[A, B], c: C)(f: (B, C) => C): C = | ||
fa.fold(_ => c, f(_, c)) | ||
|
||
def foldRight[B, C](fa: Either[A, B], c: Lazy[C])(f: (B, Lazy[C]) => C): Lazy[C] = | ||
fa.fold(_ => c, b => Lazy(f(b, c))) | ||
} | ||
|
||
implicit def eitherOrder[A: Order, B: Order]: Order[Either[A, B]] = new Order[Either[A, B]] { | ||
def compare(x: Either[A, B], y: Either[A, B]): Int = x.fold( | ||
a => y.fold(Order[A].compare(a, _), _ => -1), | ||
b => y.fold(_ => 1, Order[B].compare(b, _)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry we haven't added it to the contributor guide, but can you please follow the convention agreed upon in #27 and use |
||
) | ||
} | ||
|
||
implicit def eitherShow[A, B](implicit AA: Show[A], BB: Show[B]): Show[Either[A, B]] = | ||
new Show[Either[A, B]] { | ||
def show(f: Either[A, B]): String = f.fold( | ||
a => s"Left(${AA.show(a)})", | ||
b => s"Right(${BB.show(b)})" | ||
) | ||
} | ||
} | ||
|
||
sealed trait EitherInstances1 extends EitherInstances2 { | ||
implicit def eitherPartialOrder[A: PartialOrder, B: PartialOrder]: PartialOrder[Either[A, B]] = | ||
new PartialOrder[Either[A, B]] { | ||
def partialCompare(x: Either[A, B], y: Either[A, B]): Double = x.fold( | ||
a => y.fold(PartialOrder[A].partialCompare(a, _), _ => -1), | ||
b => y.fold(_ => 1, PartialOrder[B].partialCompare(b, _)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as https://github.com/non/cats/pull/125/files#r24407698 but for |
||
) | ||
} | ||
} | ||
|
||
sealed trait EitherInstances2 { | ||
implicit def eitherEq[A: Eq, B: Eq]: Eq[Either[A, B]] = new Eq[Either[A, B]] { | ||
def eqv(x: Either[A, B], y: Either[A, B]): Boolean = x.fold( | ||
a => y.fold(Eq[A].eqv(a, _), _ => false), | ||
b => y.fold(_ => false, Eq[B].eqv(b, _)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comment as https://github.com/non/cats/pull/125/files#r24407698 but for |
||
) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@wedens ah sorry here's one more that I missed - the
Applicative.apply
calls.