You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The key is that without a let the same i is captured by each closure, so the two closures actually do the same thing. With a let i, each iteration has a different i and the closures are thus different and each one when called will return the value of its own i, i.e. the corresponding iteration number. This is a very Lispy thing.
I have previously thought that it might be good if each iteration of a for loop implicitly had its own i, but that might adversely affect performance. I suspect, however, it would be easier to reason about, would possibly allow us to eliminate the let keyword, and might be more friendly to implicit parallelism since it breaks the dependency between different iterations.
[the example]
Does Julia intend that these two ways of assigning an anonymous function yield different results?
Yes, currently the intent is for for to mutate the loop variable. I would definitely consider having it (semantically) create a new binding for each iteration, which is more compatible with parallelization among other things.
[post from Stefan regarding example below]
The key is that without a let the same i is captured by each closure, so the two closures actually do the same thing. With a let i, each iteration has a different i and the closures are thus different and each one when called will return the value of its own i, i.e. the corresponding iteration number. This is a very Lispy thing.
I have previously thought that it might be good if each iteration of a for loop implicitly had its own i, but that might adversely affect performance. I suspect, however, it would be easier to reason about, would possibly allow us to eliminate the let keyword, and might be more friendly to implicit parallelism since it breaks the dependency between different iterations.
[the example]
Does Julia intend that these two ways of assigning an anonymous function yield different results?
M=Array(Any,2)
for i in 1:2
M[i] = () -> i
end
M1, M2
(2, 2)
while
N=Array(Any,2)
N[1] = () -> 1
N[2] = () -> 2
N1, N2
(1,2)
The text was updated successfully, but these errors were encountered: