Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Iterator.head()/tail() does not follow Traversable contract #2936

Open
digiovinazzo opened this issue Nov 18, 2024 · 0 comments
Open

Iterator.head()/tail() does not follow Traversable contract #2936

digiovinazzo opened this issue Nov 18, 2024 · 0 comments

Comments

@digiovinazzo
Copy link

Traversable interface is extended by several other interfaces of collections, one of those is Iterator.
But it behaves differently than the others when using head() and tail() methods, see here below the test that fails for Iterator, but not for all other sequential collections.

class ScratchTest {
    public static void main(String[] args) {
        testTraversable(LinkedHashSet.of(0, 1, 2, 3));
        testTraversable(List.of(0, 1, 2, 3));
        testTraversable(Queue.of(0, 1, 2, 3));
        testTraversable(Array.of(0, 1, 2, 3));
        testTraversable(Vector.of(0, 1, 2, 3));
        testTraversable(Iterator.of(0, 1, 2, 3)); // fails!
    }

    private static void testTraversable(Traversable<Integer> traversable) {
        assertEquals(Integer.valueOf(0), traversable.head());
        traversable = traversable.tail();
        assertEquals(Integer.valueOf(1), traversable.head());
        traversable = traversable.tail();
        assertEquals(Integer.valueOf(2), traversable.head());
        traversable = traversable.tail();
        assertEquals(Integer.valueOf(3), traversable.head());
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant