On 7 June 2014 17:48, Roelof W. [email protected] wrote:
Im trying to understand currying.
I understand how you can add, substract two numbers by using currying.
I think maybe you don’t actually understand what currying is.
Currying basically means: taking a function that takes N arguments, and
turning it into a nested sequence of N functions that each take 1
The practical use is “partial application”, where you assign values to
of the functions.
For example, take this lambda function:
mean = ->(a, b, c) do
(a + b + c) / 3
mean[1, 2, 6] # => 3
It takes three parameters, and returns their geometric mean. It doesn’t
make sense to call it with only one parameter.
If I curry it, I get a new lambda function which takes one parameter,
returns a new lambda function which remembers that parameter, and
the next parameter, etc.:
curried = mean.curry # ~= mean(?, ?, ?)
mean_1 = curried # ~= mean(1, ?, ?)
mean_1_2 = mean_1 # ~= mean(1, 2, ?)
mean_1_2 # ~= mean(1, 2, 3) => 3
mean_1_2 # ~= mean(1, 2, 9) => 4
mean_1_3 = mean_1 # ~= mean(1, 3, ?)
mean_1_3 # ~= mean(1, 3, 11) => 5
But I want to make it a step harder.
but that one does not give the 1 back.
I can’t even work out the intent of that function, to give advice on how
make it work. Does this not do what you want?
3 + one # => 4