feat:ChatGPT第 3 课:函数对象、匿名函数、=>、map

This commit is contained in:
2026-09-13 16:11:11 +08:00
parent d08ecade11
commit d589eeaf30
15 changed files with 292 additions and 0 deletions
@@ -0,0 +1,32 @@
package practise3.comprehensive
/*综合题 ⭐
给你:
val scores = List(55, 60, 72, 85, 91, 96)
完成:
第一步
使用 filter:
只保留 >= 60
得到:
List(60, 72, 85, 91, 96)
第二步
使用 map:
每个成绩 + 5
得到:
List(65, 77, 90, 96, 101)
也就是说最终写出类似:
val result = scores
.filter(...)
.map(...)
println(result)*/
object Comprehensive {
def main(args: Array[String]): Unit = {
val scores = List(55, 60, 72, 85, 91, 96)
val result = scores
.filter(x => x >= 60)
.map(x => x + 5)
println(result)
}
}
@@ -0,0 +1,13 @@
package practise3.demo
/*
def 方法 vs => 函数值
*/
object Demo1DefAndFunction {
def main(args: Array[String]): Unit = {
def DefAdd(a: Int, b: Int): Int = a + b //这是一个方法
val ValAdd = (a: Int, b: Int) => a + b //这是一个函数值
println(DefAdd(10, 20))
println(ValAdd(10, 20))
}
}
@@ -0,0 +1,11 @@
package practise3.demo
/*
* => 匿名函数语法
* */
object Demo2FunctionLiteral {
def main(args: Array[String]): Unit = {
val f = (a: Int) => a * 2 //本质是输入一个数字 ➡️ 乘以 2 ➡️ 得到结果
println(f(10))
}
}
@@ -0,0 +1,13 @@
package practise3.demo
/*
* 函数赋值给变量
* */
object Demo3FunctionVariable {
def main(args: Array[String]): Unit = {
val age = 20 // 普通变量
val name = "Tom" // 字符串
val double = (x: Int) => x * 2 // 函数也可以
println(double(5)) // 此时double不是一个数字,而是一个“函数”。
}
}
@@ -0,0 +1,16 @@
package practise3.demo
/*
* 函数类型 Int => Int
* */
object Demo4FunctionType {
def main(args: Array[String]): Unit = {
val double = (x: Int) => x * 2 // 这个函数可以理解成Int => Int
val double2: Int => Int = (x: Int) => x * 2 // 接收一个 Int,返回一个 Int。
val double3: Int => Int = x => x * 2 // 再简化
println(double(2))
println(double2(2))
println(double3(2))
}
}
@@ -0,0 +1,11 @@
package practise3.demo
/*
* 多参数函数
* */
object Demo5MultiParameterFunction {
def main(args: Array[String]): Unit = {
val add = (a: Int, b: Int) => a + b // 它的函数类型是(Int, Int) => Int
println(add(10, 20))
}
}
@@ -0,0 +1,36 @@
package practise3.demo
/*
* map 基本使用
* */
object Demo6Map {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3, 4, 5)
/*
* 普通 Java 思维可能是:
* 遍历 List
*
* 取出一个元素
* ⬇️
* 乘 2
* ⬇️
* 放到新 List
* */
val result = nums.map(x => x * 2) // scala直接,对 nums 中的每一个元素,都执行一次 x => x * 2
/*
* nums
* ⬇
* map
* ⬇
* 给 map 一个函数
* ⬇
* x => x * 2
*/
val result2 = nums.map(x=>x)
val result3 = (x:Int) => x * 2
println(result)
println(result2)
println(result3(2))
}
}
@@ -0,0 +1,16 @@
package practise3.demo
/*
* 匿名函数与函数变量
* */
object Demo7FunctionAsParameter {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3, 4, 5)
val result =nums.map(x => x * 2)
val double = (x: Int) => x * 2
val result2 = nums.map(double)
println(result)
println(result2)
}
}
@@ -0,0 +1,26 @@
package practise3.demo
/*
* filter 基本使用
* */
object Demo8Filter {
def main(args: Array[String]): Unit = {
/*
* map 是“每一个都处理”。
* 那么我只想留下符合条件的数据怎么办?
*
*/
val nums = List(1, 2, 3, 4, 5)
val result = nums.filter(x => x >= 3)
println(result)
/*
* 1 → false → 丢掉
* 2 → false → 丢掉
* 3 → true → 保留
* 4 → true → 保留
* 5 → true → 保留
*/
}
}
@@ -0,0 +1,14 @@
package practise3.demo
/*
* map 与 filter 对比
* */
object Demo9MapAndFilter {
def main(args: Array[String]): Unit = {
println(List(1, 2, 3).map(x => x * 10)) // List(10, 20, 30)
println(List(1, 2, 3).filter(x => x >= 2)) // List(2, 3)
/*
* map → 改造元素
*filter → 筛选元素
*/
}
}
@@ -0,0 +1,28 @@
package practise3.exercises
/*
* 练习 1:创建一个函数
创建:
double
要求:
输入 10
输出 20
也就是:
println(double(10))
得到:
20
要求使用:
=>
而不是 def。
*/
object Exercises1 {
def main(args: Array[String]): Unit = {
val double = (x:Int) => x * 2
println(double(10))
}
}
@@ -0,0 +1,21 @@
package practise3.exercises
/*练习 2:创建平方函数
创建:
square
要求:
println(square(5))
得到:
25
要求写成:
val square = ...*/
object Exercises2 {
def main(args: Array[String]): Unit = {
val square = (x: Int) => x * x
print(square(5))
}
}
@@ -0,0 +1,21 @@
package practise3.exercises
/*练习 3:两个参数的函数
创建:
add
要求:
println(add(10, 20))
输出:
30
函数类型最好也写出来:
val add: (Int, Int) => Int = ...*/
object Exercises3 {
def main(args: Array[String]): Unit = {
val add = (x: Int, y: Int) => x + y
println(add(10, 20))
}
}
@@ -0,0 +1,15 @@
package practise3.exercises
/*练习 4:使用 map
给你
val nums = List(1, 2, 3, 4, 5)
使用 map,得到:
List(2, 4, 6, 8, 10)*/
object Exercises4 {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3, 4, 5)
val result = nums.map(x => x*2)
println(result)
}
}
@@ -0,0 +1,19 @@
package practise3.exercises
/*练习 5:使用 filter
给你:
val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
使用 filter,只保留偶数:
List(2, 4, 6, 8, 10)
提示:
x % 2 == 0*/
object Exercises5 {
def main(args: Array[String]): Unit = {
val nums = List(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val result = nums.filter(x => x % 2 == 0)
println(result)
}
}