# Closures and Let Blocks

**URL:** <https://discourse.julialang.org/t/closures-and-let-blocks/50197>\
**Category:** General Usage\
**Tags:** closure\
**Created:** [November 15, 2020, 6:43pm UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197 "2020-11-15T18:43:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nucklass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nucklass/32/11989_2.png) [@nucklass](https://discourse.julialang.org/u/nucklass)\
**Post date:** [November 15, 2020, 6:43pm UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197/1 "2020-11-15T18:43:18Z")

</div>

Anyone have any basic examples of when to use/what the effects are of having a let block along with a closure?

I’ve been looking to learn more about functional programming in Julia and reading through the manual, but I don’t think I understand the uses of a let block from reading the docs.

Any help is appreciated, and any more resources in functional programming with Julia (or in general ) would be greatly appreciated!!

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [November 15, 2020, 10:27pm UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197/2 "2020-11-15T22:27:05Z")

</div>

The classic case is when you want to create a new binding that will be captured in a closure. For example if you’re modifying a value in a while loop and capturing it, then that might not do what you want:

```julia
function fib_closures(n)
    fns = []
    a = b = 1
    while b <= n
        push!(fns, x -> x + b)
        a, b = b, a + b
    end
    return fns
end

```

```julia
julia> [f(0) for f in fib_closures(10)]
5-element Vector{Int64}:
 13
 13
 13
 13
 13

```

If you want each captured closure to get a private copy of `b` then you need to use `let` to create a fresh binding for each one (which can have the same name):

```julia
function fib_closures(n)
    fns = []
    a = b = 1
    while b <= n
        let b = b
            push!(fns, x -> x + b)
        end
        a, b = b, a + b
    end
    return fns
end

```

```julia
julia> [f(0) for f in fib_closures(10)]
5-element Vector{Int64}:
 1
 2
 3
 5
 8

```

---

<div class="post-metadata">

**Author:** ![nucklass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nucklass/32/11989_2.png) [@nucklass](https://discourse.julialang.org/u/nucklass)\
**Post date:** [November 16, 2020, 6:15pm UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197/3 "2020-11-16T18:15:47Z")

</div>

Thank you, this helps a bunch!  
I also understand there’s some well known type stability issues when referencing enclosed variables. I’ve heard that a let block can help with speed things up a bit, though I don’t quite understand why. Any insights on this?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [November 17, 2020, 6:36am UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197/4 "2020-11-17T06:36:00Z")

</div>

See [Performance of captured variable](https://docs.julialang.org/en/v1/manual/performance-tips/#man-performance-captured) in the manual.

---

<div class="post-metadata">

**Author:** ![nucklass](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nucklass/32/11989_2.png) [@nucklass](https://discourse.julialang.org/u/nucklass)\
**Post date:** [November 17, 2020, 6:48am UTC](https://discourse.julialang.org/t/closures-and-let-blocks/50197/5 "2020-11-17T06:48:07Z")

</div>

Thank you! Must’ve slipped through the cracks while going through the manual. Also, I’ve been using julia-repl in emacs quite a bit, so thanks for that!
