Skip to content

Commit

Permalink
Change repeatN to support 0 as argument
Browse files Browse the repository at this point in the history
This combinator now works equivalently to repeat.take(n)
  • Loading branch information
Leonhard Riedisser committed Oct 11, 2024
1 parent 9f51643 commit b8a5bb4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2574,8 +2574,9 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
* }}}
*/
def repeatN(n: Long): Stream[F, O] = {
require(n > 0, "n must be > 0") // same behaviour as sliding
if (n > 1) this ++ repeatN(n - 1)
require(n >= 0, "n must be >= 0")
if (n > 0) this ++ repeatN(n - 1)
else if (n == 0) Stream.empty
else this
}

Expand Down
2 changes: 1 addition & 1 deletion core/shared/src/test/scala/fs2/StreamSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,7 @@ class StreamSuite extends Fs2Suite {

property("repeatN") {
forAll(
Gen.chooseNum(1, 200),
Gen.chooseNum(0, 200),
Gen.chooseNum(1, 200).flatMap(i => Gen.listOfN(i, arbitrary[Int]))
) { (n: Int, testValues: List[Int]) =>
assertEquals(
Expand Down

0 comments on commit b8a5bb4

Please sign in to comment.