# Broadcast over splatted tuples

**URL:** <https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690>\
**Category:** New to Julia\
**Created:** [January 1, 2021, 9:22am UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690 "2021-01-01T09:22:16Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)\
**Post date:** [January 1, 2021, 9:22am UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/1 "2021-01-01T09:22:16Z")

</div>

I can’t figure out the syntax for doing broadcasting and splatting at the same time  
Example:

```julia
func(x1, x2) = x1+x2
v = collect(zip(rand(100), rand(100)));

```

I have function `func` which takes 2 arguments, and I have vector of tuples. I could splat one element like `func(v[1]...)`. How can I broadcast over the splatted tuples? This doesn’t work: `func.(v...)`. I guess it makes sense, because the splat is being applied to the vector when it should be applied to the elements of the vector, and we really want to broadcast the splatting before broadcasting `func`. I looked to see if there’s some `splat` keyword that implements the splatting, so I could do `func.(splat.(v))`, but it seems such keyword doesn’t exist.

Help?

---

<div class="post-metadata">

**Author:** ![rikh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rikh/32/204104_2.png) [@rikh](https://discourse.julialang.org/u/rikh)\
**Post date:** [January 1, 2021, 10:29am UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/2 "2021-01-01T10:29:34Z")

</div>

Nice question! This is also something I struggle with. Best I could come up with is

```julia
julia> [func(t...) for t in v]
100-element Array{Float64,1}:
 0.7858096481491528
 1.6654849232758508
 1.8326554485741802
 ...

```

---

<div class="post-metadata">

**Author:** ![roflmaostc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/roflmaostc/32/30123_2.png) [@roflmaostc](https://discourse.julialang.org/u/roflmaostc)\
**Post date:** [January 1, 2021, 10:39am UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/3 "2021-01-01T10:39:18Z")

</div>

Of couse we can also write it using a `map`:

```julia
map(x -> func(x...), a);

```

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 1, 2021, 12:23pm UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/4 "2021-01-01T12:23:33Z")

</div>

> [@freeman](#):
>
> I looked to see if there’s some `splat` keyword that implements the splatting, so I could do `func.(splat.(v))` , but it seems such keyword doesn’t exist.

You should also be able to do `Base.splat(func).(v)`

---

<div class="post-metadata">

**Author:** ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)\
**Post date:** [January 1, 2021, 1:26pm UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/5 "2021-01-01T13:26:48Z")

</div>

Oh interesting, so a `splat` function actually exists. Do you know how the splat symbols `...` are implemented in terms of the `splat` function?

---

<div class="post-metadata">

**Author:** ![FPGro](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fpgro/32/20822_2.png) [@FPGro](https://discourse.julialang.org/u/FPGro)\
**Post date:** [January 1, 2021, 1:35pm UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/6 "2021-01-01T13:35:17Z")

</div>

The splatting operator `... ` is a language feature, while the `splat(fun) ` function is an adaptor exactly for the case that you mentioned. Implementation and docstring:

[https://github.com/JuliaLang/julia/blob/76952a88ea650ae8c6b6b1d010ef695ed9c8244d/base/operators.jl#L1150](https://github.com/JuliaLang/julia/blob/76952a88ea650ae8c6b6b1d010ef695ed9c8244d/base/operators.jl#L1150)

---

<div class="post-metadata">

**Author:** ![freeman](https://avatars.discourse-cdn.com/v4/letter/f/ec9cab/32.png) [@freeman](https://discourse.julialang.org/u/freeman)\
**Post date:** [January 1, 2021, 1:44pm UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/7 "2021-01-01T13:44:52Z")

</div>

Haha indeed, the example is the same as mine 🙂 thank you for this.

---

<div class="post-metadata">

**Author:** ![simeonschaub](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/simeonschaub/32/216566_2.png) [@simeonschaub](https://discourse.julialang.org/u/simeonschaub)\
**Post date:** [January 1, 2021, 2:04pm UTC](https://discourse.julialang.org/t/broadcast-over-splatted-tuples/52690/8 "2021-01-01T14:04:43Z")

</div>

As @FPGro pointed out, `...` is not implemented in terms of `Base.splat`, but is actually lowered to a call to `Core._apply_iterate`. You could even write what you want like this:

```julia
julia> Core._apply_iterate.(iterate, func, v)
100-element Vector{Float64}:
[...]

```

(but it’s really an implementation detail, so probably don’t do that)
