answerkey/streamingio/07.answer.md
def exists[I](f: I => Boolean): Pipe[I, Boolean] =
src => src.map(f).toPull.tally(using Monoid.booleanOr).toStream
def takeThrough[I](f: I => Boolean): Pipe[I, I] =
src => src.toPull.takeWhile(f)
.flatMap(remainder => remainder.take(1)).void.toStream
def dropWhile[I](f: I => Boolean): Pipe[I, I] =
src => src.toPull.dropWhile(f)
.flatMap(remainder => remainder).void.toStream
def existsHalting[I](f: I => Boolean): Pipe[I, Boolean] =
exists(f) andThen takeThrough(!_) andThen dropWhile(!_)
def last[I](init: I): Pipe[I, I] =
def go(value: I, p: Pull[I, Unit]): Pull[I, Unit] =
p.uncons.flatMap:
case Left(_) => Pull.Output(value)
case Right((hd, tl)) => go(hd, tl)
src => go(init, src.toPull).toStream
def existsHalting[I](f: I => Boolean): Pipe[I, Boolean] =
exists(f) andThen takeThrough(!_) andThen last(false)