Skip to content

Commit

Permalink
changed requirement to Alternative / added syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
kailuowang committed Mar 26, 2017
1 parent fd0e2b7 commit e948b8a
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
10 changes: 5 additions & 5 deletions core/src/main/scala/cats/Monad.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import syntax.either._

/**
* Execute an action repeatedly as long as the given `Boolean` expression
* returns `true`. The condition is evalated before the loop body.
* Collects the results into an arbitrary `MonadCombine` value, such as a `List`.
* returns `true`. The condition is evaluated before the loop body.
* Collects the results into an arbitrary `Alternative` value, such as a `Vector`.
*/
def whileM[G[_], A](p: F[Boolean])(body: => F[A])(implicit G: MonadCombine[G]): F[G[A]] = {
def whileM[G[_], A](p: F[Boolean])(body: => F[A])(implicit G: Alternative[G]): F[G[A]] = {
val b = Eval.later(body)
tailRecM[G[A], G[A]](G.empty)(xs => ifM(p)(
ifTrue = {
Expand Down Expand Up @@ -52,9 +52,9 @@ import syntax.either._
/**
* Execute an action repeatedly until the `Boolean` condition returns `true`.
* The condition is evaluated after the loop body. Collects results into an
* arbitrary `MonadCombine` value, such as a `List`.
* arbitrary `Alternative` value, such as a `Vector`.
*/
def untilM[G[_], A](f: F[A])(cond: => F[Boolean])(implicit G: MonadCombine[G]): F[G[A]] = {
def untilM[G[_], A](f: F[A])(cond: => F[Boolean])(implicit G: Alternative[G]): F[G[A]] = {
val p = Eval.later(cond)
flatMap(f)(x => map(whileM(map(p.value)(!_))(f))(xs => G.combineK(G.pure(x), xs)))
}
Expand Down
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/all.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ trait AllSyntax
with GroupSyntax
with InvariantSyntax
with ListSyntax
with MonadSyntax
with MonadCombineSyntax
with MonadErrorSyntax
with MonadFilterSyntax
Expand Down
7 changes: 7 additions & 0 deletions core/src/main/scala/cats/syntax/applicative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@ package syntax

trait ApplicativeSyntax {
implicit def catsSyntaxApplicativeId[A](a: A): ApplicativeIdOps[A] = new ApplicativeIdOps[A](a)
implicit def catsSyntaxApplicative[F[_], A](fa: F[A]): ApplicativeOps[F, A] = new ApplicativeOps[F, A](fa)
}

final class ApplicativeIdOps[A](val a: A) extends AnyVal {
def pure[F[_]](implicit F: Applicative[F]): F[A] = F.pure(a)
}

final class ApplicativeOps[F[_], A](fa: F[A])(implicit F: Applicative[F]) {
def replicateA(n: Int): F[List[A]] = F.replicateA(n, fa)
def unlessA(cond: Boolean): F[Unit] = F.unlessA(cond)(fa)
def whenA(cond: Boolean): F[Unit] = F.whenA(cond)(fa)
}
15 changes: 15 additions & 0 deletions core/src/main/scala/cats/syntax/monad.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package cats
package syntax

trait MonadSyntax {
implicit def catsSyntaxMonad[F[_]: Monad, A](fa: F[A]): MonadOps[F, A] = new MonadOps(fa)
}

final class MonadOps[F[_], A](fa: F[A])(implicit M: Monad[F]) {
def whileM[G[_]](p: F[Boolean])(implicit G: Alternative[G]): F[G[A]] = M.whileM(p)(fa)
def whileM_(p: F[Boolean]): F[Unit] = M.whileM_(p)(fa)
def untilM[G[_]](p: F[Boolean])(implicit G: Alternative[G]): F[G[A]] = M.untilM(fa)(p)
def untilM_(p: F[Boolean]): F[Unit] = M.untilM_(fa)(p)
def iterateWhile(p: A => Boolean): F[A] = M.iterateWhile(fa)(p)
def iterateUntil(p: A => Boolean): F[A] = M.iterateUntil(fa)(p)
}
1 change: 1 addition & 0 deletions core/src/main/scala/cats/syntax/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package object syntax {
object group extends GroupSyntax
object invariant extends InvariantSyntax
object list extends ListSyntax
object monad extends MonadSyntax
object monadCombine extends MonadCombineSyntax
object monadError extends MonadErrorSyntax
object monadFilter extends MonadFilterSyntax
Expand Down

0 comments on commit e948b8a

Please sign in to comment.