当前位置:网站首页>Currying Scala functions
Currying Scala functions
2022-06-27 06:32:00 【YaoYong_ BigData】
One 、 Definition
It transforms a function that accepts multiple parameters into a function that accepts a single parameter ( First parameter of the original function ) Function of , And return the technique of taking the remaining arguments and returning the result of the new function .
A simple understanding is to change the expression of a function, but its functional characteristics remain unchanged , Coriolism is actually very practical . Whether it is in improving applicability or in delaying execution or fixing volatile factors , Coritization technology plays an important role .
Two 、 example
First we define a function :
def add(x:Int,y:Int)=x+ySo when we apply , It should be used like this :add(1,2)
Now let's shape this function :
def add(x:Int)(y:Int) = x + ySo when we apply , It should be used like this :add(1)(2), The end result is the same 3, This way, ( The process ) It's called Coriolis .
3、 ... and 、 Implementation process

add(1)(2) It's actually calling two ordinary functions in turn ( Non Coriolis function ), The first call uses a parameter x, Returns the value of a function type , The second use of parameters y Call the value of this function type .
In essence, it first evolved into such a method :
def add(x:Int)=(y:Int)=>x+ySo what does this function mean ? Receive one x Is the parameter , Returns an anonymous function , The anonymous function is defined as : Receive one Int Type parameter y, Function body is x+y. Call this method now .
val result = add(1) Return to one result, that result The value of should be an anonymous function :(y:Int)=>1+y
So in order to get the results , We continue to call result.
val sum = result(2)The final printed result is 3.
Four 、 Complete example
object _02FuncDemo {
// Define a method ( currying ), Calculate two Int Type value
def calculate(a:Int, b:Int)(calc:(Int, Int) => Int) ={
calc(a, b)
}
def main(args: Array[String]): Unit = {
println(calculate(10, 10) {
(x, y) => x + y
})
println(calculate(1,2)(_ + _))
println(calculate(1,2)(_ * _))
println(calculate(1,2)(_ - _))
}
}Output results :
20
3
2
-1边栏推荐
- Free SSH and telnet client putty
- The risk of multithreading -- thread safety
- [getting started] regular expression Basics
- IDEA中关于Postfix Completion代码模板的一些设置
- Configuration of vscode korofileheader
- JVM的垃圾回收机制
- 可扩展哈希
- Maxcompute SQL 的查询结果条数受限1W
- 【QT小记】QT中正则表达式QRegularExpression的基本使用
- Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
猜你喜欢

建模竞赛-光传送网建模与价值评估

创建一个基础WDM驱动,并使用MFC调用驱动

Altium Designer 19 器件丝印标号位置批量统一摆放

信息系统项目管理师---第七章 项目成本管理

JVM object composition and storage

Crawler learning 5--- anti crawling identification picture verification code (ddddocr and pyteseract measured effect)

Caldera安装及简单使用

Us camera cloud service scheme: designed for lightweight video production scenes

Run opcua protocol demo on raspberry pie 4B to access kubeedge
[email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT"/>NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
随机推荐
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
The number of query results of maxcompute SQL is limited to 1W
Gaussian distribution, linear regression, logistic regression
HTAP 深入探索指南
Change the status to the corresponding text during MySQL query
爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
Scala函数柯里化(Currying)
Create a basic WDM driver and use MFC to call the driver
TiDB的使用限制
JVM overall structure analysis
JVM class loading mechanism
JS to implement bidirectional data binding
JVM object composition and storage
Assembly language - Wang Shuang Chapter 11 flag register - Notes
TiDB 中的数据库模式概述
Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
汇编语言-王爽 第13章 int指令-笔记
Maxcompute SQL 的查询结果条数受限1W
C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
[QT dot] realize the watchdog function to detect whether the external program is running