From 60d8e5af0c1c6b0677d2892869894a4b804be445 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 f3f364a2a0..fa4d05079c 100644 --- a/core/src/main/scala/cats/Monad.scala +++ b/core/src/main/scala/cats/Monad.scala @@ -76,29 +76,19 @@ import simulacrum.typeclass * 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)(Left(_)) - else pure(Right(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(Right(a)) - else map(f)(Left(_)) - } + iterateUntilM(i)(_ => f)(p) } - } /** * Apply a monadic function iteratively until its result fails