当前位置:网站首页>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
边栏推荐
- What are the five characteristics of network security? What are the five attributes?
- 推荐几个AI智能平台
- Is pioneer futures reliable? How to open a futures account safely?
- 极致效率,云原生数据库TDSQL-C安身立命的根本
- On the routing tree of gin
- I took a private job and earned 15250. Is it still necessary to do my main business?
- Mobile learning notes of u++ programming
- Fast and accurate point cloud registration based on minimizing 3D NDT distance
- Is SQL analysis query unavailable in the basic version?
- Exploration and practice of dewu app data simulation platform
猜你喜欢

Meet webassembly again

Recommendation of individual visa free payment scheme

华为机器学习服务银行卡识别功能,一键实现银行卡识别与绑定

ROS2前置基础教程 | 小鱼教你用CMake依赖查找流程

FPGA采集DHT11温湿度

ML笔记-matrix fundamental, Gradient Descent

全新混合架构iFormer!将卷积和最大池化灵活移植到Transformer

加密市场进入寒冬,是“天灾”还是“人祸”?

Are there many unemployed people in 2022? Is it particularly difficult to find a job this year?

Ros2 pre basic tutorial | using cmakelists Txt compile ros2 node
随机推荐
Please, don't be brainwashed. This is the living reality of 90% of Chinese people
ML笔记-matrix fundamental, Gradient Descent
小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效
鸿世电器冲刺创业板:年营收6亿 刘金贤股权曾被广德小贷冻结
Tree structured binary tree
求求了,别被洗脑了,这才是90%中国人的生存实况
模板特例化 template<>
TDengine 连接器上线 Google Data Studio 应用商店
Once, I had 5 part-time jobs just to buy a new earring for my girlfriend
The IPO of Tian'an technology was terminated: Fosun and Jiuding were shareholders who planned to raise 350million yuan
三菱机械臂demo程序
Wallys/DR7915-wifi6-MT7915-MT7975-2T2R-support-OpenWRT-802.11AX-supporting-MiniPCIe
All famous network platforms in the world
Is the encryption market a "natural disaster" or a "man-made disaster" in the cold winter?
推荐几个AI智能平台
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
How to open an account in flush? Is online account opening safe?
『忘了再学』Shell流程控制 — 38、while循环和until循环介绍
Simulation Keil et vspd
数据资产管理:数据发现,发现什么,怎么发现?