Skip to content

Commit

Permalink
Add unconsNonEmpty (typelevel#1527)
Browse files Browse the repository at this point in the history
  • Loading branch information
Valy Dia committed Oct 3, 2019
1 parent d5d1b54 commit ad08526
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion core/shared/src/main/scala/fs2/Stream.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3794,7 +3794,7 @@ object Stream extends StreamLowPriority {
hd.size match {
case 0 => tl.pull.uncons1
case 1 => Pull.pure(Some(hd(0) -> tl))
case n => Pull.pure(Some(hd(0) -> tl.cons(hd.drop(1))))
case _ => Pull.pure(Some(hd(0) -> tl.cons(hd.drop(1))))
}
}

Expand Down Expand Up @@ -3847,6 +3847,15 @@ object Stream extends StreamLowPriority {
else go(Nil, n, self)
}

/** Like [[uncons]] but skips over the empty chunk. */
def unconsNonEmpty: Pull[F, INothing, Option[(Chunk[O], Stream[F, O])]] =
uncons.flatMap {
case None => Pull.pure(None)
case Some((hd, tl)) =>
if (hd.isEmpty) tl.pull.unconsNonEmpty
else Pull.pure(Some((hd, tl)))
}

/** Drops the first `n` elements of this `Stream`, and returns the new `Stream`. */
def drop(n: Long): Pull[F, INothing, Option[Stream[F, O]]] =
if (n <= 0) Pull.pure(Some(self))
Expand Down

0 comments on commit ad08526

Please sign in to comment.