当前位置:网站首页>Scala language learning-04-function passed in as parameter function as return value
Scala language learning-04-function passed in as parameter function as return value
2022-06-22 15:45:00 【Sunshine clover lxgzxj】
One 、 Exercise questions
// practice 1: Function is passed in as a parameter
//func Implement incoming Int Return function f1
//f1 The implementation passes in a string return function f2
//f2 The implementation passes in a character and returns bool type
// Ask to execute func(0)("")('0') return false, Other situations return to true
// practice 2: Function as return value
//ArrayElementOp Realization :
// The first parameter is passed in Int Type array
// The second parameter is passed into the function ElementAddVal
// Return to one Int Type array
//ElementAddVal Realization :
// Pass in two int Type parameter
// Returns the addition of two numbers , The type is int
// Test array :(1, 2, 3, 4, 5, 6)
// Return results :(2, 4, 6, 8, 10, 12)
Two 、 Test code
object TestFuncParam {
def main(args: Array[String]): Unit = {
val TempArray: Array[Int] = Array(1, 2, 3, 4, 5, 6)
val NewArray: Array[Int] = ArrayElementOp(TempArray, ElementAddVal)
println(NewArray.mkString(" | "))
println(func(0)("")('0'))
println(func(1)("")('0'))
}
// practice : Function as parameter
def ArrayElementOp(ParamArray: Array[Int], Func: (Int,Int) => Int): Array[Int] = {
for(i <- ParamArray) yield Func(i,i)
}
def ElementAddVal(Param1: Int, Param2: Int): Int = {
Param1 + Param2
}
// practice : Function as return value
def func(Param1: Int): String => (Char => Boolean) = {
def f1(Param2: String): Char => Boolean = {
def f2(Param3: Char): Boolean = {
if(Param1 == 0 && Param2 == "" && Param3 == '0'){
false
}
else {
true
}
}
f2
}
f1
}
}
3、 ... and 、 test result
[[email protected] test]# scala TestFuncParam
2 | 4 | 6 | 8 | 10 | 12
false
true
边栏推荐
- 排序之归并排序
- Using virtual serial port to debug serial port in keil MDK
- ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
- keil MDK 中使用虚拟串口调试串口
- 阿里云中间件的开源往事
- "Forget to learn again" shell process control - 38. Introduction to while loop and until loop
- 壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代
- 宏源期货开户安全么?宏源期货公司可以降低手续费?
- TDengine 连接器上线 Google Data Studio 应用商店
- Verilog使用inout信号的方法
猜你喜欢

DevSecOps: CI/CD 流水线安全的最佳实践

接了个私活项目,一下赚了15250,还有必要做主业吗?

【newman】postman生成漂亮的测试报告

Recommendation of individual visa free payment scheme

HMS Core新闻行业解决方案:让技术加上人文的温度

Community article | mosn building subset optimization ideas sharing

壹连科技冲刺深交所:年营收14亿 65%收入来自宁德时代

mysql如何将字段修改为not null

快速玩转CI/CD图形化编排

How MySQL modifies a field to not null
随机推荐
Verilog使用inout信号的方法
How to open an account in flush? Is online account opening safe?
Tdengine connector goes online Google Data Studio store
Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
Common operations in Visual Studio development
mysql的concat()函数如何用
Hello, big guys. Error reporting when using MySQL CDC for the first time
推进兼容适配,使能协同发展 GBase 5月适配速递
[Newman] postman generates beautiful test reports
好风凭借力 – 使用Babelfish 加速迁移 SQL Server 的代码转换实践
C语言学生成绩排名系统
润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东
Once, I had 5 part-time jobs just to buy a new earring for my girlfriend
【一起上水硕系列】Day Three - video
天安科技IPO被终止:曾拟募资3.5亿 复星与九鼎是股东
New load balancing webclient CRUD
高精度计算
FreeRtos 任务优先级和中断优先级
NF RESNET: network signal analysis worth reading after removing BN normalization | ICLR 2021
乱解码nlp