Skip to content

Commit

Permalink
Small optimisations: evalMapFilter
Browse files Browse the repository at this point in the history
- evalMapFilter: directly implement with Pull.flatMapOutput
- Replace a few uses of `evalMap` with `foreach`
  • Loading branch information
diesalbla committed Oct 16, 2022
1 parent 91e7079 commit 1c7595e
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1032,8 +1032,11 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
* res0: List[Int] = List(4, 8)
* }}}
*/
def evalMapFilter[F2[x] >: F[x], O2](f: O => F2[Option[O2]]): Stream[F2, O2] =
evalMap(f).collect { case Some(v) => v }
def evalMapFilter[F2[x] >: F[x], O2](f: O => F2[Option[O2]]): Stream[F2, O2] = {
// Short definition: evalMap(f).collect { case Some(v) => v }
def evalOut(o: O): Pull[F2, O2, Unit] = Pull.eval(f(o)).flatMap(Pull.outputOption1)
underlying.flatMapOutput(evalOut).streamNoScope
}

/** Like `[[Stream#scan]]`, but accepts a function returning an `F[_]`.
*
Expand Down Expand Up @@ -2196,7 +2199,7 @@ final class Stream[+F[_], +O] private[fs2] (private[fs2] val underlying: Pull[F,
pauseWhenTrue: Stream[F2, Boolean]
): Stream[F2, O] =
Stream.eval(SignallingRef[F2, Boolean](false)).flatMap { pauseSignal =>
def writer = pauseWhenTrue.evalMap(pauseSignal.set).drain
def writer = pauseWhenTrue.foreach(pauseSignal.set)

pauseWhen(pauseSignal).mergeHaltBoth(writer)
}
Expand Down Expand Up @@ -4138,7 +4141,7 @@ object Stream extends StreamLowPriority {

def outcomeJoiner: F[Unit] =
outcomes.stream
.evalMap(identity)
.foreach(identity)
.compile
.drain
.guaranteeCase {
Expand Down

0 comments on commit 1c7595e

Please sign in to comment.