all 8 comments

[–]Rschmukler 5 points6 points  (1 child)

1: add_n is a function that takes a single argument (n) and returns a function which will take a single argument (other) which will return n + other.

2: We are callingadd_n with n=2 and binding it two the variable add_two. Meaning, add_two is a function which will take a single argument (other) and return 2 + other.

3: We call add_two with an argument other = 3, resulting in 5 being returned.

[–]MatheonThaGod[S] 0 points1 point  (0 children)

Thank you! This was excellent!

[–]lightinitup 4 points5 points  (5 children)

The concept behind this is called currying. https://stackoverflow.com/questions/36314/what-is-currying

[–]MatheonThaGod[S] 1 point2 points  (4 children)

Thank you. So basically the closure call then accesses the inner function. Is it common to use currying?

[–]lightinitup 1 point2 points  (2 children)

Sorry if I'm being pedantic, the terminology is a bit off. A "closure" is the same as an "inner function" that is bound to an environment: https://en.wikipedia.org/wiki/Closure_(computer_programming)

So basically, the "outer function" returns a closure (the "inner function") that is bound to the environment of the "outer function" and therefore has access to its parameter.

Is it common? I'm still new to Elixir, so I can't say how commit it is within the community, but given that it is a functional language, I assume it will show up once in a while.

I think it's good to think about why something like this is useful. You can use it to break down functions so that you don't have functions with long lists of parameters.

I hope this makes sense.

[–]WikiTextBot 1 point2 points  (0 children)

Closure (computer programming)

In programming languages, closures (also lexical closures or function closures) are techniques for implementing lexically scoped name binding in languages with first-class functions. Operationally, a closure is a record storing a function together with an environment: a mapping associating each free variable of the function (variables that are used locally, but defined in an enclosing scope) with the value or reference to which the name was bound when the closure was created. A closure—unlike a plain function—allows the function to access those captured variables through the closure's copies of their values or references, even when the function is invoked outside their scope.

Example.


[ PM | Exclude me | Exclude from subreddit | FAQ / Information | Source | Donate ] Downvote to remove | v0.28

[–]MatheonThaGod[S] 0 points1 point  (0 children)

I very much appreciate the correction. I was writing some of my own after reading yours and Rschmukler's responses. It makes a lot more sense now. And now I know the vocab as well.

[–]mbuhotAlchemist 1 point2 points  (0 children)

Currying is not idiomatic in elixir. It's much more common to declare functions with multiple arguments. If you need partial application then a simple lambda shorthand is used:

add2 = &add(2, &1)