Skip to content

Instantly share code, notes, and snippets.

@pavlosgi
Last active April 22, 2016 12:00
Show Gist options
  • Save pavlosgi/c33e26460e594dd6095340b3532a1fdc to your computer and use it in GitHub Desktop.
Save pavlosgi/c33e26460e594dd6095340b3532a1fdc to your computer and use it in GitHub Desktop.
Doing sequence with effects
package habito.effects
object EffSequence {
import org.atnos.eff._
import Eff._
import Effects._
import EvalEffect._
import WriterCreation._
import cats.data._
import cats.syntax.all._
import Tag._
object HadoopStack {
trait HadoopTag
case class HadoopConf(mappers: Int)
type HadoopReader[A] = Reader[HadoopConf, A] @@ HadoopTag
type WriterString[A] = Writer[String, A]
type Hadoop = HadoopReader |: WriterString |: Eval |: NoEffect
object Hadoop {
implicit val HadoopReaderMember: Member.Aux[HadoopReader, Hadoop, WriterString |: Eval |: NoEffect] =
Member.first
implicit val WriterStringMember: Member.Aux[WriterString, Hadoop, HadoopReader |: Eval |: NoEffect] =
Member.successor
implicit val EvalMember: Member.Aux[Eval, Hadoop, HadoopReader |: WriterString |: NoEffect] =
Member.successor
}
import Hadoop._
def askHadoopConf: Eff[Hadoop, HadoopConf] =
ReaderEffect.askTagged
def readFile(path: String): Eff[Hadoop, String] =
for {
c <- askHadoopConf
_ <- tell("Reading from "+path)
} yield c.mappers.toString
import ReaderImplicits._
def runHadoopReader[R <: Effects, A](conf: HadoopConf): Eff[HadoopReader |: R, A] => Eff[R, A] =
(e: Eff[HadoopReader |: R, A]) => ReaderEffect.runReaderTagged(conf)(e)
}
// this imports the `into` and runXXX syntax
import org.atnos.eff.syntax.all._
import HadoopStack._
val list = List(readFile("/tmp/data"), readFile("/tmp/data"), readFile("/tmp/data"))
import cats.std.all._
val efs = list.sequence
val action = for {
// read a file from hadoop
s <- readFile("/tmp/data")
} yield ()
import org.atnos.eff.implicits._
// and we can run the composite action
action.runReaderTagged(HadoopConf(10)).runWriter.runEval.run
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment