-
-
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
Add Foldable
, Traverse
and Comonad
instances to WriterT
#2182
Merged
Merged
Changes from 7 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
0f3b16c
Added foldLeft and foldRight to WriterT and Foldable instance
barambani 4d1d7f7
Added tests for Foldable instance
barambani 241f213
Added traverse. Fixed priority of the implicit search for isntances
barambani e68849d
Added traverse implementation to WriterT
barambani 6729608
Added test for WriterT Traverse
barambani 2a7f5f2
Refactored names
barambani 454cc8f
Changed priority of Traverse instance
barambani 4cf8304
Added comonad instance for WrtiterT and Const. Added tests
barambani 0c2c002
Completed Comonad tests using tuple instead of Const
barambani e63a7e3
Refactored extract derivation to avoid implicit search
barambani fba0f7a
Added instances and reolution tests for Id. Removed unneeded type
barambani 2e4b2b5
Minor refactoring
barambani fd077c3
Attempt to increase coverage
barambani a40e9f7
Removed unneeded serialization test
barambani b3ab664
Refactored traverse derivation
barambani 152ee7a
Fixed improper inheritance of WriterTFunctor
barambani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
package cats | ||
package data | ||
|
||
import cats.Foldable | ||
import cats.kernel.instances.tuple._ | ||
|
||
import cats.kernel.CommutativeMonoid | ||
import cats.syntax.semigroup._ | ||
|
||
|
@@ -63,6 +63,17 @@ final case class WriterT[F[_], L, V](run: F[(L, V)]) { | |
mapWritten(_ => monoidL.empty) | ||
|
||
def show(implicit F: Show[F[(L, V)]]): String = F.show(run) | ||
|
||
def foldLeft[C](c: C)(f: (C, V) => C)(implicit F: Foldable[F]): C = | ||
F.foldLeft(run, c)((z, lv) => f(z, lv._2)) | ||
|
||
def foldRight[C](lc: Eval[C])(f: (V, Eval[C]) => Eval[C])(implicit F: Foldable[F]): Eval[C] = | ||
F.foldRight(run, lc)((lv, z) => f(lv._2, z)) | ||
|
||
def traverse[G[_], V1](f: V => G[V1])(implicit F: Traverse[F], G: Applicative[G]): G[WriterT[F, L, V1]] = | ||
G.map( | ||
F.traverse(run)(lv => G.product(G.pure(lv._1), f(lv._2))) | ||
)(WriterT.apply) | ||
} | ||
|
||
object WriterT extends WriterTInstances with WriterTFunctions { | ||
|
@@ -98,6 +109,13 @@ private[data] sealed abstract class WriterTInstances extends WriterTInstances0 { | |
} | ||
|
||
private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1 { | ||
implicit def catsDataTraverseForWriterT[F[_], L, V](implicit F: Traverse[F]): Traverse[WriterT[F, L, ?]] = | ||
new WriterTTraverse[F, L] { | ||
val F0: Traverse[F] = F | ||
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2 { | ||
implicit def catsDataMonadErrorForWriterT[F[_], L, E](implicit F: MonadError[F, E], L: Monoid[L]): MonadError[WriterT[F, L, ?], E] = | ||
new WriterTMonadError[F, L, E] { | ||
implicit val F0: MonadError[F, E] = F | ||
|
@@ -134,9 +152,14 @@ private[data] sealed abstract class WriterTInstances0 extends WriterTInstances1 | |
|
||
implicit def catsDataMonoidForWriterTId[L:Monoid, V:Monoid]: Monoid[WriterT[Id, L, V]] = | ||
catsDataMonoidForWriterT[Id, L, V] | ||
|
||
implicit def catsDataFoldableForWriterT[F[_], L, V](implicit F: Foldable[F]): Foldable[WriterT[F, L, ?]] = | ||
new WriterTFoldable[F, L]{ | ||
val F0: Foldable[F] = F | ||
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2 { | ||
private[data] sealed abstract class WriterTInstances2 extends WriterTInstances3 { | ||
implicit def catsDataMonadForWriterTId[L:Monoid]: Monad[WriterT[Id, L, ?]] = | ||
catsDataMonadForWriterT[Id, L] | ||
|
||
|
@@ -147,7 +170,7 @@ private[data] sealed abstract class WriterTInstances1 extends WriterTInstances2 | |
catsDataSemigroupForWriterT[Id, L, V] | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances2 extends WriterTInstances3 { | ||
private[data] sealed abstract class WriterTInstances3 extends WriterTInstances4 { | ||
implicit def catsDataMonadForWriterT[F[_], L](implicit F: Monad[F], L: Monoid[L]): Monad[WriterT[F, L, ?]] = | ||
new WriterTMonad[F, L] { | ||
implicit val F0: Monad[F] = F | ||
|
@@ -163,12 +186,12 @@ private[data] sealed abstract class WriterTInstances2 extends WriterTInstances3 | |
catsDataCoflatMapForWriterT[Id, L] | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances3 extends WriterTInstances4 { | ||
private[data] sealed abstract class WriterTInstances4 extends WriterTInstances5 { | ||
implicit def catsDataFlatMapForWriterTId[L:Semigroup]: FlatMap[WriterT[Id, L, ?]] = | ||
catsDataFlatMapForWriterT2[Id, L] | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances4 extends WriterTInstances5 { | ||
private[data] sealed abstract class WriterTInstances5 extends WriterTInstances6 { | ||
implicit def catsDataFlatMapForWriterT1[F[_], L](implicit F: FlatMap[F], L: Monoid[L]): FlatMap[WriterT[F, L, ?]] = | ||
new WriterTFlatMap1[F, L] { | ||
implicit val F0: FlatMap[F] = F | ||
|
@@ -181,15 +204,15 @@ private[data] sealed abstract class WriterTInstances4 extends WriterTInstances5 | |
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances5 extends WriterTInstances6 { | ||
private[data] sealed abstract class WriterTInstances6 extends WriterTInstances7 { | ||
implicit def catsDataApplicativeErrorForWriterT[F[_], L, E](implicit F: ApplicativeError[F, E], L: Monoid[L]): ApplicativeError[WriterT[F, L, ?], E] = | ||
new WriterTApplicativeError[F, L, E] { | ||
implicit val F0: ApplicativeError[F, E] = F | ||
implicit val L0: Monoid[L] = L | ||
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances6 extends WriterTInstances7 { | ||
private[data] sealed abstract class WriterTInstances7 extends WriterTInstances8 { | ||
implicit def catsDataAlternativeForWriterT[F[_], L](implicit F: Alternative[F], L: Monoid[L]): Alternative[WriterT[F, L, ?]] = | ||
new WriterTAlternative[F, L] { | ||
implicit val F0: Alternative[F] = F | ||
|
@@ -202,7 +225,7 @@ private[data] sealed abstract class WriterTInstances6 extends WriterTInstances7 | |
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances7 extends WriterTInstances8 { | ||
private[data] sealed abstract class WriterTInstances8 extends WriterTInstances9 { | ||
implicit def catsDataMonoidKForWriterT[F[_], L](implicit F: MonoidK[F]): MonoidK[WriterT[F, L, ?]] = | ||
new WriterTMonoidK[F, L] { | ||
implicit val F0: MonoidK[F] = F | ||
|
@@ -219,7 +242,7 @@ private[data] sealed abstract class WriterTInstances7 extends WriterTInstances8 | |
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances8 extends WriterTInstances9 { | ||
private[data] sealed abstract class WriterTInstances9 extends WriterTInstances10 { | ||
implicit def catsDataSemigroupKForWriterT[F[_], L](implicit F: SemigroupK[F]): SemigroupK[WriterT[F, L, ?]] = | ||
new WriterTSemigroupK[F, L] { | ||
implicit val F0: SemigroupK[F] = F | ||
|
@@ -233,15 +256,15 @@ private[data] sealed abstract class WriterTInstances8 extends WriterTInstances9 | |
|
||
} | ||
|
||
private[data] sealed abstract class WriterTInstances9 extends WriterTInstances10 { | ||
private[data] sealed abstract class WriterTInstances10 extends WriterTInstances11 { | ||
implicit def catsDataApplyForWriterT[F[_], L](implicit F: Apply[F], L: Semigroup[L]): Apply[WriterT[F, L, ?]] = | ||
new WriterTApply[F, L] { | ||
implicit val F0: Apply[F] = F | ||
implicit val L0: Semigroup[L] = L | ||
} | ||
} | ||
|
||
private[data] sealed abstract class WriterTInstances10 { | ||
private[data] sealed abstract class WriterTInstances11 { | ||
implicit def catsDataCoflatMapForWriterT[F[_], L](implicit F: Functor[F]): CoflatMap[WriterT[F, L, ?]] = | ||
new WriterTCoflatMap[F, L] { | ||
implicit val F0: Functor[F] = F | ||
|
@@ -410,6 +433,20 @@ private[data] sealed trait WriterTCoflatMap[F[_], L] extends CoflatMap[WriterT[F | |
def coflatMap[A, B](fa: WriterT[F, L, A])(f: WriterT[F, L, A] => B): WriterT[F, L, B] = fa.map(_ => f(fa)) | ||
} | ||
|
||
private[data] sealed trait WriterTFoldable[F[_], L] extends Foldable[WriterT[F, L, ?]] { | ||
|
||
implicit def F0: Foldable[F] | ||
|
||
def foldLeft[A, B](fa: WriterT[F, L, A], b: B)(f: (B, A) => B): B = fa.foldLeft(b)(f) | ||
def foldRight[A, B](fa: WriterT[F, L, A], lb: Eval[B])(f: (A, Eval[B]) => Eval[B]): Eval[B] = fa.foldRight(lb)(f) | ||
} | ||
|
||
private[data] sealed trait WriterTTraverse[F[_], L] extends Traverse[WriterT[F, L, ?]] with WriterTFoldable[F, L] { | ||
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. If you extends 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 will change it thanks 👍 |
||
|
||
implicit def F0: Traverse[F] | ||
|
||
def traverse[G[_]: Applicative, A, B](fa: WriterT[F, L, A])(f: A => G[B]): G[WriterT[F, L, B]] = fa.traverse(f) | ||
} | ||
|
||
private[data] trait WriterTFunctions { | ||
def putT[F[_], L, V](vf: F[V])(l: L)(implicit functorF: Functor[F]): WriterT[F, L, V] = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can this be
G.tupleLeft(f(lv._2), lv._1)
instead ofG.product(G.pure(lv._1), f(lv._2))
?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.
sure changing it 👍