Skip to content

Instantly share code, notes, and snippets.

@danbills
Created July 21, 2018 21:08
Show Gist options
  • Save danbills/ff86dc5d4d69bd07ce095e9627c5edea to your computer and use it in GitHub Desktop.
Save danbills/ff86dc5d4d69bd07ce095e9627c5edea to your computer and use it in GitHub Desktop.
scanState in fs2
def scanState[F[_], S, A, B](init: S)(f: A => State[S, B]): Pipe[F, A, B] =
_.pull.scanSegments(init){
case (previousState, segment: Segment[A, Unit]) =>
segment.mapAccumulate(previousState)({
case (state, o1) =>
val stateFunc = f(o1)
(stateFunc run state).value
}).mapResult(_._2)
}.stream
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment