Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scan1Semigroup method to Stream #2029

Merged
merged 2 commits into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2533,6 +2533,17 @@ final class Stream[+F[_], +O] private[fs2] (private val free: FreeC[F, O, Unit])
case Some((hd, tl)) => Pull.output1(hd) >> tl.scan_(hd: O2)(f)
}.stream

/**
* Like `[[scan1]], but uses the implicitly available `Semigroup[O2]` to combine elements.
*
* @example {{{
* scala> Stream(1,2,3,4).scan1Semigroup.toList
* res0: List[Int] = List(1, 3, 6, 10)
* }}}
*/
def scan1Semigroup[O2 >: O](implicit O2: Semigroup[O2]): Stream[F, O2] =
scan1(O2.combine)

/**
* Like `scan` but `f` is applied to each chunk of the source stream.
* The resulting chunk is emitted and the result of the chunk is used in the
Expand Down
10 changes: 10 additions & 0 deletions core/shared/src/test/scala/fs2/StreamCombinatorsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,16 @@ class StreamCombinatorsSuite extends Fs2Suite {
}
}

property("scan1Semigroup") {
forAll { (s: Stream[Pure, Int]) =>
val v = s.toVector
val f = (a: Int, b: Int) => a + b
s.scan1Semigroup.toVector == v.headOption.fold(Vector.empty[Int])(h =>
v.drop(1).scanLeft(h)(f)
)
}
}

test("sleep") {
val delay = 200.millis
// force a sync up in duration, then measure how long sleep takes
Expand Down