Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
guersam committed Aug 22, 2016
1 parent 91d9f93 commit 85b0a27
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 35 deletions.
3 changes: 1 addition & 2 deletions yax/core/src/main/scala/doobie/hi/connection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,13 @@ import scalaz.{ Monad, ~>, Catchable, Foldable }
import scalaz.syntax.monad._
#-scalaz
#+cats
import cats.{ Foldable, Monad }
import cats.Foldable
import cats.implicits._
#-cats
#+fs2
import fs2.{ Stream => Process }
import fs2.util.{ Effect, ~> }
import fs2.Stream.{ attemptEval, eval, empty, fail, emits, repeatEval, bracket }
import fs2.interop.cats.reverse._
#-fs2

/**
Expand Down
16 changes: 12 additions & 4 deletions yax/core/src/main/scala/doobie/syntax/catchable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@ import doobie.util.{ catchable => C }
#+cats
import cats.Unapply
import scala.{ Either => \/ }
#-cats
#+fs2
import fs2.util.{ Catchable, Monad }
import fs2.util.Catchable
#-fs2
#-cats

/** Syntax for `Catchable` combinators defined in `util.catchable`. */
object catchable {

#+scalaz
class DoobieCatchableOps[M[_]: Monad, A](self: M[A])(implicit c: Catchable[M]) {
#-scalaz
#+fs2
class DoobieCatchableOps[M[_], A](self: M[A])(implicit c: Catchable[M]) {
#-fs2

#+cats
def attempt: M[Throwable \/ A] =
Expand All @@ -42,15 +47,18 @@ object catchable {
trait ToDoobieCatchableOps0 {

/** @group Syntax */
#+scalaz
implicit def toDoobieCatchableOpsUnapply[MA](ma: MA)(
implicit M0: Unapply[Monad, MA],
C0: Unapply[Catchable, MA]
): DoobieCatchableOps[M0.M, M0.A] =
#+scalaz
new DoobieCatchableOps[M0.M, M0.A](M0.apply(ma))(M0.TC, C0.TC.asInstanceOf[Catchable[M0.M]])
#-scalaz
#+cats
new DoobieCatchableOps[M0.M, M0.A](M0.subst(ma))(M0.TC, C0.TC.asInstanceOf[Catchable[M0.M]])
implicit def toDoobieCatchableOpsUnapply[MA](ma: MA)(
implicit C0: Unapply[Catchable, MA]
): DoobieCatchableOps[C0.M, C0.A] =
new DoobieCatchableOps[C0.M, C0.A](C0.subst(ma))(C0.TC)
#-cats

}
Expand Down
42 changes: 13 additions & 29 deletions yax/core/src/main/scala/doobie/util/compat.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@ package doobie.util

object compat {

// #+fs2
// object fs2 {
// import _root_.fs2.util.{ Catchable => Fs2Catchable}

// implicit def doobieCatchableToFs2Catchable[M[_]: Monad](implicit c: Catchable[M]): fs2.util.Catchable[M] =
// new fs2.util.Catchable[M] {
// def flatMap[A, B](a: M[A])(f: A => M[B]) = a.flatMap(f)
// def pure[A](a: A) = a.pure[M]
// def attempt[A](ma: M[A]) = c.attempt(ma).map(_.toEither)
// def fail[A](t: Throwable) = c.fail(t)
// }

// }
// #-fs2

#+cats
object cats {
import _root_.cats.{ Applicative, FlatMap, Monad, MonadCombine }
Expand All @@ -39,24 +24,23 @@ object compat {
implicit class MoreCatsMonadOps[F[_], A](fa: F[A])(implicit M: Monad[F])
extends applicative.MoreCatsApplicativeOps(fa)(M) {

def whileM[G[_]](p: F[Boolean])(implicit G: MonadCombine[G]): F[G[A]] =
M.ifM(p)(M.flatMap(fa)(x => M.map(whileM(p)(G))(xs => G.combineK(G.pure(x), xs))), M.pure(G.empty))
def whileM[G[_]](p: F[Boolean])(implicit G: MonadCombine[G]): F[G[A]] =
M.ifM(p)(M.flatMap(fa)(x => M.map(whileM(p)(G))(xs => G.combineK(G.pure(x), xs))), M.pure(G.empty))

def whileM_(p: F[Boolean]): F[Unit] =
M.ifM(p)(M.flatMap(fa)(_ => whileM_(p)), M.pure(()))
def whileM_(p: F[Boolean]): F[Unit] =
M.ifM(p)(M.flatMap(fa)(_ => whileM_(p)), M.pure(()))

def untilM[G[_]](cond: F[Boolean])(implicit G: MonadCombine[G]): F[G[A]] =
M.flatMap(fa)(x => M.map(whileM(M.map(cond)(!_))(G))(xs => G.combineK(G.pure(x), xs)))
def untilM[G[_]](cond: F[Boolean])(implicit G: MonadCombine[G]): F[G[A]] =
M.flatMap(fa)(x => M.map(whileM(M.map(cond)(!_))(G))(xs => G.combineK(G.pure(x), xs)))

def untilM_(cond: F[Boolean]): F[Unit] =
M.flatMap(fa)(_ => whileM_(M.map(cond)(!_)))
def untilM_(cond: F[Boolean]): F[Unit] =
M.flatMap(fa)(_ => whileM_(M.map(cond)(!_)))

def iterateWhile(p: A => Boolean): F[A] =
M.flatMap(fa)(y => if (p(y)) iterateWhile(p) else M.pure(y))

def iterateUntil(p: A => Boolean): F[A] =
M.flatMap(fa)(y => if (p(y)) M.pure(y) else iterateUntil(p))
def iterateWhile(p: A => Boolean): F[A] =
M.flatMap(fa)(y => if (p(y)) iterateWhile(p) else M.pure(y))

def iterateUntil(p: A => Boolean): F[A] =
M.flatMap(fa)(y => if (p(y)) M.pure(y) else iterateUntil(p))
}

}
Expand All @@ -79,7 +63,7 @@ object compat {
Kleisli.pure[M, E, Unit](()).flatMap(_ => ma)
def unsafeRunAsync[A](ma: Kleisli[M, E, A])(cb: Attempt[A] => Unit): Unit = Predef.???
}
}
}
#-fs2
}
#-cats
Expand Down

0 comments on commit 85b0a27

Please sign in to comment.