-
-
Save puffnfresh/a724471f910a4029c29d to your computer and use it in GitHub Desktop.
A clever use of unapply to get syntax on Scalaz process for error handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package shajra.extn.scalaz.stream | |
import scalaz.{ Bind, Traverse, Unapply } | |
import scalaz.stream.Process | |
trait Syntax { | |
implicit class ProcessSyntax[F[_], A](self: Process[F, A]) { | |
def through[B](c: Channel[F, A, B]): Process[F, B] = | |
ProcessFunctions.throughChannel(self, c) | |
} | |
case class ProcessMSyntax[F[_], MM[_], A] | |
(self: Process[F, MM[A]], trav: Traverse[MM]) { | |
def throughM[MB] | |
(c: Channel[F, A, MB]) | |
(implicit uBind: Unapply[Bind, MB] { type M[X] = MM[X] }) | |
: Process[F, MM[uBind.A]] = { | |
val substC = uBind.leibniz.subst[Channel[F, A, ?]](c) | |
ProcessFunctions.throughChannelM(self, substC)(uBind.TC, trav) | |
} | |
} | |
implicit def processSyntax[F[_], MA] | |
(p: Process[F, MA]) | |
(implicit uTrav: Unapply[Traverse, MA]) | |
: ProcessMSyntax[F, uTrav.M, uTrav.A] = | |
ProcessMSyntax(uTrav.leibniz.subst[Process[F, ?]](p), uTrav.TC) | |
} | |
object Syntax extends Syntax |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment