当前位置:网站首页>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+y
So 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 + y
So 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+y
So 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
边栏推荐
猜你喜欢
KubeSphere 集群配置 NFS 存储解决方案-收藏版
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
分数阶PID控制
C Primer Plus Chapter 11_ Strings and string functions_ Codes and exercises
[email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT"/>
NoViableAltException([email protected][2389:1: columnNameTypeOrConstraint : ( ( tableConstraint ) | ( columnNameT
Gaussian distribution, linear regression, logistic regression
Yaml file encryption
免费的 SSH 和 Telnet 客户端PuTTY
Using CSDN to develop cloud and build navigation websites
Ahb2apb bridge design (2) -- Introduction to synchronous bridge design
随机推荐
Change the status to the corresponding text during MySQL query
Fast implementation of thread mesh networking
Cloud-Native Database Systems at Alibaba: Opportunities and Challenges
研究生数学建模竞赛-无人机在抢险救灾中的优化应用
427-二叉树(617.合并二叉树、700.二叉搜索树中的搜索、98. 验证二叉搜索树、530.二叉搜索树的最小绝对差)
Multithreading basic Part3
Download CUDA and cudnn
EasyExcel:读取Excel数据到List集合中
Assembly language - Wang Shuang Chapter 9 Principles of transfer instructions - Notes
Kubesphere cluster configuration NFS storage solution - favorite
Assembly language - Wang Shuang Chapter 13 int instruction - Notes
网关状态检测 echo request/reply
Gaussian distribution, linear regression, logistic regression
Sqlsever 字段相乘后保留2位小数
Proxy-Reflect使用详解
《汇编语言-王爽》第3章笔记及实验
LeetCode 0086.分隔链表
Fast realization of Bluetooth communication between MCU and mobile phone
Ahb2apb bridge design (2) -- Introduction to synchronous bridge design
爬虫学习5---反反爬之识别图片验证码(ddddocr和pytesseract实测效果)