Skip to content

Commit

Permalink
Merge pull request #1392 from zainab-ali/state
Browse files Browse the repository at this point in the history
Adding get method to StateT
  • Loading branch information
non authored Sep 22, 2016
2 parents c3fc69d + 9ce7dc0 commit 7b15008
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
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

0 comments on commit 7b15008

Please sign in to comment.