Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the value of Scala function and Curryization?

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report