Skip to content

Commit

Permalink
Add empty to State and StateT objects
Browse files Browse the repository at this point in the history
Co-authored-by: Lars Hupel <[email protected]>
  • Loading branch information
sderosiaux and larsrh committed Sep 29, 2018
1 parent e093bbd commit 34f5a26
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
12 changes: 11 additions & 1 deletion core/src/main/scala/cats/data/IndexedStateT.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private[data] trait CommonStateTConstructors {
IndexedStateT(s => F.pure((s, s)))
}

object IndexedStateT extends IndexedStateTInstances with CommonStateTConstructors {
object IndexedStateT extends IndexedStateTInstances with CommonStateTConstructors0 {
def apply[F[_], SA, SB, A](f: SA => F[(SB, A)])(implicit F: Applicative[F]): IndexedStateT[F, SA, SB, A] =
new IndexedStateT(F.pure(f))

Expand All @@ -223,6 +223,11 @@ object IndexedStateT extends IndexedStateTInstances with CommonStateTConstructor
IndexedStateT(_ => F.map(fsb)(s => (s, ())))
}

private[data] trait CommonStateTConstructors0 extends CommonStateTConstructors {
def empty[F[_], S, A](implicit A: Monoid[A], F: Applicative[F]): IndexedStateT[F, S, S, A] =
pure(A.empty)
}

private[data] abstract class StateTFunctions extends CommonStateTConstructors {
def apply[F[_], S, A](f: S => F[(S, A)])(implicit F: Applicative[F]): StateT[F, S, A] =
IndexedStateT(f)
Expand Down Expand Up @@ -299,6 +304,11 @@ private[data] abstract class StateFunctions {
*/
def pure[S, A](a: A): State[S, A] = State(s => (s, a))

/**
* Return `A`'s empty monoid value and maintain the input state.
*/
def empty[S, A: Monoid]: State[S, A] = pure(Monoid[A].empty)

/**
* Modify the input state and return Unit.
*/
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/cats/data/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ package object data {
* context along with the `A` value.
*/
type StateT[F[_], S, A] = IndexedStateT[F, S, S, A]
object StateT extends StateTFunctions
object StateT extends StateTFunctions with CommonStateTConstructors0

type State[S, A] = StateT[Eval, S, A]
object State extends StateFunctions
Expand Down
11 changes: 11 additions & 0 deletions tests/src/test/scala/cats/tests/IndexedStateTSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ class IndexedStateTSuite extends CatsSuite {
}
}

test("State.empty, StateT.empty and IndexedStateT.empty are consistent"){
forAll { (s: String) =>
val state: State[String, Int] = State.empty
val stateT: State[String, Int] = StateT.empty
val indexedStateT: State[String, Int] = IndexedStateT.empty

state.run(s) should === (stateT.run(s))
state.run(s) should === (indexedStateT.run(s))
}
}

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

0 comments on commit 34f5a26

Please sign in to comment.