Generator for formal language (a|b)*


#1

What’s the easiest way to construct a generator that (lazily) returns all words in (a|b)* ordered by its length?


#2

Don’t know if it is the easiest, but I would consider something like

using IterTools
fixedlen(seq, n) = product(Iterators.repeated(seq, n)...)
allseqs(seq) = Iterators.flatten(fixedlen(seq, i) for i in Iterators.countfrom(0))

eg

julia> collect(Iterators.take(allseqs([:a, :b]), 10))
10-element Array{Tuple,1}:
 ()          
 (:a,)       
 (:b,)       
 (:a, :a)    
 (:b, :a)    
 (:a, :b)    
 (:b, :b)    
 (:a, :a, :a)
 (:b, :a, :a)
 (:a, :b, :a)