Running through Programming Elixir. Need some help with Parameterized Functions

Can anyone walk me through how this works?

add_n = fn n -> (fn other -> n + other end) end
add_two = add_n.(2)
#returns 5

might be useful
see the source code for more info
it is always cool for one to learn from such a discovery

1 Like

One anonymous function returns another anonymous function. This technical approach called carrying.

1 Like

add_n gets assigned a function, of arity 1, which will return another function of arity 1, which will return the sum of the two previously passed in arguments.

When you call add_n.(2) you get back a function fn other -> 2 + other end, you then call add_two.(3) which then returns 5.

1 Like