Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Forked from shajra-cs/Syntax.scala
Created October 7, 2015 05:53
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 puffnfresh/a724471f910a4029c29d to your computer and use it in GitHub Desktop.
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
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