How to generate the permutation of a vector with elements selected from a certain set

If you want the “uncollected” form to only extract the length without the allocaiton, you can do:

all_perm(x, n) = Iterators.product([x for i = 1:n]...)

You can still do length(all_perm([0, 1], 3)) on this but you didn’t allocate all the permutations (in case there are many). But you would have to use vec(collect(all_perm([0,1], 3)) to get the result as a vector rather than a matrix (the default for ProductIterator).

1 Like