# How to call a function that returns a function efficiently

**URL:** https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851
**Category:** General Usage
**Created:** [June 21, 2018, 11:39am UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851 "2018-06-21T11:39:21Z")
**Posts on this page:** 1
**Showing post:** 9

<div class="post-metadata">

### Author: ![aaowens](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aaowens/32/12101_2.png) [@aaowens](https://discourse.julialang.org/u/aaowens)
#### Post date: [June 21, 2018, 11:25pm UTC](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851/9 "2018-06-21T23:25:24Z")

</div>

You can also use the `let` block trick to solve the type stability issue, which is much easier than what I did above.

```julia
using QuantEcon
using BenchmarkTools, Compat # Time benckmarking

function valor(ind_α::Int64, α_ap::Array{Float64,2})
    # Long iteration to compute something that depends on α
    aux = zeros(100)

    for ind_aux in 1:100
        for ind_ap in 1:5000
            aux[ind_aux] = aux[ind_aux] + α_ap[ind_α, ind_ap]
        end
    end

    # Define interpolation
    aux_inter = LinInterp(linspace(0.,254.,100), aux)

    return_f = let aux_inter = aux_inter, ind_α = ind_α
        function(ap::Float64)
                    # Compute consumption
                    c = ind_α + ap

                    return log(c) + 0.95*aux_inter(ap)
                end
            end
    return return_f
end

function torna()
	pf = zeros(5000)
    V = zeros(5000)
    α_ap = rand(5000,5000)
	for ind_z in 1:500
		# Get function to optimize
		aux_f = valor(ind_z,α_ap)

		# Solve
		pf[ind_z], V[ind_z] = golden_method(aux_f, 0., 100.)
	end
	return pf, V
end

```

However, it’s pretty slow regardless.

---

_[View the full topic](https://discourse.julialang.org/t/how-to-call-a-function-that-returns-a-function-efficiently/11851)._
