Skip to content
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

Add Try instances #1059

Merged
merged 7 commits into from
Jun 1, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions core/src/main/scala/cats/std/future.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ trait FutureInstances extends FutureInstances1 {

override def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa.map(f)
}

implicit def futureGroup[A: Group](implicit ec: ExecutionContext): Group[Future[A]] =
new FutureGroup[A]
}

private[std] sealed trait FutureInstances1 extends FutureInstances2 {
Expand All @@ -63,10 +60,3 @@ private[cats] class FutureMonoid[A](implicit A: Monoid[A], ec: ExecutionContext)
def empty: Future[A] =
Future.successful(A.empty)
}

private[cats] class FutureGroup[A](implicit A: Group[A], ec: ExecutionContext) extends FutureMonoid[A] with Group[Future[A]] {
def inverse(fx: Future[A]): Future[A] =
fx.map(_.inverse)
override def remove(fx: Future[A], fy: Future[A]): Future[A] =
(fx zip fy).map { case (x, y) => x |-| y }
}
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/std/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ package object std {
object bigInt extends BigIntInstances
object bigDecimal extends BigDecimalInstances

object `try` extends TryInstances
object try_ extends TryInstances
object tuple extends TupleInstances
}
22 changes: 7 additions & 15 deletions core/src/main/scala/cats/std/try.scala
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,23 @@ trait TryInstances extends TryInstances1 {
override def map[A, B](ta: Try[A])(f: A => B): Try[B] = ta.map(f)
}

implicit def tryGroup[A: Group]: Group[Try[A]] =
new TryGroup[A]

implicit def showTry[A](implicit A: Show[A]): Show[Try[A]] =
new Show[Try[A]] {
def show(fa: Try[A]): String = fa match {
case Success(a) => s"Success(${A.show(a)})"
case Failure(e) => s"Failure($e)"
}
}
implicit def eqTry[A](implicit A: Eq[A]): Eq[Try[A]] =
/**
* you may with to do equality by making `implicit val eqT: Eq[Throwable] = Eq.allEqual`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor typo: with -> wish

* doing a fine grained equality on Throwable can make the code very execution
* order dependent
*/
implicit def eqTry[A, T](implicit A: Eq[A], T: Eq[Throwable]): Eq[Try[A]] =
new Eq[Try[A]] {
def eqv(x: Try[A], y: Try[A]): Boolean = (x, y) match {
case (Success(a), Success(b)) => A.eqv(a, b)
case (Failure(_), Failure(_)) => true // all failures are equivalent
case (Failure(a), Failure(b)) => T.eqv(a, b)
case _ => false
}
}
Expand Down Expand Up @@ -102,13 +104,3 @@ private[cats] class TrySemigroup[A: Semigroup] extends Semigroup[Try[A]] {
private[cats] class TryMonoid[A](implicit A: Monoid[A]) extends TrySemigroup[A] with Monoid[Try[A]] {
def empty: Try[A] = Success(A.empty)
}

private[cats] class TryGroup[A](implicit A: Group[A]) extends TryMonoid[A] with Group[Try[A]] {
def inverse(fx: Try[A]): Try[A] =
fx.map(_.inverse)
override def remove(fx: Try[A], fy: Try[A]): Try[A] =
for {
x <- fx
y <- fy
} yield x |-| y
}