# Double broadcast

**URL:** <https://discourse.julialang.org/t/double-broadcast/127930>\
**Category:** General Usage\
**Tags:** broadcast\
**Created:** [April 10, 2025, 2:09pm UTC](https://discourse.julialang.org/t/double-broadcast/127930 "2025-04-10T14:09:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![atteson](https://avatars.discourse-cdn.com/v4/letter/a/91b2a8/32.png) [@atteson](https://discourse.julialang.org/u/atteson)\
**Post date:** [April 10, 2025, 2:09pm UTC](https://discourse.julialang.org/t/double-broadcast/127930/1 "2025-04-10T14:09:26Z")

</div>

I frequently find myself wishing to do a double broadcast, e.g.:

```julia
f = x->x+1
(v -> f.(v)).( [[1,2,3], [4,5,6]])

```

Is this the most concise way to do it?

---

<div class="post-metadata">

**Author:** ![yolhan\_mannes](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yolhan_mannes/32/220485_2.png) [@yolhan\_mannes](https://discourse.julialang.org/u/yolhan_mannes)\
**Post date:** [April 10, 2025, 2:16pm UTC](https://discourse.julialang.org/t/double-broadcast/127930/2 "2025-04-10T14:16:01Z")

</div>

since a function is not iterable, you could do

```julia
map.(f,[[1,2,3],[4,5,6]])

```

or make use of dispatch

```julia
julia> f(x) = x+1
f (generic function with 1 method)

julia> f(x::AbstractArray) = f.(x)
f (generic function with 2 methods)

julia> f.([[1,2,3],[4,5,6]])

```

---

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [April 10, 2025, 2:43pm UTC](https://discourse.julialang.org/t/double-broadcast/127930/3 "2025-04-10T14:43:03Z")

</div>

I never thought about broadcasting map before cause I’ve definitely also done `(v -> f.(v)).( [[1,2,3], [4,5,6]])` before. That’s an interesting workaround! It also reminds me of the opposite “half broadcast” trick `f.(Ref([1,2,3]), [4,5,6])` to result in `[f([1,2,3], 4), f([1,2,3], 5], ...]`.

---

<div class="post-metadata">

**Author:** ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)\
**Post date:** [April 10, 2025, 4:53pm UTC](https://discourse.julialang.org/t/double-broadcast/127930/4 "2025-04-10T16:53:09Z")

</div>

For completeness, another option is

```julia-repl
julia> Broadcast.BroadcastFunction(f).( [[1,2,3], [4,5,6]])
2-element Vector{Vector{Int64}}:
 [2, 3, 4]
 [5, 6, 7]

julia> Broadcast.BroadcastFunction(Broadcast.BroadcastFunction(f))( [[1,2,3], [4,5,6]]) # same, but replace the . with BroadcastFunction
2-element Vector{Vector{Int64}}:
 [2, 3, 4]
 [5, 6, 7]

julia> .+ # it's the same machinery used by dotted operators
Base.Broadcast.BroadcastFunction(+)

```

It’s equivalent to your `v -> f.(v)` anonymous function, although I would say it’s at least as ugly. But I’ll leave it here anyway as someone might find it useful for nesting multiple layers of broadcasting. Although for nested broadcasting, dispatch-based recursion (as mentioned above) is often nicer.

---

<div class="post-metadata">

**Author:** ![DanDeepPhase](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dandeepphase/32/24076_2.png) [@DanDeepPhase](https://discourse.julialang.org/u/DanDeepPhase)\
**Post date:** [April 10, 2025, 5:17pm UTC](https://discourse.julialang.org/t/double-broadcast/127930/5 "2025-04-10T17:17:52Z")

</div>

I generally use nested array comprehension for this type of problem.

```julia
[[x+1 for x in v] for v in [[1,2,3], [4,5,6]] ]

```
