# Creating lots of anonymous functions in a loop

**URL:** <https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330>\
**Category:** General Usage\
**Created:** [March 26, 2019, 2:42am UTC](https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330 "2019-03-26T02:42:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [March 26, 2019, 2:42am UTC](https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330/1 "2019-03-26T02:42:06Z")

</div>

This is probably a silly question, but…

I often have a code pattern like the following:

```julia
for n = 1:something_large
    anon_func = x -> some_func(x, n) #Example anon func that needs to be defined within loop
    i = findfirst(anon_func, A)
    # some other code
end

```

Will a code pattern like this be creating a huge number of anonymous functions, and if so, are there any implications to this? Or will it create only one anonymous function at the code lowering point, and so there is basically nothing to worry about here? Any other potential pitfalls I should be aware of?

Cheers and thanks to all responders,

Colin

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [March 26, 2019, 2:48am UTC](https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330/2 "2019-03-26T02:48:11Z")

</div>

It create only one version of the lowered code.

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 26, 2019, 3:26am UTC](https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330/3 "2019-03-26T03:26:51Z")

</div>

> [@colintbowers](#):
>
> Or will it create only one anonymous function at the code lowering point, and so there is basically nothing to worry about here?

Yup, pretty much that. Each closure created in the loop will be a different instance of the same type, and compilation will only need to happen once. The only potential performance issue is [performance of captured variables in closures · Issue #15276 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/15276) The symptom to look for there is Core.Box in your `@code_warntype`

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [March 26, 2019, 3:41am UTC](https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330/4 "2019-03-26T03:41:01Z")

</div>

Great! Thanks for responding.

---

<div class="post-metadata">

**Author:** ![colintbowers](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/colintbowers/32/8033_2.png) [@colintbowers](https://discourse.julialang.org/u/colintbowers)\
**Post date:** [March 26, 2019, 3:42am UTC](https://discourse.julialang.org/t/creating-lots-of-anonymous-functions-in-a-loop/22330/5 "2019-03-26T03:42:30Z")

</div>

Ah! I’d seen the word “closure” pop up quite a lot around here but never quite worked out what it meant. The comments on that issue make things much clearer. Thanks for posting.

Cheers,

Colin
