Generator comprehensions follow a similar format to array comprehensions, but use parentheses ()
instead of square brackets []
.
(expression for element = iterable)
Such an expression returns a Generator
object.
julia> (x^2 for x = 1:5)
Base.Generator{UnitRange{Int64},##1#2}(#1,1:5)
Generator comprehensions may be provided as the only argument to a function, without the need for an extra set of parentheses.
julia> join(x^2 for x = 1:5)
"1491625"
However, if more than one argument is provided, the generator comprehension requries its own set of parentheses.
julia> join(x^2 for x = 1:5, ", ")
ERROR: syntax: invalid iteration specification
julia> join((x^2 for x = 1:5), ", ")
"1, 4, 9, 16, 25"