Skip to content

Commit

Permalink
fixed the interpretation of interleaved applicative effects
Browse files Browse the repository at this point in the history
  • Loading branch information
etorreborre committed Mar 23, 2017
1 parent 7bccde1 commit 67a316e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 9 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ You can also check out [this presentation](http://bit.ly/eff_flatmap_2016) at fl

You add `eff` as an sbt dependency:
```scala
libraryDependencies += "org.atnos" %% "eff" % "4.0.1"
libraryDependencies += "org.atnos" %% "eff" % "4.0.2"

// to write types like Reader[String, ?]
addCompilerPlugin("org.spire-math" %% "kind-projector" % "0.9.3")
Expand Down
9 changes: 9 additions & 0 deletions jvm/src/test/scala/org/atnos/eff/EffSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class EffSpec extends Specification with ScalaCheck { def is = s2"""
An effect can be translated into other effects of the same stack $translateIntoEffect

Applicative calls can be optimised by "batching" requests $optimiseRequests
Interleaved applicative calls can be interpreted properly $interleavedApplicative (see release notes for 4.0.2)

"""

Expand Down Expand Up @@ -409,6 +410,14 @@ class EffSpec extends Specification with ScalaCheck { def is = s2"""
result ==== optimisedResult
}

def interleavedApplicative = {
type S = Fx2[Option, String Either ?]
val action = (1 to 4).toList.traverseA(i =>
if (i % 2 == 0) OptionEffect.some[S, Int](i) else EitherEffect.right[S, String, Int](i))

action.runOption.runEither.run ==== Right(Some(List(1, 2, 3, 4)))

}

/**
* Helpers
Expand Down
4 changes: 4 additions & 0 deletions notes/4.0.2.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Correctness fix.

The new incarnation of the generic interpret had an embarrassing bug for applicative calls.
It is fixed now.
2 changes: 1 addition & 1 deletion shared/src/main/scala/org/atnos/eff/Interpret.scala
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ trait Interpret {
if (collected.effects.isEmpty)
collected.othersEff(continuation.interpret(e => runInterpreter(e)(interpreter))(interpretLast)).addLast(interpretLast(last))
else
interpreter.onApplicativeEffect(collected.effects, interpretContinuation(continuation)).addLast(interpretLast(last))
interpreter.onApplicativeEffect(collected.effects, interpretContinuation(collected.continuation(continuation, m))).addLast(interpretLast(last))
}
}

Expand Down
10 changes: 5 additions & 5 deletions shared/src/main/scala/org/atnos/eff/SafeEffect.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ trait SafeInterpretation extends SafeCreation { outer =>
case Some((l, m)) =>
attempt(l)(m) flatMap {
case Left(t) => outer.finalizerException[R](t)(m) >> pure((Right(a), errors.toList))
case Right(()) => pure((Right(a), errors.toList))
case Right(_) => pure((Right(a), errors.toList))
}
}

Expand All @@ -87,7 +87,7 @@ trait SafeInterpretation extends SafeCreation { outer =>
case Some((l, m)) =>
attempt(l)(m) flatMap {
case Left(t) => outer.finalizerException[R](t)(m) >> outer.exception[R, Out[A]](e)(m)
case Right(()) => outer.exception[R, Out[A]](e)(m)
case Right(_) => outer.exception[R, Out[A]](e)(m)
}
}
}
Expand All @@ -114,7 +114,7 @@ trait SafeInterpretation extends SafeCreation { outer =>
case Some((l, m)) =>
attempt(l)(m) flatMap {
case Left(t) => outer.finalizerException[R](t)(m) >> outer.exception[R, Unit](e)(m)
case Right(()) => outer.exception[R, Unit](e)(m)
case Right(_) => outer.exception[R, Unit](e)(m)
}
}

Expand All @@ -124,7 +124,7 @@ trait SafeInterpretation extends SafeCreation { outer =>
case Some((l, m)) =>
attempt(l)(m) flatMap {
case Left(t) => outer.finalizerException[R](t)(m) >> Eff.impure(x, continuation)
case Right(()) => Eff.impure(x, continuation)
case Right(_) => Eff.impure(x, continuation)
}
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@ trait SafeInterpretation extends SafeCreation { outer =>
case Some((l, m)) =>
attempt(l)(m) flatMap {
case Left(t1) => outer.finalizerException[R](t1)(m) >> outer.exception[R, Out[A]](t)(m)
case Right(()) => exception[R, Out[A]](t)(m)
case Right(_) => exception[R, Out[A]](t)(m)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion try-eff.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
test -e ~/.coursier/cr || (mkdir -p ~/.coursier && wget -q -O ~/.coursier/cr https://git.io/vgvpD && chmod +x ~/.coursier/cr)
CLASSPATH="$(~/.coursier/cr fetch -q -p \
\
org.atnos:eff_2.12:4.0.1 \
org.atnos:eff_2.12:4.0.2 \
com.lihaoyi:ammonite_2.12.1:0.8.1 \
\
)" java ammonite.Main --predef 'import org.atnos.eff._, all._, syntax.all._'
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "4.0.1"
version in ThisBuild := "4.0.2"

0 comments on commit 67a316e

Please sign in to comment.