Skip to content

Instantly share code, notes, and snippets.

@pavlosgi
Last active April 22, 2016 12:02
Show Gist options
  • Save pavlosgi/6cf3682beb3a09d1a2907ce8a86e48a8 to your computer and use it in GitHub Desktop.
Save pavlosgi/6cf3682beb3a09d1a2907ce8a86e48a8 to your computer and use it in GitHub Desktop.
Trying to use XorT with Eff
package habito.effects.tests
object EffXor {
import org.atnos.eff._
import Eff._
import Effects._
import EvalEffect._
import Tag._
import WriterCreation._
import cats.data._
import cats.syntax.all._
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 HadoopStack._
import org.atnos.eff.syntax.all._
import cats.std.all._
val action = for {
// read a file from hadoop
s <- XorT.pure(readFile("/tmp/data"))
y <- XorT.pure(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