# Calculation of Functions in a loop inside another Function

**URL:** <https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278>\
**Category:** General Usage\
**Created:** [January 13, 2021, 9:56am UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278 "2021-01-13T09:56:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)\
**Post date:** [January 13, 2021, 9:56am UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278/1 "2021-01-13T09:56:15Z")

</div>

Hi, I’m looking to calculate nested functions of form f(g(h(...k(x)...)) and have written a function that takes in two arguments. One is called `chain` which is the vector of `Function` defined in the order of their calculation and the input array `x`.

```julia
function chain_cal(chain:: Vector{Function}, x:: CuArray)
    n = size(chain)[1]
    
    for i in 1:n
        x = chain[i](x)
    end
    return x
end

```

I defined a variable `chain` with two functions named `square`

```julia
chain = Vector([square, square])

```

```julia
2-element Array{typeof(square),1}:
 square (generic function with 1 method)
 square (generic function with 1 method)

```

and when I run `chain_cal`, I get an error as follows

```julia
chain_cal(chain, x)

```

```julia
MethodError: no method matching chain_cal(::Array{typeof(square),1}, ::CuArray{Int64,1})
Closest candidates are:
  chain_cal(!Matched::Array{Array{Function,1},1}, ::CuArray) at In[16]:1
  chain_cal(!Matched::Array{Function,1}, ::CuArray) at In[31]:1

Stacktrace:
 [1] top-level scope at In[35]:1
 [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091

```

Is there a way where I can modify `chain_cal` such that it accepts all functions or do I have to modify my earlier defined `square` function such that it’s type is `Function`?

```julia
#=
Square each element in the input CUDA Array
=#
function square(x:: CuArray)
    return x.^2
end

```

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 13, 2021, 10:04am UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278/2 "2021-01-13T10:04:57Z")

</div>

The signature does not match since:

```julia
julia> typeof([square,square]) <: Vector{Function}
false

```

You can either update:

> [@tgautam03](#):
>
> `chain_cal(chain:: Vector{Function}, x:: CuArray)`

to `chain_cal(chain::Vector{<:Function}, x::CuArray)` since

```julia
julia> typeof([square,square]) <: Vector{<:Function}
true

```

or update

> [@tgautam03](#):
>
> `chain = Vector([square, square])`

to `Function[square, square]` which would create a `Vector{Function}` and thus match your signature since

```julia
julia> typeof(Function[square,square]) <: Vector{Function}
true

```

See [relevant manual section](https://docs.julialang.org/en/v1/manual/types/#Parametric-Composite-Types).

---

<div class="post-metadata">

**Author:** ![tgautam03](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tgautam03/32/20545_2.png) [@tgautam03](https://discourse.julialang.org/u/tgautam03)\
**Post date:** [January 13, 2021, 10:19am UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278/3 "2021-01-13T10:19:18Z")

</div>

Thanks.

Can you tell me why the user defined functions are not of type `Function`?

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [January 13, 2021, 10:21am UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278/4 "2021-01-13T10:21:14Z")

</div>

They are subtypes of `Function`:

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

julia> typeof(f) <: Function
true

```

---

<div class="post-metadata">

**Author:** ![tisztamo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tisztamo/32/16200_2.png) [@tisztamo](https://discourse.julialang.org/u/tisztamo)\
**Post date:** [January 13, 2021, 12:29pm UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278/5 "2021-01-13T12:29:55Z")

</div>

> [@tgautam03](#):
>
> Can you tell me why the user defined functions are not of type `Function` ?

Creating a unique type for every function allows the compiler to specialize higher-order functions (e.g. `map`) based on the types of their function arguments. You pass an anonymous function to `map` and it may be inlined inside `map`.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [January 13, 2021, 4:21pm UTC](https://discourse.julialang.org/t/calculation-of-functions-in-a-loop-inside-another-function/53278/6 "2021-01-13T16:21:40Z")

</div>

For a different implementation you could also use `∘` (though I haven’t worked with `CuArray`s, maybe there’s something special about them that interferes?)

I mean something like this:

```julia
square(x) = x.^2
x = [1,2,3]
chain = [square, square]

julia> ∘(chain...)(x)
3-element Array{Int64,1}:
  1
 16
 81

```

We can also chain scalar functions and make a broadcast call on the result:

```julia
chain = [x->x^2, sqrt, inv]; # These functions don't work on vectors...

julia> ∘(chain...).(x) # but we can use a broadcast call
3-element Array{Float64,1}:
 1.0
 0.5000000000000001
 0.3333333333333333

```
