# \`broadcast\` with iterators?

**URL:** <https://discourse.julialang.org/t/broadcast-with-iterators/4567>\
**Category:** General Usage\
**Created:** [June 30, 2017, 3:07pm UTC](https://discourse.julialang.org/t/broadcast-with-iterators/4567 "2017-06-30T15:07:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![fasiha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fasiha/32/1146_2.png) [@fasiha](https://discourse.julialang.org/u/fasiha)\
**Post date:** [June 30, 2017, 3:07pm UTC](https://discourse.julialang.org/t/broadcast-with-iterators/4567/1 "2017-06-30T15:07:54Z")

</div>

Start with `x = randn(6, 4)`.

This works:

`broadcast((x,tup) -> x * tup[1] * tup[2], x, collect(Base.product(0:5, 0:3)))`

But of course I’d like to do this without that `collect` (and, eventually, in-place, overwriting `x`) but this doesn’t work:

`broadcast((x,tup) -> x * tup[1] * tup[2], x, Base.product(0:5,0:3))`

A friend tells me this is because iterators don’t know their own length, which makes sense since the error message involves not finding `getindex`. Is there any way to make this computation fast without `for` loops?

---

<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:** [June 30, 2017, 3:58pm UTC](https://discourse.julialang.org/t/broadcast-with-iterators/4567/2 "2017-06-30T15:58:44Z")

</div>

You can just use `map` instead of `broadcast`.

But in this case you could also just do `broadcast(*, x, 0:5, (0:3).')` or `x .* (0:5) .* (0:3).'` or `@. x * (0:5) * (0:3).'`.

---

<div class="post-metadata">

**Author:** ![fasiha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fasiha/32/1146_2.png) [@fasiha](https://discourse.julialang.org/u/fasiha)\
**Post date:** [June 30, 2017, 4:52pm UTC](https://discourse.julialang.org/t/broadcast-with-iterators/4567/3 "2017-06-30T16:52:20Z")

</div>

Thanks!

But I can’t do `map!((x,tup) -> x * tup[1] * tup[2], x, x, Base.product(0:5,0:3))` to overwrite `x` because `map!` can’t handle iterators, is that correct?

---

<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:** [June 30, 2017, 6:05pm UTC](https://discourse.julialang.org/t/broadcast-with-iterators/4567/4 "2017-06-30T18:05:02Z")

</div>

> [@fasiha](#):
>
> `map!` can’t handle iterators, is that correct?

Apparently not, though it seems like this would be a reasonable enhancement to work on.

Honestly, though, I think you’ve passed the point where it is easier and clearer just to write a loop.
