Get the App
SLTechnology News&Howtos  ›  Servers  › 

Learn a little bit of Scala's Currying function every day

Shulou Source: shulou.com Published: 2022-06-02 21:55:55 09月22日 Update

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

Tags: Function form two parameters source code procedure Cory example scene equivalence Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information Microsoft Huawei Docker