# Broadcasting iterator?

**URL:** https://discourse.julialang.org/t/broadcasting-iterator/60349
**Category:** General Usage
**Tags:** broadcasting, iterators
**Created:** [April 30, 2021, 9:34pm UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349 "2021-04-30T21:34:15Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 30, 2021, 9:34pm UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/1 "2021-04-30T21:34:15Z")

</div>

Say I have

```julia
x = [1 2]
y = [10, 20, 30]
f(a,b) = a+b+0.5

```

I want an iterator that goes through the elements of `f.(x,y)` (11.5, 21.5, 31.5, …) without allocating the output array. Bonus points for an iterator that can also give the indices of `x`, `y`, and their nonexistent broadcasted output per iteration: (1,1,1), (1,2,2), (1,3,3), … That last bit might be a bit trickier for arrays that don’t allow linear indexing.

---

<div class="post-metadata">

### Author: ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)
#### Post date: [April 30, 2021, 9:40pm UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/2 "2021-04-30T21:40:53Z")

</div>

`(f(a,b) for (a,b) in Iterators.product(x,y))`?

---

<div class="post-metadata">

### Author: ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)
#### Post date: [April 30, 2021, 9:42pm UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/3 "2021-04-30T21:42:21Z")

</div>

It might help to start with

```julia
x = [1 2]
y = [10, 20, 30]
f(a,b) = a+b+0.5
f.(x',y')
# 2×3 Array{Float64,2}:
# 11.5 21.5 31.5
# 12.5 22.5 32.5

```

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [April 30, 2021, 9:53pm UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/4 "2021-04-30T21:53:44Z")

</div>

I’m not exactly sure what product means but it doesn’t seem to mean broadcast. Say I have `z=[1, 2, 3]` and do `f.(y, z)`.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [May 1, 2021, 7:01am UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/5 "2021-05-01T07:01:14Z")

</div>

I read enough of github issue #19198 to learn that `b = Broadcast.broadcasted(f, x, y)` gives an iterable over the elements of `f.(x,y)`. `@time` suggests it doesn’t allocate the output array; `Broadcast.materialize(b)` does.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [May 3, 2021, 12:40am UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/6 "2021-05-03T00:40:10Z")

</div>

Couple more comments on the “bonus” part of the post after reading through `broadcast.jl`:

- `eachindex(b::Broadcasted)` will give an iterable of `b`’s indices.
- An index of `b` can also be used to “index” into the input arrays `x` and `y` to get the values used for that iteration: `Broadcast._getindex(b.args, b_index)`. This was the application I had in mind.
- If you really need the _indices_ of `x` and `y` per iteration: `Broadcast.newindex(x, b_index)`

---

<div class="post-metadata">

### Author: ![N5N3](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/n5n3/32/17663_2.png) [@N5N3](https://discourse.julialang.org/u/N5N3)
#### Post date: [May 3, 2021, 4:17pm UTC](https://discourse.julialang.org/t/broadcasting-iterator/60349/7 "2021-05-03T16:17:59Z")

</div>

Well, you just need [LazyArrays.jl](https://github.com/JuliaArrays/LazyArrays.jl)
