# How are functors useful?

**URL:** <https://discourse.julialang.org/t/how-are-functors-useful/24110>\
**Category:** New to Julia\
**Tags:** functors\
**Created:** [May 11, 2019, 2:46pm UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110 "2019-05-11T14:46:27Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![c42f](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/c42f/32/52842_2.png) [@c42f](https://discourse.julialang.org/u/c42f)\
**Post date:** [May 14, 2019, 1:10am UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/21 "2019-05-14T01:10:32Z")

</div>

Interesting. This may just be an implementation detail — internally closures are lowered into a `struct` with a field for each capture:

[https://github.com/JuliaLang/julia/blob/236df47251c203c71abd0604f2f19bf1f9c639fd/src/julia-syntax.scm#L2742-L2751](https://github.com/JuliaLang/julia/blob/236df47251c203c71abd0604f2f19bf1f9c639fd/src/julia-syntax.scm#L2742-L2751)

So while you can currently get the captures out by name (and can even introspect them with `fieldnames`) I’m not sure it’s going to stay this way.

---

<div class="post-metadata">

**Author:** ![TsurHerman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tsurherman/32/1234_2.png) [@TsurHerman](https://discourse.julialang.org/u/TsurHerman)\
**Post date:** [May 14, 2019, 5:57am UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/23 "2019-05-14T05:57:43Z")

</div>

trying to repost here…

> [@TsurHerman](#):
>
> I use it to add average execution time calculation and average frame rate calculation to arbitrary functions.
> 
> For functions that are part of a complex workflow whos running time is heavily dependent on the input(real-time image processing)

> [@tk3369](#):
>
> That sounds interesting. Do you mind sharing the code or is it already in some public repo?

sure here it is, it is not very styled as it is not public

```julia
fprint_runtime(g,n) = begin
    acum = 0.0
    counter = 0
    last_print_time = time()
    (args...) -> begin
        t = time()
        ret = g(args...)
        acum += time() - t;
        counter += 1
        if (t - last_print_time > n)
            avg = round(1e3*acum/counter)
            print("Average runtime of $(counter) runs : $avg ms\n")
            last_print_time = time()
            acum = 0.0
            counter = 0
        end
        ret
    end
end
export fprint_runtime

```

it accepts `g` a general function and `n` the minimal amount of seconds between console runtime logging,  
and return a function that behaves like `g` that also logs its own runtime.

---

<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:** [May 14, 2019, 6:25am UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/24 "2019-05-14T06:25:24Z")

</div>

> [@Vasily\_Pisarev](#):
>
> I thought, a name captured inside a closure is not supposed to be accessible by name from the scope the closure is exported into.

But `x` isn’t in the scope at all:

```julia
julia> x
ERROR: UndefVarError: x not defined

```

You are calling

```julia
getproperty(f, :x)

```

to extract a value via an implementation detail:

```julia
julia> fieldnames(typeof(f))
(:x,)

```

---

<div class="post-metadata">

**Author:** ![Vasily\_Pisarev](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vasily_pisarev/32/7929_2.png) [@Vasily\_Pisarev](https://discourse.julialang.org/u/Vasily_Pisarev)\
**Post date:** [May 14, 2019, 1:18pm UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/25 "2019-05-14T13:18:46Z")

</div>

> You are calling `getproperty(f, :x)` to extract a value via an implementation detail

Right. It’s just that here is the first place I’ve found out that there _is_ a function to access the list of captured symbols and their values for a given closure.

---

<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:** [May 14, 2019, 1:59pm UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/26 "2019-05-14T13:59:03Z")

</div>

This is documented in

[https://docs.julialang.org/en/v1/devdocs/functions/#Closures-1](https://docs.julialang.org/en/v1/devdocs/functions/#Closures-1)

but, most importantly, you are talking about _internals_ of the language, not something you should rely on in normal usage.

If you want the fields, really do use a `struct`. Otherwise, future changes to the internal representation could easily break your code — for example, the compiler could change the names of the variables closed over at any point.

---

<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:** [May 14, 2019, 4:13pm UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/27 "2019-05-14T16:13:39Z")

</div>

Or decide on different types for the fields of the closure. Or decide that they will not change whereas that may not previously have been figured out.

---

<div class="post-metadata">

**Author:** ![Erik\_Engheim](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erik_engheim/32/8122_2.png) [@Erik\_Engheim](https://discourse.julialang.org/u/Erik_Engheim)\
**Post date:** [March 23, 2020, 5:03pm UTC](https://discourse.julialang.org/t/how-are-functors-useful/24110/28 "2020-03-23T17:03:35Z")

</div>

I know this is a late reply, but I thought I add some advantages about functors I did not see mentioned in this thread.

I have implemented various algorithms in both approaches. Sometimes I have some struct which must be passed in to select correct algorithm and parameters, while other times I use a functor.

Say you use the non-functor approach. Your calls would be something like `f(a, x)` where `a` is a parameter used by the algorithm and `x` is the input. With a functor it is is more like `f = F(a); f(x)`. The benefit of the former is that sometimes you want multiple operations operating on the same structure. E.g. one encryption and one decryption algorithm. In this case functors are not much of an advantage I suppose.

However the benefit of being able to call a function like `f(x)` where `x` is the input is because function composition tends to be a lot easier to accomplish with single argument function. Or function composition at least benefits from function only taking inputs and not parameters controlling the operations of the algorithm. It makes it easier to chain function calls with `|>`, to use them with `map`, `broadcast`, `filter` etc.

Functions which only take inputs and not parameters are more compatible with various libraries. It provides a more universal interface. It is a bit like Unix pipelines. The power comes form a simple standardised universal interface that a lot of programs can adhere to.

[Previous page](https://discourse.julialang.org/t/how-are-functors-useful/24110.md?page=1)
