Learn a little bit of Scala's Currying function every day
1. Curring function definition?
Convert a function that originally takes two arguments into two functions. The first function receives the first argument, and then returns the second function that receives the second argument.
In the process of function call, it becomes the form of continuous call of two functions.
It is also reflected in the marathon source code as well as the Spark source code, so understand the Curring function in the form of () ().
2. Examples:
Object CurryingTest {def main (args: Array [String]): Unit = {println ("- >:\ t" + sum (3jin4)) println ("- >:\ t" + sum2 (5)) println ("- >:\ t" + sum2 (5) (1)) / / Corey function println ("- >:\ t" + sum3 (4) (8))} def sum (a: Int) B: Int) = a + b def sum2 (a: Int) = (b: Int) = > a + b / / the intermediate process def sum3 (a: Int) (b: Int) = a + b} when converting a sum function into a sum3 function
In fact, there is no real understanding of what scenarios the Curry function is used in.
For now, it's just that as long as you see
Sum3 (a: Int) (b: Int) = a + b
This form can be directly understood as or equivalent to
Sum (a: Int, b: Int) = a + b
Form