-
-
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
Adding CoflatMap and tests to WriterT and fixed laws for CoflatMap #1049
Changes from 2 commits
3efe27f
6ab573c
128c494
82443c9
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 |
---|---|---|
|
@@ -87,8 +87,7 @@ private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1 | |
implicit val L0: Monoid[L] = L | ||
} | ||
|
||
implicit def writerTIdFunctor[L]: Functor[WriterT[Id, L, ?]] = | ||
writerTFunctor[Id, L] | ||
def writerTIdFunctor[L]: Functor[WriterT[Id, L, ?]] = writerTIdCoflatMap | ||
|
||
implicit def writerTIdFlatMap[L:Semigroup]: FlatMap[WriterT[Id, L, ?]] = | ||
writerTFlatMap[Id, L] | ||
|
@@ -111,7 +110,11 @@ private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2 | |
new WriterTMonoid[F, L, V] { | ||
implicit val F0: Monoid[F[(L, V)]] = W | ||
} | ||
|
||
implicit def writerTIdCoflatMap[L]: CoflatMap[WriterT[Id, L, ?]] = | ||
writerTCoflatMap[Id, L] | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances2 extends WriterTInstances3 { | ||
implicit def writerTMonadWriter[F[_], L](implicit F: Monad[F], L: Monoid[L]): MonadWriter[WriterT[F, L, ?], L] = | ||
new WriterTMonadWriter[F, L] { | ||
|
@@ -131,6 +134,7 @@ private[data] sealed abstract class WriterTInstances3 extends WriterTInstances4 | |
implicit val F0: Alternative[F] = F | ||
implicit val L0: Monoid[L] = L | ||
} | ||
|
||
} | ||
|
||
private[data] sealed abstract class WriterTInstances4 extends WriterTInstances5 { | ||
|
@@ -168,7 +172,10 @@ private[data] sealed abstract class WriterTInstances6 extends WriterTInstances7 | |
} | ||
|
||
private[data] sealed abstract class WriterTInstances7 { | ||
implicit def writerTFunctor[F[_], L](implicit F: Functor[F]): Functor[WriterT[F, L, ?]] = new WriterTFunctor[F, L] { | ||
|
||
def writerTFunctor[F[_], L](implicit F: Functor[F]): Functor[WriterT[F, L, ?]] = writerTCoflatMap | ||
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. I don't think this is needed for anything. |
||
|
||
implicit def writerTCoflatMap[F[_], L](implicit F: Functor[F]): WriterTCoflatMap[F, L] = new WriterTCoflatMap[F, L] { | ||
implicit val F0: Functor[F] = F | ||
} | ||
} | ||
|
@@ -259,12 +266,18 @@ private[data] sealed trait WriterTSemigroup[F[_], L, A] extends Semigroup[Writer | |
WriterT(F0.combine(x.run, y.run)) | ||
} | ||
|
||
private[data] sealed trait WriterTMonoid[F[_], L, A] extends Monoid[WriterT[F, L, A]] with WriterTSemigroup[F, L, A]{ | ||
private[data] sealed trait WriterTMonoid[F[_], L, A] extends Monoid[WriterT[F, L, A]] with WriterTSemigroup[F, L, A] { | ||
override implicit def F0: Monoid[F[(L, A)]] | ||
|
||
def empty: WriterT[F, L, A] = WriterT(F0.empty) | ||
} | ||
|
||
private[data] sealed trait WriterTCoflatMap[F[_], L] extends CoflatMap[WriterT[F, L, ?]] with WriterTFunctor[F, L] { | ||
|
||
def coflatMap[A, B](fa: WriterT[F, L, A])(f: WriterT[F, L, A] => B): WriterT[F, L, B] = fa.map(_ => f(fa)) | ||
} | ||
|
||
|
||
trait WriterTFunctions { | ||
def putT[F[_], L, V](vf: F[V])(l: L)(implicit functorF: Functor[F]): WriterT[F, L, V] = | ||
WriterT(functorF.map(vf)(v => (l, v))) | ||
|
@@ -281,6 +294,3 @@ trait WriterTFunctions { | |
def valueT[F[_], L, V](vf: F[V])(implicit functorF: Functor[F], monoidL: Monoid[L]): WriterT[F, L, V] = | ||
WriterT.putT[F, L, V](vf)(monoidL.empty) | ||
} | ||
|
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -268,4 +268,16 @@ class WriterTTests extends CatsSuite { | |
|
||
Semigroup[WriterT[Id, Int, Int]] | ||
} | ||
|
||
{ | ||
// F has a Functor | ||
implicit val F: Functor[ListWrapper] = ListWrapper.functor | ||
|
||
CoflatMap[WriterT[ListWrapper, Int, ?]] | ||
checkAll("WriterT[Listwrapper, Int, ?]", CoflatMapTests[WriterT[ListWrapper, Int, ?]].coflatMap[Int, Int, Int]) | ||
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. need to check serializability |
||
checkAll("WriterT[ListWrapper, Int, ?]", SerializableTests.serializable(CoflatMap[WriterT[ListWrapper, Int, ?]])) | ||
|
||
// Id has a Functor | ||
CoflatMap[WriterT[Id, Int, ?]] | ||
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. Could you also summon the |
||
} | ||
} |
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.
We should be able to delete this now that
writerTIdCoflatMap
exists. You might want to move it up to where this was.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.
If I move it up, the implicit conflicts with
writerTIdFlatMap
when there is aSemigroup
forL
, as they are bothFunctors
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.
Ah I see. Glad we have tests for that! :). Do things work if you just remove this
writerTIdFunctor
?