-
-
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
XorTInstances + cleanup tests #1106
Changes from 4 commits
7e764db
d2a0912
cdefe00
9516f8f
1e1b323
41ad7af
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 |
---|---|---|
|
@@ -241,14 +241,17 @@ private[data] abstract class XorTInstances extends XorTInstances1 { | |
val F0: Traverse[F] = F | ||
} | ||
|
||
implicit def xortTransLift[E]: TransLift.Aux[XorT[?[_], E, ?], Functor] = | ||
implicit def catsDataTransLiftForXorT[E]: TransLift.Aux[XorT[?[_], E, ?], Functor] = | ||
new TransLift[XorT[?[_], E, ?]] { | ||
type TC[M[_]] = Functor[M] | ||
|
||
def liftT[M[_]: Functor, A](ma: M[A]): XorT[M,E,A] = | ||
XorT(Functor[M].map(ma)(Xor.right)) | ||
} | ||
|
||
implicit def catsMonoidForXorT[F[_], L, A](implicit F: Monoid[F[L Xor A]]): Monoid[XorT[F, L, A]] = | ||
new XorTMonoid[F, L, A] { implicit val F0 = F } | ||
|
||
} | ||
|
||
private[data] abstract class XorTInstances1 extends XorTInstances2 { | ||
|
@@ -258,7 +261,10 @@ private[data] abstract class XorTInstances1 extends XorTInstances2 { | |
implicit val L0 = L | ||
new XorTMonadFilter[F, L] { implicit val F = F0; implicit val L = L0 } | ||
} | ||
*/ | ||
*/ | ||
|
||
implicit def catsSemigroupForXorT[F[_], L, A](implicit F: Semigroup[F[L Xor A]]): Semigroup[XorT[F, L, A]] = | ||
new XorTSemigroup[F, L, A] { implicit val F0 = F } | ||
|
||
implicit def catsDataFoldableForXorT[F[_], L](implicit F: Foldable[F]): Foldable[XorT[F, L, ?]] = | ||
new XorTFoldable[F, L] { | ||
|
@@ -282,19 +288,11 @@ private[data] abstract class XorTInstances2 extends XorTInstances3 { | |
} | ||
|
||
private[data] abstract class XorTInstances3 extends XorTInstances4 { | ||
implicit def catsDataMonadErrorForXorT[F[_], L](implicit F: Monad[F]): MonadError[XorT[F, L, ?], L] = { | ||
implicit val F0 = F | ||
implicit def catsDataMonadErrorForXorT[F[_], L](implicit F0: Monad[F]): MonadError[XorT[F, L, ?], L] = | ||
new XorTMonadError[F, L] { implicit val F = F0 } | ||
} | ||
|
||
implicit def catsDataSemigroupKForXorT[F[_], L](implicit F: Monad[F]): SemigroupK[XorT[F, L, ?]] = | ||
new SemigroupK[XorT[F,L,?]] { | ||
def combineK[A](x: XorT[F,L,A], y: XorT[F, L, A]): XorT[F, L, A] = | ||
XorT(F.flatMap(x.value) { | ||
case l @ Xor.Left(_) => y.value | ||
case r @ Xor.Right(_) => F.pure(r) | ||
}) | ||
} | ||
implicit def catsDataSemigroupKForXorT[F[_], L](implicit F0: Monad[F]): SemigroupK[XorT[F, L, ?]] = | ||
new XorTSemigroupK[F, L] { implicit val F = F0 } | ||
|
||
implicit def catsDataEqForXorT[F[_], L, R](implicit F: Eq[F[L Xor R]]): Eq[XorT[F, L, R]] = | ||
new XorTEq[F, L, R] { | ||
|
@@ -303,10 +301,28 @@ private[data] abstract class XorTInstances3 extends XorTInstances4 { | |
} | ||
|
||
private[data] abstract class XorTInstances4 { | ||
implicit def catsDataFunctorForXorT[F[_], L](implicit F: Functor[F]): Functor[XorT[F, L, ?]] = { | ||
implicit val F0 = F | ||
implicit def catsDataFunctorForXorT[F[_], L](implicit F0: Functor[F]): Functor[XorT[F, L, ?]] = | ||
new XorTFunctor[F, L] { implicit val F = F0 } | ||
} | ||
} | ||
|
||
private[data] trait XorTSemigroup[F[_], L, A] extends Semigroup[XorT[F, L, A]] { | ||
implicit val F0: Semigroup[F[L Xor A]] | ||
def combine(x: XorT[F, L ,A], y: XorT[F, L , A]): XorT[F, L , A] = | ||
XorT(F0.combine(x.value, y.value)) | ||
} | ||
|
||
private[data] trait XorTMonoid[F[_], L, A] extends Monoid[XorT[F, L, A]] with XorTSemigroup[F, L, A] { | ||
implicit val F0: Monoid[F[L Xor A]] | ||
def empty: XorT[F, L, A] = XorT(F0.empty) | ||
} | ||
|
||
private[data] trait XorTSemigroupK[F[_], L] extends SemigroupK[XorT[F, L, ?]] { | ||
implicit val F: Monad[F] | ||
def combineK[A](x: XorT[F,L,A], y: XorT[F, L, A]): XorT[F, L, A] = | ||
XorT(F.flatMap(x.value) { | ||
case l @ Xor.Left(_) => y.value | ||
case r @ Xor.Right(_) => F.pure(r) | ||
}) | ||
} | ||
|
||
private[data] trait XorTFunctor[F[_], L] extends Functor[XorT[F, L, ?]] { | ||
|
@@ -316,8 +332,9 @@ private[data] trait XorTFunctor[F[_], L] extends Functor[XorT[F, L, ?]] { | |
|
||
private[data] trait XorTMonad[F[_], L] extends Monad[XorT[F, L, ?]] with XorTFunctor[F, L] { | ||
implicit val F: Monad[F] | ||
def pure[A](a: A): XorT[F, L, A] = XorT.pure[F, L, A](a) | ||
def pure[A](a: A): XorT[F, L, A] = XorT(F.pure(Xor.right(a))) | ||
def flatMap[A, B](fa: XorT[F, L, A])(f: A => XorT[F, L, B]): XorT[F, L, B] = fa flatMap f | ||
override def ap[A, B](x: XorT[F, L, A => B])(y: XorT[F, L, A]): XorT[F, L, B] = super.ap(x)(y) | ||
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. Is there a reason to override this if we are just calling through to 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. Good spot - this is a left over from the conflicting |
||
} | ||
|
||
private[data] trait XorTMonadError[F[_], L] extends MonadError[XorT[F, L, ?], L] with XorTMonad[F, L] { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,14 +44,21 @@ object ListWrapper { | |
|
||
def eqv[A : Eq]: Eq[ListWrapper[A]] = Eq[List[A]].on[ListWrapper[A]](_.list) | ||
|
||
val foldable: Foldable[ListWrapper] = | ||
new Foldable[ListWrapper] { | ||
def foldLeft[A, B](fa: ListWrapper[A], b: B)(f: (B, A) => B): B = | ||
Foldable[List].foldLeft(fa.list, b)(f) | ||
val traverse: Traverse[ListWrapper] = { | ||
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. Unfortunately I think there are going to be merge conflicts between this and #1103. I guess we'll have to get that sorted out after one of them is merged. 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. #1103 is merged - should I resync this or would it tell me if there were conflicts? 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. Hmm interesting. It looks like maybe there's no merge conflict because the code is exactly the same? 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. Like I mentioned somewhere, I didn't reinvent the wheel and reused the |
||
val F = Traverse[List] | ||
|
||
new Traverse[ListWrapper] { | ||
def foldLeft[A, B](fa: ListWrapper[A], b: B)(f: (B, A) => B): B = | ||
F.foldLeft(fa.list, b)(f) | ||
def foldRight[A, B](fa: ListWrapper[A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = | ||
Foldable[List].foldRight(fa.list, lb)(f) | ||
F.foldRight(fa.list, lb)(f) | ||
def traverse[G[_], A, B](fa: ListWrapper[A])(f: A => G[B])(implicit G0: Applicative[G]): G[ListWrapper[B]] = { | ||
G0.map(F.traverse(fa.list)(f))(ListWrapper.apply) | ||
} | ||
} | ||
} | ||
|
||
val foldable: Foldable[ListWrapper] = traverse | ||
|
||
val functor: Functor[ListWrapper] = | ||
new Functor[ListWrapper] { | ||
|
@@ -85,6 +92,19 @@ object ListWrapper { | |
} | ||
} | ||
|
||
val monadRec: MonadRec[ListWrapper] = { | ||
val M = MonadRec[List] | ||
|
||
new MonadRec[ListWrapper] { | ||
def pure[A](x: A): ListWrapper[A] = ListWrapper(M.pure(x)) | ||
def flatMap[A, B](fa: ListWrapper[A])(f: A => ListWrapper[B]): ListWrapper[B] = ListWrapper(M.flatMap(fa.list)(a => f(a).list)) | ||
def tailRecM[A, B](a: A)(f: A => ListWrapper[cats.data.Xor[A,B]]): ListWrapper[B] = | ||
ListWrapper(M.tailRecM(a)(a => f(a).list)) | ||
} | ||
} | ||
|
||
val flatMapRec: FlatMapRec[ListWrapper] = monadRec | ||
|
||
val monad: Monad[ListWrapper] = monadCombine | ||
|
||
val applicative: Applicative[ListWrapper] = monadCombine | ||
|
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.
minor: I think this could simply be
that.flatMap(this.map)