Hi!
I just fond a problem on how Linq Sequences are handled on Java implementation. The problem can be found when you run a Linq query over another Linq result.
var aSequence = new List<int>{ 0, 1, 2}.Select(i => i);
for (var i = 0 ; i < 2; i++)
{
for (var j = 0 ; j < 2; j++) {
writeLn("{0}, {1} => {2}", i, j, aSequence.Any(v =>
{
writeLn("predicate: {0}, {1}, {2}", i, j, v);
return v == i || v == j;
}));
}
}
It will output:
predicate: 0, 0, 0
0, 0 => true
predicate: 0, 1, 1
0, 1 => true
predicate: 1, 0, 2
1, 0 => false
1, 1 => false
But it should be:
predicate: 0, 0, 0
0, 0 => true
predicate: 0, 1, 0
0, 1 => true
predicate: 1, 0, 0
1, 0 => true
predicate: 1, 1, 0
predicate: 1, 1, 1
1, 1 => true
Looking at the generated code inside cooper.jar I found the problem is you are implementing Iterator and Iterable on the same class:
private final class $Where$d__0<T> implements Iterator, Iterable {
Iterable
should return a new Iterator
on each call to Iterator<T> iterator();
but this class is always returning it self, so it is the same instance. Providing the same instance causes it to keep the iterator state, which implies you can only run over the elements of this Iterable
once over its lifetime.
Thanks!