Created
April 9, 2018 16:26
-
-
Save justinhj/b1ef60cd41e373fd0c947e87875f3b26 to your computer and use it in GitHub Desktop.
FS2 gist to pull from a stream until N items that return true for the provided predicate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def takeWhileRepeat[F[_],O](n: Long, f: O => Boolean): Pipe[F,O,O] = { | |
def go(s: Stream[F,O], wasTrueCount : Int) : Pull[F,O,Unit] = { | |
if(wasTrueCount == n) { | |
Pull.done | |
} | |
else { | |
s.pull.uncons1.flatMap { | |
case Some((o, tl)) => | |
if(f(o)) { | |
Pull.output1(o) >> go(tl, wasTrueCount + 1) | |
} | |
else { | |
Pull.output1(o) >> go(tl, wasTrueCount) | |
} | |
case None => | |
Pull.done | |
} | |
} | |
} | |
in => go(in, 0).stream | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment