In the midst of writing some ring arithmetic code, I found my Julia session producing very strange results for an element-wise modulo operation.
v = collect(1:20)
[(i, n) for i=1:20, n=(v .^2 .% 17) if i == n] # produces 19 tuples of (n, n)
"""
test_that_above_is_incorrect()
Show that the tuple isn't incrementing the index, or is throwing out the
value assigned to `i`. Whatever it is doing, applying the same logic in
a loop shows the only found element is unity.
"""
function test_that_above_is_incorrect()
out = []
for (counter, n) in enumerate(1:20)
if (n ^2) % 17 == counter
push!(out, n)
end
end
out # outputs a vector of a single element
end
I know the logical equivalent with enumerate
work correctly i.e.
[(i,n) for (i,n) in enumerate(v) if i == ((n ^ 2) % 17)]
is just fine.
So where did I go astray in the initial comprehension?
Thanks in advance