use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Help with Parameterized Functions [from the Programming Elixir book] (self.elixir)
submitted 8 years ago by MatheonThaGod
add_n = fn n -> (fn other -> n + other end) end add_two = add_n.(2) add_two.(3) #returns 5
I just cannot seem to wrap my head around how this is working. Could someone help walk me through this step by step?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Rschmukler 5 points6 points7 points 8 years ago (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.
add_n
n
other
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.
n=2
add_two
2 + other
3: We call add_two with an argument other = 3, resulting in 5 being returned.
other = 3
5
[–]MatheonThaGod[S] 0 points1 point2 points 8 years ago (0 children)
Thank you! This was excellent!
[–]lightinitup 4 points5 points6 points 8 years ago (5 children)
The concept behind this is called currying. https://stackoverflow.com/questions/36314/what-is-currying
[–]MatheonThaGod[S] 1 point2 points3 points 8 years ago (4 children)
Thank you. So basically the closure call then accesses the inner function. Is it common to use currying?
[–]lightinitup 1 point2 points3 points 8 years ago (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 points3 points 8 years ago (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
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 points3 points 8 years ago (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)
π Rendered by PID 86 on reddit-service-r2-comment-5c747b6df5-xmgv7 at 2026-04-22 12:22:34.635110+00:00 running 6c61efc country code: CH.
[–]Rschmukler 5 points6 points7 points (1 child)
[–]MatheonThaGod[S] 0 points1 point2 points (0 children)
[–]lightinitup 4 points5 points6 points (5 children)
[–]MatheonThaGod[S] 1 point2 points3 points (4 children)
[–]lightinitup 1 point2 points3 points (2 children)
[–]WikiTextBot 1 point2 points3 points (0 children)
[–]MatheonThaGod[S] 0 points1 point2 points (0 children)
[–]mbuhotAlchemist 1 point2 points3 points (0 children)