# Broadcast a function to a generator of tuples

**URL:** <https://discourse.julialang.org/t/broadcast-a-function-to-a-generator-of-tuples/28672>\
**Category:** General Usage\
**Created:** [September 12, 2019, 1:42am UTC](https://discourse.julialang.org/t/broadcast-a-function-to-a-generator-of-tuples/28672 "2019-09-12T01:42:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)\
**Post date:** [September 12, 2019, 1:42am UTC](https://discourse.julialang.org/t/broadcast-a-function-to-a-generator-of-tuples/28672/1 "2019-09-12T01:42:54Z")

</div>

I have a generator that produces tuples, and I’d like to call a function with the contents of each tuple as arguments.  
The output I’m looking for should be equivalent to `2(1:3)`.

```julia-auto
julia> g = ((x,x) for x=1:3)
Base.Generator{UnitRange{Int64},getfield(Main, Symbol("##265#266"))}(getfield(Main, Symbol("##265#266"))(), 1:3)

julia> f(a,b) = a+b
f (generic function with 1 method)

julia> f.(g)
ERROR: MethodError: no method matching f(::Tuple{Int64,Int64})

julia> f(first(g)...) # works as expected for the first tuple
2

julia> f.(g...) # attempting to call f() on first element of all tuples?
ERROR: MethodError: no method matching f(::Int64, ::Int64, ::Int64)

julia> f.(Ref(g)...) # splatting each generator term
ERROR: MethodError: no method matching f(::Tuple{Int64,Int64})

```

#### Edit:

Two options I found:

```julia-auto
julia> map(x -> f(x...), g)
3-element Array{Int64,1}:
 2
 4
 6

```

```julia-auto
julia> fwrapper(t) = f(t...)
fwrapper (generic function with 1 method)

julia> fwrapper.(g)
3-element Array{Int64,1}:
 2
 4
 6

```

Looks like both of these produce arrays. Wondering if there’s a recommended way to keep this behaving a generator. For example, if I wanted to pass these along to `sum`, would it unnecessary allocate the array first?

```julia-auto
julia> sum(fwrapper.(g))
12

```

#### Edit 2:

These seem to be the best approach:

```julia-auto
# Generator
julia> (f(t...) for t in g)
Base.Generator{Base.Generator{UnitRange{Int64}.......

# For use in other functions like this:
julia> sum(f(t...) for t in g)
12

```

---

<div class="post-metadata">

**Author:** ![milesf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/milesf/32/9289_2.png) [@milesf](https://discourse.julialang.org/u/milesf)\
**Post date:** [September 12, 2019, 2:24am UTC](https://discourse.julialang.org/t/broadcast-a-function-to-a-generator-of-tuples/28672/2 "2019-09-12T02:24:51Z")

</div>

Copying answer here, so I can flag this post as answered.

```julia
julia> g = ((x,x) for x=1:3)
Base.Generator{UnitRange{Int64},......

julia> f(a,b) = a+b
f (generic function with 1 method)

julia> (f(t...) for t in g)
Base.Generator{Base.Generator{UnitRange{Int64}.......

```

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [September 12, 2019, 2:29am UTC](https://discourse.julialang.org/t/broadcast-a-function-to-a-generator-of-tuples/28672/3 "2019-09-12T02:29:01Z")

</div>

You can use `Base.splat` like `sum(Base.splat(f), g)` but I don’t know if it is a public API. (It’s also pretty trivial to implement.)
