From 7d6551f9eb666adf7ede51c8d422d2583cc162f4 Mon Sep 17 00:00:00 2001 From: zainab-ali Date: Wed, 29 Jun 2016 19:54:57 +0100 Subject: [PATCH 1/3] Adding lift and modify to StateT --- core/src/main/scala/cats/data/StateT.scala | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/src/main/scala/cats/data/StateT.scala b/core/src/main/scala/cats/data/StateT.scala index 864c869905..118fe5da16 100644 --- a/core/src/main/scala/cats/data/StateT.scala +++ b/core/src/main/scala/cats/data/StateT.scala @@ -133,6 +133,12 @@ object StateT extends StateTInstances { def pure[F[_], S, A](a: A)(implicit F: Applicative[F]): StateT[F, S, A] = StateT(s => F.pure((s, a))) + + def lift[F[_], S, A](fa: F[A])(implicit F: Applicative[F]): StateT[F, S, A] = + StateT(s => F.map(fa)(a => (s, a))) + + def modify[F[_], S, A](f: S => F[A])(implicit F: Applicative[F]): StateT[F, S, A] = + StateT(s => F.map(f(s))(a => (s, a))) } private[data] sealed trait StateTInstances extends StateTInstances1 { From d7c1bb5671611d79a2c247fa07816818bcd336d8 Mon Sep 17 00:00:00 2001 From: zainab-ali Date: Wed, 29 Jun 2016 21:45:21 +0100 Subject: [PATCH 2/3] Adding inspect instead of modify to StateT --- core/src/main/scala/cats/data/StateT.scala | 2 +- .../src/test/scala/cats/tests/StateTTests.scala | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/core/src/main/scala/cats/data/StateT.scala b/core/src/main/scala/cats/data/StateT.scala index 118fe5da16..a55960661a 100644 --- a/core/src/main/scala/cats/data/StateT.scala +++ b/core/src/main/scala/cats/data/StateT.scala @@ -137,7 +137,7 @@ object StateT extends StateTInstances { def lift[F[_], S, A](fa: F[A])(implicit F: Applicative[F]): StateT[F, S, A] = StateT(s => F.map(fa)(a => (s, a))) - def modify[F[_], S, A](f: S => F[A])(implicit F: Applicative[F]): StateT[F, S, A] = + def inspect[F[_], S, A](f: S => F[A])(implicit F: Applicative[F]): StateT[F, S, A] = StateT(s => F.map(f(s))(a => (s, a))) } diff --git a/tests/src/test/scala/cats/tests/StateTTests.scala b/tests/src/test/scala/cats/tests/StateTTests.scala index e7ebd9e290..d41511f4a0 100644 --- a/tests/src/test/scala/cats/tests/StateTTests.scala +++ b/tests/src/test/scala/cats/tests/StateTTests.scala @@ -29,6 +29,22 @@ class StateTTests extends CatsSuite { } } + test("State.inspect and StateT.inspect are consistent") { + forAll { (s: String, f: String => Int) => + val state: State[String, Int] = State.inspect(f) + val stateT: State[String, Int] = StateT.inspect(f.andThen(Eval.now)) + state.run(s) should === (stateT.run(s)) + } + } + + test("State.pure and StateT.lift are consistent") { + forAll { (s: String, i: Int) => + val state: State[String, Int] = State.pure(i) + val stateT: State[String, Int] = StateT.lift(Eval.now(i)) + state.run(s) should === (stateT.run(s)) + } + } + test("Cartesian syntax is usable on State") { val x = add1 *> add1 x.runS(0).value should === (2) From 90a228011c6b6f029544d86bcb322214c037e11a Mon Sep 17 00:00:00 2001 From: zainab-ali Date: Fri, 8 Jul 2016 17:10:44 +0100 Subject: [PATCH 3/3] Adding StateT.modify --- core/src/main/scala/cats/data/StateT.scala | 11 +++++++- .../test/scala/cats/tests/StateTTests.scala | 26 ++++++++++++++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/core/src/main/scala/cats/data/StateT.scala b/core/src/main/scala/cats/data/StateT.scala index a55960661a..13799da11f 100644 --- a/core/src/main/scala/cats/data/StateT.scala +++ b/core/src/main/scala/cats/data/StateT.scala @@ -137,8 +137,17 @@ object StateT extends StateTInstances { def lift[F[_], S, A](fa: F[A])(implicit F: Applicative[F]): StateT[F, S, A] = StateT(s => F.map(fa)(a => (s, a))) - def inspect[F[_], S, A](f: S => F[A])(implicit F: Applicative[F]): StateT[F, S, A] = + def inspect[F[_], S, A](f: S => A)(implicit F: Applicative[F]): StateT[F, S, A] = + StateT(s => F.pure((s, f(s)))) + + def inspectF[F[_], S, A](f: S => F[A])(implicit F: Applicative[F]): StateT[F, S, A] = StateT(s => F.map(f(s))(a => (s, a))) + + def modify[F[_], S](f: S => S)(implicit F: Applicative[F]): StateT[F, S, Unit] = + StateT(s => F.pure((f(s), ()))) + + def modifyF[F[_], S](f: S => F[S])(implicit F: Applicative[F]): StateT[F, S, Unit] = + StateT(s => F.map(f(s))(s => (s, ()))) } private[data] sealed trait StateTInstances extends StateTInstances1 { diff --git a/tests/src/test/scala/cats/tests/StateTTests.scala b/tests/src/test/scala/cats/tests/StateTTests.scala index d41511f4a0..43e4c77a36 100644 --- a/tests/src/test/scala/cats/tests/StateTTests.scala +++ b/tests/src/test/scala/cats/tests/StateTTests.scala @@ -32,7 +32,31 @@ class StateTTests extends CatsSuite { test("State.inspect and StateT.inspect are consistent") { forAll { (s: String, f: String => Int) => val state: State[String, Int] = State.inspect(f) - val stateT: State[String, Int] = StateT.inspect(f.andThen(Eval.now)) + val stateT: State[String, Int] = StateT.inspect(f) + state.run(s) should === (stateT.run(s)) + } + } + + test("State.inspect and StateT.inspectF are consistent") { + forAll { (s: String, f: String => Int) => + val state: State[String, Int] = State.inspect(f) + val stateT: State[String, Int] = StateT.inspectF(f.andThen(Eval.now)) + state.run(s) should === (stateT.run(s)) + } + } + + test("State.modify and StateT.modify are consistent") { + forAll { (s: String, f: String => String) => + val state: State[String, Unit] = State.modify(f) + val stateT: State[String, Unit] = StateT.modify(f) + state.run(s) should === (stateT.run(s)) + } + } + + test("State.modify and StateT.modifyF are consistent") { + forAll { (s: String, f: String => String) => + val state: State[String, Unit] = State.modify(f) + val stateT: State[String, Unit] = StateT.modifyF(f.andThen(Eval.now)) state.run(s) should === (stateT.run(s)) } }