# Map to generator

**URL:** <https://discourse.julialang.org/t/map-to-generator/1411>\
**Category:** General Usage\
**Tags:** question\
**Created:** [January 11, 2017, 11:00am UTC](https://discourse.julialang.org/t/map-to-generator/1411 "2017-01-11T11:00:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 11, 2017, 11:00am UTC](https://discourse.julialang.org/t/map-to-generator/1411/1 "2017-01-11T11:00:37Z")

</div>

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

```julia
(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

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

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [January 11, 2017, 3:52pm UTC](https://discourse.julialang.org/t/map-to-generator/1411/2 "2017-01-11T15:52:34Z")

</div>

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

A slightly more evil trick would be to define

```nohighlight
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.

---

<div class="post-metadata">

**Author:** ![bramtayl](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bramtayl/32/3614_2.png) [@bramtayl](https://discourse.julialang.org/u/bramtayl)\
**Post date:** [January 12, 2017, 4:46am UTC](https://discourse.julialang.org/t/map-to-generator/1411/3 "2017-01-12T04:46:10Z")

</div>

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.
