Skip to content

Commit 0d4be9d

Browse files
committed
Move sameElements to Iterator
1 parent 2af14fa commit 0d4be9d

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/main/scala/strawman/collection/Iterator.scala

+9
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ trait Iterator[+A] extends IterableOnce[A] { self =>
9797
def hasNext = self.hasNext && thatIterator.hasNext
9898
def next() = (self.next(), thatIterator.next())
9999
}
100+
def sameElements[B >: A](that: IterableOnce[B]): Boolean = {
101+
val those = that.iterator()
102+
while (hasNext && those.hasNext)
103+
if (next() != those.next())
104+
return false
105+
// At that point we know that *at least one* iterator has no next element
106+
// If *both* of them have no elements then the collections are the same
107+
hasNext == those.hasNext
108+
}
100109
}
101110

102111
object Iterator {

src/main/scala/strawman/collection/Seq.scala

+2-10
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,8 @@ trait SeqLike[+A, +C[X] <: Seq[X]]
5959
/** Do the elements of this collection are the same (and in the same order)
6060
* as those of `that`?
6161
*/
62-
def sameElements[B >: A](that: IterableOnce[B]): Boolean = {
63-
val these = coll.iterator()
64-
val those = that.iterator()
65-
while (these.hasNext && those.hasNext)
66-
if (these.next() != those.next())
67-
return false
68-
// At that point we know that *at least one* iterator has no next element
69-
// If *both* of them have no elements then the collections are the same
70-
these.hasNext == those.hasNext
71-
}
62+
def sameElements[B >: A](that: IterableOnce[B]): Boolean =
63+
coll.iterator().sameElements(that)
7264

7365
/** Method called from equality methods, so that user-defined subclasses can
7466
* refuse to be equal to other collections of the same kind.

0 commit comments

Comments
 (0)