From ef956faa023510395a19e0600036cf5af75cf1ae Mon Sep 17 00:00:00 2001 From: Michael Pilquist Date: Tue, 10 Sep 2019 09:16:31 -0400 Subject: [PATCH] Added s.pull.lastOrError --- core/shared/src/main/scala/fs2/Stream.scala | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/shared/src/main/scala/fs2/Stream.scala b/core/shared/src/main/scala/fs2/Stream.scala index afbad8bf73..1eee2c7328 100644 --- a/core/shared/src/main/scala/fs2/Stream.scala +++ b/core/shared/src/main/scala/fs2/Stream.scala @@ -3959,6 +3959,13 @@ object Stream extends StreamLowPriority { go(None, self) } + /** Returns the last element of the input, if non-empty, otherwise fails the pull with a `NoSuchElementException`. */ + def lastOrError(implicit F: RaiseThrowable[F]): Pull[F, INothing, O] = + last.flatMap { + case None => Pull.raiseError(new NoSuchElementException) + case Some(o) => Pull.pure(o) + } + /** Like [[uncons]] but does not consume the chunk (i.e., the chunk is pushed back). */ def peek: Pull[F, INothing, Option[(Chunk[O], Stream[F, O])]] = uncons.flatMap {