Skip to content

Commit

Permalink
Stack-safe replicateA_ implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
s5bug committed Jan 29, 2021
1 parent 221ccf6 commit c695ff8
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions core/src/main/scala/cats/Applicative.scala
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,12 @@ import scala.annotation.implicitNotFound
* }}}
*/
def replicateA_[A](n: Int, fa: F[A]): F[Unit] =
if (n == 0) this.pure(())
else this.productR(fa, this.replicateA_(n - 1, fa))
def go(x: Int, step: F[Unit]): F[Unit] = {
if(x == 0) step
else go(x - 1, this.productR(fa, step))
}
go(n, this.pure(()))
}

/**
* Compose an `Applicative[F]` and an `Applicative[G]` into an
Expand Down

0 comments on commit c695ff8

Please sign in to comment.