Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the value of Scala function and Curryization?

Shulou Source: shulou.com Published: 2022-06-03 05:39:24 10月18日 Update

What is the value of Scala function and Curryization? in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

1. Function value

In Scala, you can create functions in functions, assign functions to references, or pass them as arguments to other functions.

Example 1:

Def totalResultOverRange (number:Int, codeBlok:Int = > Int): Int = {var result = 0 for (I (if (I% 2 = = 0) I else 0)) println ("result:" + result)

Call method 2:

/ / define a function value def codeFunc (I: Int): Int = {if (I% 2 = = 0) I else 0} var result = totalResultOverRange (10, codeFunc) println ("result:" + result)

The above two call methods result in:

Result:30

Multi-parameter example:

Def inject (arr: Array [Int], initial:Int, operation: (Int, Int) = > Int) = {var carryOver = initial arr.foreach (I = > carryOver = operation (carryOver, I)) carryOver}

Call 1:

Val array = Array val sum = inject (array, 0, (carryOver,elem) = > carryOver + elem) println ("Sum of elements in array is" + sum)

Result 1:

Sum of elements in array is 21

Call 2:

Val array = Array val sum = inject (array, 0, (carryOver,elem) = > Math.max (carryOver,elem)) println ("Max of elements in array is" + sum)

Result 2:

Max of elements in array is 6

Second, Curryalization

Curryization in Scala can convert a function from receiving multiple arguments to multiple parameter lists. If you want to call a function multiple times with the same set of arguments, you can use curry to reduce the noise and make the code more interesting.

Let's take a look at how Scala provides curry support. The method we want to write is not to receive a list of parameters, which has multiple parameters, but multiple lists of parameters, each of which can have one or more parameters.

In other words, instead of writing def foo (a: Int, b: Int, c: Int) {}, it's def foo (a: Int) (b: Int) (c: Int) {}. You can call this method like foo (1) (2) (3), foo (1) {2} {3}, or even foo {1} {2} {3}.

Example:

Def cusFoldLeft (arr:Array [Int], initial:Int) (operation: (Int,Int) = > Int) = {var carryOver = initial arr.foreach (e = > carryOver = operation (carryOver, e)) carryOver}

Call:

Val array = Array val max = cusFoldLeft (array, 0) {(carryOver, elem) = > Math.max (carryOver, elem)} println ("max:" + max)

Results:

Max:6 's answer to the question about the value of Scala function and Curryalization is shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Tags: Functions parameters methods multiple results examples questions more but help answers easy easy that is code content smell noise guys. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Shulou Technology Microsoft Docker MySQL