# How to use higher-order functions in Julia properly?

**URL:** <https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101>\
**Category:** General Usage\
**Created:** [December 2, 2020, 10:29am UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101 "2020-12-02T10:29:57Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KZiemian](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kziemian/32/9020_2.png) [@KZiemian](https://discourse.julialang.org/u/KZiemian)\
**Post date:** [December 2, 2020, 10:29am UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101/1 "2020-12-02T10:29:58Z")

</div>

As was [pointed out](https://discourse.julialang.org/t/evaluation-in-calling-scope/34897/10) by Steven G. Johnson, [higher-order functions](https://en.wikipedia.org/wiki/Higher-order_function) are great part of Julia language (topic for another big discusion) and often much more suitable to solve problems that metaprogramming tools, which are for many method of choice.

I want to learn more about them and how I should use them in Julia, but I new to this topic and I don’t know where to start.

As a side note, searching of both Documentation and YouTube about metaprogramming in Julia give you few answers immediately, when my searching about higher-order functions in documentation seems to found mostly pages with words like “higher dimensions” (I didn’t check all results). And one YT video from 2017. This is unfortunate, since as Tamas Papp [points out](https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022), metaprogramming is often “first trick in the bag”, when should be the last, left for advanced users.

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 2, 2020, 11:25am UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101/2 "2020-12-02T11:25:29Z")

</div>

There are a few words and examples about this here: [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions)

I don’t know of a document that lists all there is to know about the use of functions as values.

The main points I see are:

- A function can be used like a regular value: assigned to a variable, passed as argument, etc.

- Such function values can be created dynamically as anonymous functions, e.g. `f = x->2x`, or more verbose: `f = function(x) 2x end`. The function can refer to (capture) variables available in the scope where it’s defined.

- Passing functions as values is most common when working with `map`, `filter` and similar functions, as in many languages.

- Julia has some less common uses of functions as values including

- All functions have a type, which can be used for dispatch. As with any value, you can get the type with `typeof`:

---

<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:** [December 2, 2020, 11:46am UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101/3 "2020-12-02T11:46:44Z")

</div>

That’s a very nice writeup! Please consider making a PR to the docs (I am happy to review).

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 2, 2020, 4:26pm UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101/4 "2020-12-02T16:26:47Z")

</div>

I’m not sure what would be a good place in the documentation. Do you think it would belong in its own section on [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions) , although it overlaps with [#man-anonymous-functions](https://docs.julialang.org/en/v1/manual/functions/#man-anonymous-functions) and [#Function-composition-and-piping](https://docs.julialang.org/en/v1/manual/functions/#Function-composition-and-piping)?

---

<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:** [December 2, 2020, 4:32pm UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101/5 "2020-12-02T16:32:46Z")

</div>

I would put it after sections which build up prerequities (similarly to [Design Patterns with Parametric Methods](https://docs.julialang.org/en/v1/manual/methods/#Design-Patterns-with-Parametric-Methods)), so practically after [Function composition and piping](https://docs.julialang.org/en/v1/manual/functions/#Function-composition-and-piping).

---

<div class="post-metadata">

**Author:** ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)\
**Post date:** [December 2, 2020, 4:46pm UTC](https://discourse.julialang.org/t/how-to-use-higher-order-functions-in-julia-properly/51101/6 "2020-12-02T16:46:27Z")

</div>

OK I’ve put it in my Julia backlog which I hope to clear around Christmas. 🙂
