当前位置:网站首页>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边栏推荐
- My opinion on test team construction
- ORA-00909: 参数个数无效,concat引起
- 1317. convert an integer to the sum of two zero free integers
- Configuration of vscode korofileheader
- Spark SQL common time functions
- JVM overall structure analysis
- [QT notes] simple understanding of QT meta object system
- The risk of multithreading -- thread safety
- Quick realization of Bluetooth ibeacn function
- tracepoint
猜你喜欢

JVM常用指令

JVM class loading mechanism

The restart status of the openstack instance will change to the error handling method. The openstack built by the container restarts the compute service method of the computing node and prompts the gi

爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)

美摄云服务方案:专为轻量化视频制作场景打造

Convolution neural network -- Application of CNN model (ore prospecting prediction)

Multithreading basic part part 1

快速实现Thread Mesh组网详解

卷积神经网络---CNN模型的应用(找矿预测)

0.0.0.0:x的含义
随机推荐
tar: /usr/local:归档中找不到tar: 由于前次错误,将以上次的错误状态退出
第 299 场周赛 第四题 6103. 从树中删除边的最小分数
310. minimum height tree
爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
快速实现单片机和手机蓝牙通信
古典密码体制--代换和置换
飞行器翼尖加速度和控制面的MPC控制
Proxy-Reflect使用详解
Configuration of vscode korofileheader
Unrecognized VM option ‘‘
可扩展哈希
路由器和交换机的区别
1317. convert an integer to the sum of two zero free integers
Ahb2apb bridge design (2) -- Introduction to synchronous bridge design
The form verifies the variables bound to the V-model, and the solution to invalid verification
Scala advanced_ Member access modifier
块级元素&行内元素
MATLAB快速将影像的二维坐标转换为经纬度坐标
vscode korofileheader 的配置
力扣 179、最大数