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

Issue 2304: Missing functions for RWST #3278

Merged
merged 3 commits into from
Jan 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
22 changes: 22 additions & 0 deletions core/src/main/scala/cats/data/IndexedReaderWriterStateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ final class IndexedReaderWriterStateT[F[_], E, L, SA, SB, A](val runF: F[(E, SA)
}
}

/**
* Example:
* {{{
* scala> import cats.implicits._
* scala> val x: IndexedReaderWriterStateT[Option, String, String, Int, Int, Unit] = IndexedReaderWriterStateT.tell("something")
* scala> val y: IndexedReaderWriterStateT[Option, String, String, Int, Int, (Unit, String)] = x.listen
* scala> y.run("environment", 17)
* res0: Option[(String, Int, (Unit, String))] = Some((something,17,((),something)))
* }}}
*/
def listen(implicit F: Functor[F]): IndexedReaderWriterStateT[F, E, L, SA, SB, (A, L)] =
transform { (l, s, a) =>
(l, s, (a, l))
}

/**
* Modify the result of the computation using `f`.
*/
Expand Down Expand Up @@ -437,6 +452,10 @@ abstract private[data] class RWSTFunctions extends CommonIRWSTConstructors {
def modifyF[F[_], E, L, S](f: S => F[S])(implicit F: Applicative[F],
L: Monoid[L]): ReaderWriterStateT[F, E, L, S, Unit] =
ReaderWriterStateT((_, s) => F.map(f(s))((L.empty, _, ())))

def listen[F[_], E, L, S, A](rwst: ReaderWriterStateT[F, E, L, S, A])(
implicit F: Functor[F]
): ReaderWriterStateT[F, E, L, S, (A, L)] = rwst.listen
}

/**
Expand Down Expand Up @@ -491,6 +510,9 @@ abstract private[data] class RWSFunctions {
*/
def tell[E, L, S](l: L): ReaderWriterState[E, L, S, Unit] =
ReaderWriterStateT.tell(l)

def listen[E, L, S, A](rws: ReaderWriterState[E, L, S, A]): ReaderWriterState[E, L, S, (A, L)] =
rws.listen
}

sealed abstract private[data] class IRWSTInstances extends IRWSTInstances1 {
Expand Down
7 changes: 7 additions & 0 deletions tests/src/test/scala/cats/tests/FoldableSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ abstract class FoldableSuite[F[_]: Foldable](name: String)(implicit ArbFInt: Arb
fa.collectFold(pf) should ===(fa.toList.collect(pf).fold(m.empty)(m.combine))

def g(a: String): Option[String] = Some(a).filter(f)

// `collectSomeFold` (deprecated) is used here instead of `collectFoldSome` to
// keep testing the deprecated code until it's finally removed. This helps with
// the coverage and, most importantly, prevents from breaking deprecated code paths
// that might still be in use.
//
// https://github.com/typelevel/cats/pull/3278#discussion_r372841693
fa.collectSomeFold(g) should ===(fa.toList.filter(f).fold(m.empty)(m.combine))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,26 @@ class ReaderWriterStateTSuite extends CatsSuite {
}
}

test("pure + listen + map(_._1) + runEmptyA is identity") {
forAll { i: Int =>
IndexedReaderWriterStateT
.pure[Id, String, String, Int, Int](i)
.listen
.map(_._1)
.runEmptyA("") should ===(i)
}
}

test("tell + listen + map(_._2) + runEmptyA is identity") {
forAll { s: String =>
IndexedReaderWriterStateT
.tell[Id, String, String, Int](s)
.listen
.map(_._2)
.runEmptyA("") should ===(s)
}
}

implicit val iso: Isomorphisms[IndexedReaderWriterStateT[ListWrapper, String, String, Int, String, *]] =
Isomorphisms.invariant[IndexedReaderWriterStateT[ListWrapper, String, String, Int, String, *]](
IndexedReaderWriterStateT.catsDataFunctorForIRWST(ListWrapper.functor)
Expand Down