# Generated Tuples of Functions

**URL:** https://discourse.julialang.org/t/generated-tuples-of-functions/59499
**Category:** Performance
**Created:** [April 17, 2021, 8:51pm UTC](https://discourse.julialang.org/t/generated-tuples-of-functions/59499 "2021-04-17T20:51:07Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [April 17, 2021, 8:51pm UTC](https://discourse.julialang.org/t/generated-tuples-of-functions/59499/1 "2021-04-17T20:51:07Z")

</div>

Continuing the discussion from [Performance and allocation issue with arrays of functions (v1.5)](https://discourse.julialang.org/t/performance-and-allocation-issue-with-arrays-of-functions-v1-5/45013):

I want to modify the question I originally posed above, to the case that the collection of functions on which I wish to evaluate may, themselves, be generated. I am thinking of the following example:

```julia
@generated function compute_values(x₀, Δt, nΔt, f::Tuple{Vararg{<:Any,K}}) where {K}
    quote
        x = x₀;
        f_vals = zeros($K, nΔt)
        for n in 1:nΔt
            x += 0.5 * Δt * x
            Base.Cartesian.@nexprs $K k -> f_vals[k,n] = (f[k])(x);
        end
        return f_vals
    end
end

x₀ = 1.0;
Δt = 0.5;
nΔt = 10^2;

f_tuple = (x->x.^p for p in 1:5)

compute_values(x₀, Δt, nΔt, f_tuple)

```

but this gives the error:

```julia
MethodError: no method matching compute_values(::Float64, ::Float64, ::Int64, ::Base.Generator{UnitRange{Int64}, var"#3#5"})
Closest candidates are:
  compute_values(::Any, ::Any, ::Any, ::Tuple{Vararg{Any, K}}) where K at In[3]:1

Stacktrace:
 [1] top-level scope
   @ In[6]:1
 [2] eval
   @ ./boot.jl:360 [inlined]
 [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
   @ Base ./loading.jl:1094

```

Any recommendations on how to handle this?

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [April 18, 2021, 7:33pm UTC](https://discourse.julialang.org/t/generated-tuples-of-functions/59499/2 "2021-04-18T19:33:37Z")

</div>

In this case, it is simply that `(x->x.^p for p in 1:5)` is not a tuple.

Use

```julia
f_tuple = ntuple(p -> (x -> x.^p), 5)

```

or

```julia
f_tuple = Tuple(x->x.^p for p in 1:5)

```

---

<div class="post-metadata">

### Author: ![gideonsimpson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gideonsimpson/32/1928_2.png) [@gideonsimpson](https://discourse.julialang.org/u/gideonsimpson)
#### Post date: [April 19, 2021, 4:31pm UTC](https://discourse.julialang.org/t/generated-tuples-of-functions/59499/3 "2021-04-19T16:31:55Z")

</div>

Ok, so that helps with what i had wanted to do. Now here’s a slight wrinkle. I would like to have something like:

```julia
Tuple(x->x.^p for p in 1:5, x->sin.(x))

```

where I have a sequence of functions which are generated from a loop, and then one more function, which I need to specify, and they all need to be grouped together in a single Tuple at the end. Note that the above does not work.

---

<div class="post-metadata">

### Author: ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)
#### Post date: [April 20, 2021, 10:20pm UTC](https://discourse.julialang.org/t/generated-tuples-of-functions/59499/4 "2021-04-20T22:20:11Z")

</div>

In that case, you may use

```julia
((x->x.^p for p in 1:5)..., x->sin.(x))

```

There might also be variants which are type stable.
