Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Adding get method to StateT #1392

Merged
merged 1 commit into from
Sep 22, 2016
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
3 changes: 3 additions & 0 deletions core/src/main/scala/cats/data/StateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ object StateT extends StateTInstances {
def modifyF[F[_], S](f: S => F[S])(implicit F: Applicative[F]): StateT[F, S, Unit] =
StateT(s => F.map(f(s))(s => (s, ())))

def get[F[_], S](implicit F: Applicative[F]): StateT[F, S, S] =
StateT(s => F.pure((s, s)))

def set[F[_], S](s: S)(implicit F: Applicative[F]): StateT[F, S, Unit] =
StateT(_ => F.pure((s, ())))

Expand Down
8 changes: 8 additions & 0 deletions tests/src/test/scala/cats/tests/StateTTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ class StateTTests extends CatsSuite {
}
}

test("State.get and StateT.get are consistent") {
forAll{ (s: String) =>
val state: State[String, String] = State.get
val stateT: State[String, String] = StateT.get
state.run(s) should === (stateT.run(s))
}
}

test("State.inspect and StateT.inspect are consistent") {
forAll { (s: String, f: String => Int) =>
val state: State[String, Int] = State.inspect(f)
Expand Down