当前位置:网站首页>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边栏推荐
- Download CUDA and cudnn
- 路由器和交换机的区别
- Dev++ environment setting C language keyword display color
- 研究生数学建模竞赛-无人机在抢险救灾中的优化应用
- Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
- [QT notes] simple understanding of QT meta object system
- 第 299 场周赛 第四题 6103. 从树中删除边的最小分数
- JVM object composition and storage
- 【QT小点】实现看门狗功能,检测外部程序是否在运行
- Kubesphere cluster configuration NFS storage solution - favorite
猜你喜欢

427- binary tree (617. merge binary tree, 700. search in binary search tree, 98. verify binary search tree, 530. minimum absolute difference of binary search tree)

快速实现蓝牙iBeacn功能详解

路由器和交换机的区别

JS to implement bidirectional data binding

汇编语言-王爽 第9章 转移指令的原理-笔记

Multithreading basic part2

Kubesphere cluster configuration NFS storage solution - favorite

My opinion on test team construction

Multithreading basic part part 1

爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)
随机推荐
【QT小记】QT中正则表达式QRegularExpression的基本使用
TiDB 中的SQL 基本操作
JVM garbage collection mechanism
乐观事务和悲观事务
Once spark reported an error: failed to allocate a page (67108864 bytes), try again
Gaussian distribution, linear regression, logistic regression
下载cuda和cudnn
Thinking technology: how to solve the dilemma in work and life?
Openresty usage document
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
An Empirical Evaluation of In-Memory Multi-Version Concurrency Control
multiprocessing.pool详解
How to check the frequency of memory and the number of memory slots in CPU-Z?
【养成系】常用正则表达式
KubeSphere 集群配置 NFS 存储解决方案-收藏版
Free SSH and telnet client putty
日期 数据库日期 字符串 之间互相转换
Altium Designer 19 器件丝印标号位置批量统一摆放
matlab GUI界面仿真直流电机和交流电机转速仿真
Quick personal site building guide using WordPress