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

for loop bug? #5917

Closed
cncastillo opened this issue Feb 23, 2014 · 9 comments
Closed

for loop bug? #5917

cncastillo opened this issue Feb 23, 2014 · 9 comments

Comments

@cncastillo
Copy link

I have the following code:

julia> animals, colors
(["cat","dog","tiger","leon"],["blue","red","green","pink"])

julia> for (animal, color) in (animals, colors)
       print(animal," is ", color,"\n")
       end
cat is dog
blue is red

And the result its a little bit strange, Should this be happening (Version 0.3.0-prerelease)?. This also have problem with numbers:

julia> for (i,j,k)=([1:4],[1:4],[1:4])
       print("i: ",i," j: ",j," k: ",k,"\n")
       end
i: 1 j: 2 k: 3
i: 1 j: 2 k: 3
i: 1 j: 2 k: 3

julia> for (i,j,k)=([1:5],[1:5],[1:5])
       print("i: ",i," j: ",j," k: ",k,"\n")
       end
i: 1 j: 2 k: 3
i: 1 j: 2 k: 3
i: 1 j: 2 k: 3

julia> for (i,j,k)=([1:2],[1:2],[1:2])
       print("i: ",i," j: ",j," k: ",k,"\n")
       end
ERROR: BoundsError()
 in anonymous at no file

julia> for (i,j,k)=([1:3],[1:3],[1:3])
       print("i: ",i," j: ",j," k: ",k,"\n")
       end
i: 1 j: 2 k: 3
i: 1 j: 2 k: 3
i: 1 j: 2 k: 3
@johnmyleswhite
Copy link
Member

You might be using a syntax I'm not familiar with, but it seems like you want to use zip:

for (animal, color) in zip(animals, colors)
    print(animal," is ", color,"\n")
end

That produces:

cat is blue
dog is red
tiger is green
leon is pink

This was referenced Feb 23, 2014
@cncastillo
Copy link
Author

Why its needed to be used "zip" for doing that? I know that python also use it, but It wouldn't be more intuitive to only use a tuple?.

@Keno
Copy link
Member

Keno commented Feb 23, 2014

Tuples are iterable:
julia> for x in ((1,2),(2,3))
println(x)
end
(1,2)
(2,3)

Assignment to tuples discards superflous elements,
julia> (a,b) = (5,6,7,8)
(5,6,7,8)

julia> (a,b)
(5,6)

Combine the two and you get the behavior above. @johnmyleswhite described the solution.

@cncastillo
Copy link
Author

Thanks now i understand what was happening!

@Keno Keno closed this as completed Feb 23, 2014
@StefanKarpinski
Copy link
Member

That's a pretty weird behavior, I have to say. I kind of wish this failed rather than producing unexpected behavior. The most suspect thing is the destructuring and discarding trailing values.

@StefanKarpinski
Copy link
Member

Didn't mean to reopen. Wrong button. @github – please move the open/close buttons somewhere else. Having them next to the comment submission form is a horrible place.

@ivarne
Copy link
Member

ivarne commented Feb 23, 2014

Crosslinking #837, for a discussion about being explicit when ignoring trailing members of a tuple returned from a function. I think it will help correctness if there was some special syntax to ignore trailing elements in a tuple assignment, and Python apparently does require exact match on number of elements.

@toivoh
Copy link
Contributor

toivoh commented Feb 23, 2014

+1 for exact match. How about

a, b, rest... = sometuple

(Deja vu... I think we had this discussion somewhere else a little while
back)

@JeffBezanson
Copy link
Member

I like the a, b, rest... idea, but that's a good feature independent of this.

My concerns here are (1) you might want to extend functions to return more values without breaking client code, and (2) checking for exact match is less efficient since we'd have to generate extra code to check that no more values remain.

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

7 participants