Reputation: 722
If I want to do something on each pair of letters, it could look like this in Julia:
for l1 in 'a':'z'
for l2 in 'a':'z'
w = l1*l2
# ... do something with w ...
end
end
I want to generalise this to words of any length, given a value n
specifying the number of letters desired. How do I best do this in Julia?
Upvotes: 2
Views: 68
Reputation: 69949
You can use:
for ls in Iterators.product(fill('a':'z', n)...))
w = join(ls)
# ... do something with w ...
end
In particular if you wanted to collect them in an array you could write:
join.(Iterators.product(fill('a':'z', n)...))
or flatten it to a vector
vec(join.(Iterators.product(fill('a':'z', n)...)))
Note, however, that in most cases this will not be needed and for larger n
it is better not to materialize the output but just iterate over it as suggested above.
Upvotes: 5