当前位置:网站首页>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边栏推荐
- 免费的 SSH 和 Telnet 客户端PuTTY
- Gaussian distribution, linear regression, logistic regression
- Thinking technology: how to solve the dilemma in work and life?
- [getting started] regular expression Basics
- Ora-00909: invalid number of parameters, caused by concat
- 日期 数据库日期 字符串 之间互相转换
- EasyExcel:读取Excel数据到List集合中
- 力扣 179、最大数
- Force buckle 179, max
- Fast implementation of thread mesh networking
猜你喜欢

我对于测试团队建设的意见

分数阶PID控制

427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)

The risk of multithreading -- thread safety

高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression

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

Assembly language - Wang Shuang Chapter 11 flag register - Notes

Small program of C language practice (consolidate and deepen the understanding of knowledge points)

0.0.0.0:x的含义

Proxy-Reflect使用详解
随机推荐
写一个 goroutine 实例, 同时练习一下 chan
高斯分布Gaussian distribution、线性回归、逻辑回归logistics regression
Create a basic WDM driver and use MFC to call the driver
Gaussian distribution, linear regression, logistic regression
TiDB的事务概览
Block level elements & inline elements
Information System Project Manager - Chapter VII project cost management
multiprocessing.pool详解
Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
【QT小点】QT下载链接
快速实现单片机和手机蓝牙通信
使用 WordPress快速个人建站指南
Distribution gaussienne, régression linéaire, régression logistique
AHB2APB桥接器设计(2)——同步桥设计的介绍
JVM整体结构解析
TiDB 中的数据库模式概述
JVM garbage collection mechanism
Caldera安装及简单使用
高斯分布Gaussian distribution、線性回歸、邏輯回歸logistics regression
JVM调优思路