On 7 June 2014 17:48, Roelof W. [email protected] wrote:
Hello,
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
argument.
The practical use is “partial application”, where you assign values to
some
of the functions.
For example, take this lambda function:
mean = ->(a, b, c) do
(a + b + c) / 3
end
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,
and
returns a new lambda function which remembers that parameter, and
accepts
the next parameter, etc.:
curried = mean.curry # ~= mean(?, ?, ?)
mean_1 = curried[1] # ~= mean(1, ?, ?)
mean_1_2 = mean_1[2] # ~= mean(1, 2, ?)
mean_1_2[6] # ~= mean(1, 2, 3) => 3
mean_1_2[9] # ~= mean(1, 2, 9) => 4
mean_1_3 = mean_1[3] # ~= mean(1, 3, ?)
mean_1_3[11] # ~= mean(1, 3, 11) => 5
But I want to make it a step harder.
end
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
to
make it work. Does this not do what you want?
def one
1
end
3 + one # => 4
Cheers