# Pattern to use additional variables in do block?

**URL:** <https://discourse.julialang.org/t/pattern-to-use-additional-variables-in-do-block/62871>\
**Category:** General Usage\
**Created:** [June 14, 2021, 9:39am UTC](https://discourse.julialang.org/t/pattern-to-use-additional-variables-in-do-block/62871 "2021-06-14T09:39:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonathanBieler](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jonathanBieler](https://discourse.julialang.org/u/jonathanBieler)\
**Post date:** [June 14, 2021, 9:39am UTC](https://discourse.julialang.org/t/pattern-to-use-additional-variables-in-do-block/62871/1 "2021-06-14T09:39:00Z")

</div>

I’m trying to write a small package to process files in an easy way. I often write very similar scripts that open files, read them record by record, process the record in some way (modify it or filter it out), close the files, etc… so I’d like to have a generic do-function to simplify that.

This is a minimal example, the real thing opens files and such, but that’s enough for my question :

```julia
process(f, collection) = f.(collection)

process(1:5) do record
    2*record
end

```

Now my issue is that I also often want to compute some overall statistics over records, but I’m not too sure how to deal with this gracefully, e.g. using globals isn’t so nice :

```julia
global total = 0
process(1:5) do record
    global total += record
    2*record
end

```

Is there a good way to do this ?

---

<div class="post-metadata">

**Author:** ![jzr](https://avatars.discourse-cdn.com/v4/letter/j/eb9ed0/32.png) [@jzr](https://discourse.julialang.org/u/jzr)\
**Post date:** [June 14, 2021, 9:58am UTC](https://discourse.julialang.org/t/pattern-to-use-additional-variables-in-do-block/62871/2 "2021-06-14T09:58:40Z")

</div>

```julia
process(x) = 10x
process(f, collection) = f.(collection)

let total = 0
    results = process(1:5) do record
        total += record
        2*record
    end
    total, results
end

```

---

<div class="post-metadata">

**Author:** ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)\
**Post date:** [June 14, 2021, 11:52am UTC](https://discourse.julialang.org/t/pattern-to-use-additional-variables-in-do-block/62871/3 "2021-06-14T11:52:42Z")

</div>

Note that `global` would only be needed if you are using that in global scope. A better idea is to wrap all into a function, and then the variables local to the scope of the function can be used inside the closure (that is what is going on in the example above with the `let` as well):

```julia

julia> function f(v)
           s = 0
           idxs = findall(v) do x
             if x > 0
               s += x^2
               return true
             else
               return false
             end
           end
           return idxs, s
       end
f (generic function with 1 method)

julia> v = rand(-5:5,10);

julia> f(v)
([3, 4, 5, 6, 10], 71)

```

Meaning that if you are implementing functions like that, there should be no problem in principle to compute what you want.
