Skip to content

Instantly share code, notes, and snippets.

@benhutchison
Created September 22, 2016 04:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benhutchison/533f2d426dc8c6c6788e2f713dd446f8 to your computer and use it in GitHub Desktop.
Save benhutchison/533f2d426dc8c6c6788e2f713dd446f8 to your computer and use it in GitHub Desktop.
Collect Non-empty options within an Eff-monad
Welcome to the Ammonite Repl 0.6.2
(Scala 2.11.8 Java 1.8.0_66)
ben_hutchison-ben_hutchison@ load.ivy("org.typelevel" %% "cats" % "0.7.1")
ben_hutchison-ben_hutchison@ load.plugin.ivy("org.spire-math" %% "kind-projector" % "0.9.0")
ben_hutchison-ben_hutchison@
load.plugin.ivy("com.milessabin" % "si2712fix-plugin_2.11.8" % "1.2.0")
ben_hutchison-ben_hutchison@ load.ivy("org.atnos" %% "eff-cats" % "2.0.0-RC7")
ben_hutchison-ben_hutchison@ import org.atnos.eff._, all._, syntax.all._;
import org.atnos.eff._, all._, syntax.all._;
ben_hutchison-ben_hutchison@ import cats._, data._, implicits._;
import cats._, data._, implicits._;
ben_hutchison-ben_hutchison@ case class Command()
defined class Command
ben_hutchison-ben_hutchison@
def commandFor[E: _Option](strategy: String): Eff[E, Command] = strategy match {
case "Good" => fromOption(Some(Command()))
case "Bad" => fromOption(Option.empty[Command])
}
defined function commandFor
ben_hutchison-ben_hutchison@ val strategies = Vector("Good", "Bad")
strategies: Vector[String] = Vector("Good", "Bad")
ben_hutchison-ben_hutchison@ val commandsForStats_OptionInEffect = strategies.traverse(commandFor(_))
commandsForStats_OptionInEffect: Eff[Fx1[Option], Vector[Command]] = Impure(Union1(Some(Command())),Arrs(Vector(<function1>, <function1>)))
ben_hutchison-ben_hutchison@ commandsForStats_OptionInEffect.runOption.run
//compare vs res12
res11: Option[Vector[Command]] = None
ben_hutchison-ben_hutchison@ val commandsForStats_OptionInPayload = strategies.traverse(commandFor(_).runOption).map(_.flatten)
commandsForStats_OptionInPayload: Eff[NoFx, Vector[Command]] = Pure(Vector(Command()))
ben_hutchison-ben_hutchison@ commandsForStats_OptionInPayload.run
//compare vs res11
res12: Vector[Command] = Vector(Command())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment