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

zip2 and eachline don't play nice because next() is called twice #7369

Closed
swadey opened this issue Jun 22, 2014 · 3 comments
Closed

zip2 and eachline don't play nice because next() is called twice #7369

swadey opened this issue Jun 22, 2014 · 3 comments

Comments

@swadey
Copy link
Contributor

swadey commented Jun 22, 2014

Suppose you have test.txt which contains:

1
2
3
4
5

Running this code:

f1   = open("test.txt")
letters = ['a', 'b', 'c', 'd', 'e']
for (number, letter) in zip(eachline(f1), letters)
  println(strip(number), " ", letter)
end
close(f1)

Results in this:

[splunk:~/projects/julia]⇒ julia ziptest.jl
1 a
3 b
5 c

This seems wrong and it's caused by the fact that the next() method of EachLine changes state but it's called twice in Zip2 causing one state to be skipped per iteration:

next(z::Zip2, st) = ((next(z.a,st[1])[1], next(z.b,st[2])[1]),                                                                                                                        
                     (next(z.a,st[1])[2], next(z.b,st[2])[2])) 

Since next() can, in general have side effects, the I'm assuming the iterator protocol requires that next() only be called once per iteration, so I've submitted a corresponding PR #7370 (with the corresponding test). If that's not true, then EachLine should be fixed.

swadey added a commit to swadey/julia that referenced this issue Jun 22, 2014
@IainNZ
Copy link
Member

IainNZ commented Jun 22, 2014

See also #6284 which I think is related in spirit

@swadey
Copy link
Contributor Author

swadey commented Jun 22, 2014

right. It's the same problem but in a different part of iterators.jl. I guess it should be corrected everywhere or next() should be side-effect free everywhere.

JeffBezanson added a commit that referenced this issue Jun 23, 2014
@JeffBezanson
Copy link
Member

fixed by #7370

swadey added a commit to swadey/julia that referenced this issue Jun 23, 2014
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

3 participants