Skip to content

Commit

Permalink
Merge pull request #875 from ceedubs/state-get
Browse files Browse the repository at this point in the history
Add .get method to StateT
  • Loading branch information
mpilquist committed Feb 17, 2016
2 parents 769efe7 + c081bdb commit 82b6650
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
5 changes: 5 additions & 0 deletions core/src/main/scala/cats/data/StateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ final class StateT[F[_], S, A](val runF: F[S => F[(S, A)]]) extends Serializable
def inspect[B](f: S => B)(implicit F: Monad[F]): StateT[F, S, B] =
transform((s, _) => (s, f(s)))

/**
* Get the input state, without modifying the state.
*/
def get(implicit F: Monad[F]): StateT[F, S, S] =
inspect(identity)
}

object StateT extends StateTInstances {
Expand Down
13 changes: 13 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,19 @@ class StateTTests extends CatsSuite {
}
}

test(".get and then .run produces same state as value"){
forAll { (s: State[Long, Int], initial: Long) =>
val (finalS, finalA) = s.get.run(initial).value
finalS should === (finalA)
}
}

test(".get equivalent to flatMap with State.get"){
forAll { (s: State[Long, Int]) =>
s.get should === (s.flatMap(_ => State.get))
}
}

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

0 comments on commit 82b6650

Please sign in to comment.