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 StateT transformS #780

Merged
merged 2 commits into from
Jan 6, 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
14 changes: 14 additions & 0 deletions core/src/main/scala/cats/state/StateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,20 @@ final class StateT[F[_], S, A](val runF: F[S => F[(S, A)]]) extends Serializable
def transformF[G[_], B](f: F[(S, A)] => G[(S, B)])(implicit F: FlatMap[F], G: Applicative[G]): StateT[G, S, B] =
StateT(s => f(run(s)))

/** Transform the state used
Copy link
Contributor

Choose a reason for hiding this comment

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

Unjustifiably nitpicky: could you bump "Transform..." down to the next line to be consistent with the other ScalaDoc comments?

Also, what do you think about adding an sbt-doctest example here?

*
* This is useful when you are working with many focused `StateT`s and want to pass in a
* global state containing the various states needed for each individual `StateT`.
*/
Copy link
Contributor

Choose a reason for hiding this comment

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

Love it.

def transformS[R](f: R => S, g: (R, S) => R)(implicit F: Monad[F]): StateT[F, R, A] =
StateT { r =>
F.flatMap(runF) { ff =>
val s = f(r)
val nextState = ff(s)
F.map(nextState) { case (s, a) => (g(r, s), a) }
}
}

/**
* Modify the state (`S`) component.
*/
Expand Down
17 changes: 17 additions & 0 deletions tests/src/test/scala/cats/tests/StateTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,23 @@ class StateTTests extends CatsSuite {
}
}

test("StateT#transformS with identity is identity") {
forAll { (s: StateT[List, Long, Int]) =>
s.transformS[Long](identity, (s, i) => i) should === (s)
}
}

test("StateT#transformS modifies state") {
final case class Env(int: Int, str: String)
val x = StateT((x: Int) => Option((x + 1, x)))
val xx = x.transformS[Env](_.int, (e, i) => e.copy(int = i))
val input = 5

val got = x.run(input)
val expected = xx.run(Env(input, "hello")).map { case (e, i) => (e.int, i) }
got should === (expected)
}

{
implicit val iso = MonoidalTests.Isomorphisms.invariant[StateT[Option, Int, ?]]
checkAll("StateT[Option, Int, Int]", MonadStateTests[StateT[Option, Int, ?], Int].monadState[Int, Int, Int])
Expand Down