Map to generator



I find generators useful when I want to avoid intermediate results. However, when the transformation is already wrapped in a function,

(f(x) for x in xs)

feels redundant. Is there a “standard” way to use a function like in map, but get a generator? Or should I just define something like

"""Return a generator mapping the elements of `c` using `f`."""
mapgen(f, c) = (f(x) for x in c)


I don’t think there is any built-in terse syntax for this.

A slightly more evil trick would be to define

Base.getindex(f::Function, iter) = (f(x) for x in iter)

in which case you can do e.g. sin[1:10] and it will return a generator.


You could do Base.Generator(f, xs) I think. I don’t use generators very often but if I do I find this syntax much more convenient.