当前位置:网站首页>Scala语言学习-04-函数作为参数传入函数-函数作为返回值
Scala语言学习-04-函数作为参数传入函数-函数作为返回值
2022-06-22 14:28:00 【阳光九叶草LXGZXJ】
一、练习题目
//练习1:函数作为参数传入函数
//func实现传入Int返回函数f1
//f1实现传入一个字符串返回函数f2
//f2实现传入一个字符返回bool类型
//要求执行func(0)("")('0')返回false,其他情况返回true
//练习2:函数作为返回值
//ArrayElementOp实现:
//第一个参数传入Int类型数组
//第二个参数传入函数ElementAddVal
//返回一个Int类型数组
//ElementAddVal实现:
//传入两个int类型参数
//返回两数相加,类型为int
//测试数组:(1, 2, 3, 4, 5, 6)
//返回结果:(2, 4, 6, 8, 10, 12)
二、测试代码
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'))
}
//练习:函数作为参数
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
}
//练习:函数作为返回值
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
}
}
三、测试结果
[[email protected] test]# scala TestFuncParam
2 | 4 | 6 | 8 | 10 | 12
false
true
边栏推荐
- The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
- U++ programming array learning notes
- 类似attention nlp
- 极致效率,云原生数据库TDSQL-C安身立命的根本
- Sword finger offer46 -- translate numbers into strings
- 基础版现在SQL分析查询不能用了吗?
- Common operations in Visual Studio development
- keil MDK 中使用虚拟串口调试串口
- FPGA collects DHT11 temperature and humidity
- How to use the concat() function of MySQL
猜你喜欢

关于 GIN 的路由树

“软件定义世界,开源共筑未来” 2022开放原子全球开源峰会7月底即将开启

得物App数据模拟平台的探索和实践

数据库连接池:压力测试

Tdengine connector goes online Google Data Studio store

封装api时候token的处理

Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe

时隔17年,刘亦菲再次刷屏式爆红:普通人不想被淘汰,也要懂得这件事

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

PHP built-in protocols (supported and encapsulated protocols)
随机推荐
Yilian technology rushes to Shenzhen Stock Exchange: annual revenue of RMB 1.4 billion, 65% of which comes from Ningde times
数据资产管理:数据发现,发现什么,怎么发现?
mysql如何将字段修改为not null
U++ iterative Sorting Query learning notes
Good wind relies on strength – code conversion practice of accelerating SQL Server Migration with babelfish
蓝桥杯2019年国赛最长子序列
OOP 多重收纳(类模板)
How to open an account in flush? Is online account opening safe?
ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程
又可以这样搞nlp(分类)
I took a private job and earned 15250. Is it still necessary to do my main business?
PowerPoint 教程,如何在 PowerPoint 中添加水印?
还可以这样搞nlp(分类)
社区文章|MOSN 构建 Subset 优化思路分享
那些没考上大学的人,后来过的怎样
New hybrid architecture iformer! Flexible migration of convolution and maximum pooling to transformer
Method of using inout signal in Verilog
乱解码nlp
Ultimate efficiency is the foundation for the cloud native database tdsql-c to settle down
Meet webassembly again