一、Scala 集合元素的映射 (重点)
1. 高阶函数
- 可以接受一个函数的函数就叫高级函数
package com.yx.demo.scala.chapter08
/*
@author HarkerYX
@create 2020-12-28-16:53
*/ object HighOrderFun1 {
def main(args: Array[String]): Unit = {
//使用高阶函数
val res = test(sum2 _, 3.5)
println("res=" + res) // res=7.0
//在scala中,可以把一个函数直接赋给一个变量,但是不执行函数
val f1 = myPrint _
f1() //执行
}
def myPrint(): Unit = {
println("hello,world!")
}
//说明
//1. test就是一个高阶函数
//2. f: Double => Double 表示一个函数, 该函数可以接受一个Double,返回Double
//3. n1: Double 普通参数
//4. f(n1) 在test函数中,执行 你传入的函数
def test(f: Double => Double, n1: Double) = {
f(n1)
}
//普通的函数, 可以接受一个Double,返回Double
def s