diff --git a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala index d63e29ea..534f8948 100644 --- a/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala +++ b/compat/src/main/scala-2.11_2.12/scala/collection/compat/PackageShared.scala @@ -348,6 +348,9 @@ class IteratorExtensionMethods[A](private val self: c.Iterator[A]) extends AnyVa def sameElements[B >: A](that: c.TraversableOnce[B]): Boolean = { self.sameElements(that.iterator) } + def nextOption(): Option[A] = { + if (self.hasNext) Some(self.next()) else None + } def concat[B >: A](that: c.TraversableOnce[B]): c.TraversableOnce[B] = self ++ that def tapEach[U](f: A => U): c.Iterator[A] = self.map(a => { f(a); a }) } diff --git a/compat/src/test/scala/test/scala/collection/CollectionTest.scala b/compat/src/test/scala/test/scala/collection/CollectionTest.scala index b8ea12ae..733c070b 100644 --- a/compat/src/test/scala/test/scala/collection/CollectionTest.scala +++ b/compat/src/test/scala/test/scala/collection/CollectionTest.scala @@ -114,6 +114,14 @@ class CollectionTest { assertEquals((Seq("bar", "baz"), Seq("foo".length, "test".length)), res) } + @Test + def nextOption(): Unit = { + val it = Iterator(1, 2) + assertEquals(Some(1), it.nextOption()) + assertEquals(Some(2), it.nextOption()) + assertEquals(None, it.nextOption()) + } + @Test def tapEach(): Unit = { var count = 0