I’m using CartesianIndices to generate lists resembling base-k numbers. I want the convert the output into a “flat” vector of vectors, irrespective of the number of “digits” passed to CartesianIndices. The lists will generally be short (less than a thousand or so) and I’ll be doing this many, many times for all kinds of cases. I want to do it in a good, Julian way, but I feel that my best solution is a bit roundabout. Here’s my solution:

[[j for j in Tuple(i)] for i in CartesianIndices((2,2,2))][1:end]|>sort
8-element Vector{Vector{Int64}}:
[1, 1, 1]
[1, 1, 2]
[1, 2, 1]
[1, 2, 2]
[2, 1, 1]
[2, 1, 2]
[2, 2, 1]
[2, 2, 2]

The reverse is a good idea. The main speed difference between yours and @mbauman is the missing collect. His returns vectors but yours tuples. That makes me wonder if I really need vectors, but right now that is how my code works.

I was wondering about my use of Tuple vs. Vector … both of you had pretty fast implementations to start with though. I tried Tuple in order to flatten the CartesianIndices … perhaps there is a better way to do that, that would also allow it to be done with Vector.

Probably because here n is a runtime value, not a literal constant: that makes ntuple type-unstable. If you want tuples to be fast, you need to make sure the compiler knows the length — either because it is a literal constant, or because you passed it as a type (via Val(n)).

(If you want arrays of small fixed-length vectors, it’s always going to be better to use either arrays of tuples or arrays of StaticArrays, provided that the length of the vectors is known at compile time.)

I also changed the function and the REPL version to be identical, which did make it more of an apples and apples comparison, but the change for 3 => Val(3) made the lion’s share of the difference.

The most ‘Julian’ way is probably to not do it at all, or at least only lazily. The most efficient thing is to do nothing, avoid redundancies and allocations. I don’t know exactly what you want this for, but it smells a bit of ‘unnecessary wastefulness’

Each one of my vectors represents a polynomial, a polynomial that is invariant under the action of a group (it has some symmetry in it, for the non-groupists). But some of these polynomials are equivalent by symmetry (they are on the same orbit). I need to go through each list, find the equivalent ones and delete them from the list.

So I was going to go through the list, apply the group action (as a permutation of the indices) and then keep a running list of the unique ones…now that you make me explain it…I might be able to use a custom hash function to turn the indices into a base-10 number and then just keep track of the unique polynomials in a hash table…