Generate all words of a given size from a fixed alphabet

Iterators.product is actually already lazy. I collected it above since it sounded like that’s what OP wanted, but we can just as well iterate over it lazily:

alphabet = 'a':'d' # or "abcd"
k = 2

itr = Iterators.product(ntuple(_ -> alphabet, k)...)

for word in Base.Generator(join, itr)
    println(word)
    word == "bb" && break
end

Output:

aa
ba
ca
da
ab
bb
5 Likes