The purpose of function currying is to easily get specialized functions from more general functions. You achieve this by pre-setting some some parameters at a different time and keeping them fixed afterwards. It has nothing to do with the naming. In Python you can rename a variable/function easily at all times. Example: def simple_function(a): def line(b=0): def compute(x): return [a + b * xi for xi in x] return compute return line
Now this was not yet that exciting. It only replaced functions calls of type f(a,b,c) with calls of type f(a)(b)(c) which might even be seen as the less elegant style in Python.
But it allows you to do:
line_through_zero = simple_function(0) print('line through zero {}'.format(line_through_zero(1)(x))) # only slope and x which gives
line through zero [-4, -3, -2, -1, 0, 1, 2, 3] So the advantage of currying is that you get specialized functions that have fixed parameters and can be used instead of writing the more general form and setting the parameters fixed at each single call.
Alternatives to currying are: partial, lambda and default parameters. So in practice currying might be useful but you can also get around it if you want.
Currying means taking a function that takes multiple arguments and turning it into a chain of functions each taking one argument and returning the next function, until the last returns the result.
scala> def curriedSum(x: Int)(y: Int) = x + y
curriedSum: (Int)(Int)Int
scala> curriedSum(1)(2)
res5: Int = 3
A way to write functions with multiple parameter lists. For instance def f(x: Int)(y: Int) is a curried function with two parameter lists.A curried function is applied by passing several arguments lists, as in: f(3)(4). However, it is also possible to write a partial application of a curried function, such as f(3).A curried function is applied to multiple argument lists, instead of just one. Currying is like a kind of incremental binding of function arguments. More about Scala for beginners to advance level https://intellipaat.com/tutorial/scala-tutorial/
There are two real benefits.
The first one is block syntax:
So you will get "new language syntax".
The second one is type inference:
The purpose of function currying is to easily get specialized functions from more general functions. You achieve this by pre-setting some some parameters at a different time and keeping them fixed afterwards. It has nothing to do with the naming. In Python you can rename a variable/function easily at all times.
Example:
def simple_function(a):
def line(b=0):
def compute(x):
return [a + b * xi for xi in x]
return compute
return line
x = range(-4, 4, 1)
print('x {}'.format(list(x)))
print('constant {}'.format(simple_function(3)()(x)))
print('line {}'.format(simple_function(3)(-2)(x)))
gives
x [-4, -3, -2, -1, 0, 1, 2, 3]
constant [3, 3, 3, 3, 3, 3, 3, 3]
line [11, 9, 7, 5, 3, 1, -1, -3]
Now this was not yet that exciting. It only replaced functions calls of type f(a,b,c) with calls of type f(a)(b)(c) which might even be seen as the less elegant style in Python.
But it allows you to do:
line_through_zero = simple_function(0)
print('line through zero {}'.format(line_through_zero(1)(x))) # only slope and x
which gives
line through zero [-4, -3, -2, -1, 0, 1, 2, 3]
So the advantage of currying is that you get specialized functions that have fixed parameters and can be used instead of writing the more general form and setting the parameters fixed at each single call.
Alternatives to currying are: partial, lambda and default parameters. So in practice currying might be useful but you can also get around it if you want.
Currying means taking a function that takes multiple arguments and turning it into a chain of functions each taking one argument and returning the next function, until the last returns the result.
scala> def curriedSum(x: Int)(y: Int) = x + y
curriedSum: (Int)(Int)Int
scala> curriedSum(1)(2)
res5: Int = 3
A way to write functions with multiple parameter lists. For instance def f(x: Int)(y: Int) is a curried function with two parameter lists.A curried function is applied by passing several arguments lists, as in: f(3)(4). However, it is also possible to write a partial application of a curried function, such as f(3).A curried function is applied to multiple argument lists, instead of just one. Currying is like a kind of incremental binding of function arguments. More about Scala for beginners to advance level https://intellipaat.com/tutorial/scala-tutorial/