# In closure, compile away a step in a calculation internal to some function called in a closure?

**URL:** <https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765>\
**Category:** Internals & Design\
**Created:** [November 3, 2023, 4:27pm UTC](https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765 "2023-11-03T16:27:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [November 3, 2023, 4:27pm UTC](https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765/1 "2023-11-03T16:27:55Z")

</div>

I probably should have posted this thread in ‘internals’ rather than ‘general usage’.

Is there a way to compile away a step in a calculation internal to some function called in a closure but without manually decomposing the function into its constituent calculations within the closure?

> [@Tricks to compile away step in calculation?](https://discourse.julialang.org/t/tricks-to-compile-away-step-in-calculation/105760):
>
> Is there a way to compile away a step in a calculation internal to some function called in a closure but without manually decomposing the function into its constituent calculations within the closure? Below I provide a MWE in OLS because this is the simplest example I could think of: suppose we are going to run a loop of regressions using the same covariates but different outcome variables. We use a closure to create a new function that treats covariates as a constant. my\_pinv(X) = (X'X)^-1 \*…

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [November 3, 2023, 4:29pm UTC](https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765/2 "2023-11-03T16:29:49Z")

</div>

The Internals category is for the internals of the Julia implementation.

---

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [November 3, 2023, 4:39pm UTC](https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765/3 "2023-11-03T16:39:58Z")

</div>

I think my question is related to internals…

Core of the question is for a closure like:

```julia
X = something 
g = let x = X
    y -> f(x, y) 
end 

```

Is it currently possible for the Julia compiler, on first method call, to precalculate things within f(x,y) given X ? If so, how can this be achieved (e.g., is there some macro, like @inline, that would do this)? If not, is this something that could be feasibly accomplished in a later version of Julia?

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [November 3, 2023, 4:43pm UTC](https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765/4 "2023-11-03T16:43:43Z")

</div>

I’ll answer in the original thread.

---

<div class="post-metadata">

**Author:** ![croberts](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/croberts/32/9465_2.png) [@croberts](https://discourse.julialang.org/u/croberts)\
**Post date:** [November 3, 2023, 4:46pm UTC](https://discourse.julialang.org/t/in-closure-compile-away-a-step-in-a-calculation-internal-to-some-function-called-in-a-closure/105765/5 "2023-11-03T16:46:06Z")

</div>

Thank you!!!
