I feel like I’m always writing [foo(a,b,c) for a, b, c in 1:n] when I mean [foo(a,b,c) for a in 1:n, b in 1:n, c in 1:n], so I’d like to add something to my startup.jl to make it so.
I feel like I’ve seen conflicting information about the ease / feasibility of things like this, so figured before I get too deep I’d ask for resources / suggestions over here
I think it’s worth learning the difference, actually.
for (a,b,c) in collection implies that collection contains triples that look like (a,b,c).
for a in A, b in B, c in C are 3 nested loops: first you loop over elements of A, then within this loop you loop over B, then within that loop, loop over C.
This is standard Julia syntax, you’ll find it in the wild and will get confused if you write a macro that modifies its meaning.
then is there some other syntax for perfect square/cube/etc matrices? I feel like writing the same thing three times is the wrong way. in your example even you give A B and C as separate objects
cross(it, n) = Iterators.product((it for _ in 1:n)...)
which allows to write cross(1:n, 3) instead of Iterators.product(1:n, 1:n, 1:n).
If it were not type piracy, overloading (1:n)^3 would also be an option.
I spotted that .. has the same precedence as : and left associativity which made me think that iter..n = cross(iter, n) would let me 1:n..3, but it actually results in a parse error. (even with spaces added, so unrelated to .3.)
am I misunderstanding something about the precedence table? why does this need parens?
I tried swapping the order as well in case I just had the wrong direction, but got the same result
using <| works l expected it to [a for a in (1:n)<|(3)] ; going one level further down to ∷ tries to reach around the whole comprehension ([a for a in 1:n)∷(3]) and going one level up to ++ breaks the range apart [a for a in 1:(n)++(3)], which all makes sense; but 1:n..3 doesn’t seem to be equivalent to either (1):(n..3) or (1:n)..(3).
This is simply because .. has no asscociated methods in Base. If you add a docstring, say with "docstring for `..`" x..y = x:y, then it will show up when you say ?...
a
yeah, I understood that; I was more saying `it’s not a symbol in any of the packages I currently happen to have open, is it used commonly anywhere else?`