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 empty to State and StateT objects #2533

Merged
merged 1 commit into from
Oct 1, 2018
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
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 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there any particular reason to introduce this new trait as opposed to just putting this method directly into the object?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Binary compatibility: #2533 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To be precise, the function is not only provided on the IndexedStateT object, but also on StateT. This is the reason why it has to go into a trait in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh okay that makes sense. Thanks for the explanation.

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](implicit A: Monoid[A]): State[S, A] = pure(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