Skip to content
Permalink
Browse files

Fix ActionT.read associativity (#72)

* Add failing test

* Fix ActionT

* Naming things
  • Loading branch information...
notxcain committed May 15, 2019
1 parent e8c8c96 commit babafacf804377c252e66a6be0eb42b8dff43c7b
@@ -23,10 +23,7 @@ final class ActionT[F[_], S, E, A] private (
ActionT { (s0, u, es0) =>
unsafeRun(s0, u, es0).flatMap {
case Folded.Next((es1, a)) =>
es1
.foldM(s0)(u)
.traverse(f(a).unsafeRun(_, u, es1))
.map(_.flatten)
f(a).unsafeRun(s0, u, es1)
case Impossible =>
impossible[(Chain[E], B)].pure[F]
}
@@ -68,8 +65,8 @@ object ActionT extends ActionTFunctions with ActionTInstances {
}

trait ActionTFunctions {
def read[F[_]: Applicative, S, E]: ActionT[F, S, E, S] =
ActionT((s, _, es) => (es, s).next.pure[F])
def read[F[_]: Monad, S, E]: ActionT[F, S, E, S] =
ActionT((s, u, es) => es.foldM(s)(u).map((es, _)).pure[F])

def append[F[_]: Applicative, S, E](e: NonEmptyChain[E]): ActionT[F, S, E, Unit] =
ActionT((_, _, es0) => (es0 ++ e.toChain, ()).next.pure[F])
@@ -0,0 +1,27 @@
package aecor.tests

import aecor.data.Folded.Next
import aecor.data.{ ActionT, Folded }
import cats.Id
import cats.data.{ Chain, NonEmptyChain }
import cats.instances.string._
import cats.syntax.applicative._
import cats.syntax.flatMap._
import org.scalatest.FunSuite

class ActionTSpec extends FunSuite {
def append(s: String): ActionT[Id, String, String, Unit] = ActionT.append(NonEmptyChain.one(s))
def read: ActionT[Id, String, String, String] = ActionT.read

def run[A](action: ActionT[Id, String, String, A]): Folded[(Chain[String], A)] =
action.run("", (l, r) => (l ++ r).pure[Folded])

test("ActionT.read associativity") {
val n1 @ Next((es, out)) = run(append("a") >> (append("b") >> read))
val n2 = run(append("a") >> append("b") >> read)
assert(es === Chain("a", "b"))
assert(out === "ab")
assert(n1 === n2)
}

}

0 comments on commit babafac

Please sign in to comment.
You can’t perform that action at this time.