From 5742379f81bf533bfbc99e419ae57b53d7b21f54 Mon Sep 17 00:00:00 2001 From: "David R. Bild" Date: Wed, 9 Aug 2017 13:00:41 -0500 Subject: [PATCH] Implement iterateWhile in terms of iterateWhileM --- core/src/main/scala/cats/Monad.scala | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/core/src/main/scala/cats/Monad.scala b/core/src/main/scala/cats/Monad.scala index fc5fc59712..1cbe9800d9 100644 --- a/core/src/main/scala/cats/Monad.scala +++ b/core/src/main/scala/cats/Monad.scala @@ -76,29 +76,19 @@ import syntax.either._ * Execute an action repeatedly until its result fails to satisfy the given predicate * and return that result, discarding all others. */ - def iterateWhile[A](f: F[A])(p: A => Boolean): F[A] = { + def iterateWhile[A](f: F[A])(p: A => Boolean): F[A] = flatMap(f) { i => - tailRecM(i) { a => - if (p(a)) - map(f)(_.asLeft[A]) - else pure(a.asRight[A]) - } + iterateWhileM(i)(_ => f)(p) } - } /** * Execute an action repeatedly until its result satisfies the given predicate * and return that result, discarding all others. */ - def iterateUntil[A](f: F[A])(p: A => Boolean): F[A] = { + def iterateUntil[A](f: F[A])(p: A => Boolean): F[A] = flatMap(f) { i => - tailRecM(i) { a => - if (p(a)) - pure(a.asRight[A]) - else map(f)(_.asLeft[A]) - } + iterateUntilM(i)(_ => f)(p) } - } /** * Apply a monadic function iteratively until its result fails