/**
重点:学习scala函数的定义和使用
知识点
1.def 函数名(参数列表):函数的返回值类型={方法体}
2.scala的函数可以根据方法体的返回值,自动推断出函数的返回值类型
3.注意:如果要用scala的自动推断机制,必须有=号。
如果没有=号,则函数的返回值类型一律为Unit 空类型
**/
object Demo07 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
def f1():String={
"hello 1810"
} //> f1: ()String
def f2():Int={
100
} //> f2: ()Int
def f3()={
100
} //> f3: ()Int
def f4()={
Array(1,2,3,4)
} //> f4: ()Array[Int]
def f5(){
100
} //> f5: ()Unit
def f6(a:Int,b:Int)={
a+b
} //> f6: (a: Int, b: Int)Int
f6(2,3) //> res0: Int = 5
def f7(a:String,sep:String)={
a.split(sep)
} //> f7: (a: String, sep: String)Array[String]
f7("hello,world",",") //> res1: Array[String] = Array(hello, world)
//--要求编写函数 f8。接收一个Array(1,2,3,4),遍历数组并打印
def f8(a:Array[Int])={
for(i<-a)println(i)
} //> f8: (a: Array[Int])Unit
f8(Array(1,2,3,4)) //> 1
//| 2
//| 3
//| 4
//--scala的默认参数机制,如果调用函数时,没有传参,则用默认值
def f9(a:String,b:String="[",c:String="]")={
b+a+c
} //> f9: (a: String, b: String, c: String)String
f9("hello") //> res2: String = [hello]
f9("hello","|","|") //> res3: String = |hello|
//--scala的变长参数机制,等价于java的可变参数。
//--变长参数本质上是一种集合类型,
//--注意:变长参数必须位于参数列表最后
def f10(a:Int*)={
for(i<-a)println(i)
} //> f10: (a: Int*)Unit
f10(1,2,3) //> 1
//| 2
//| 3
val p1=new Person //> p1 : Person = Person@3498ed
p1.eat() //> eat food
}
/**
最重点的:学习匿名函数和高阶函数
知识点
1.匿名函数没有函数名
2.匿名函数可以当做参数进行赋值 和传递
3.高阶函数的特点是:把函数作为参数进行传递
**/
object Demo08 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--声明一个普通函数
def f1(a:Int,b:Int)={a+b} //> f1: (a: Int, b: Int)Int
//--声明了一个匿名函数
//--把匿名参数当做参数赋值
val f2=(a:Int,b:Int)=>{a+b} //> f2 : (Int, Int) => Int = <function2>
f2(2,3) //> res0: Int = 5
//--定义了一个高阶函数,高阶函数的特点是:把函数作为参数进行传递
//--由函数式编程引入的机制
def f3(a:Int,b:Int,f:(Int,Int)=>Int)={
f(a,b)
} //> f3: (a: Int, b: Int, f: (Int, Int) => Int)Int
f3(2,3,(a:Int,b:Int)=>{a*b}) //> res1: Int = 6
def f4(a:String,b:String,f:(String,String)=>String)={
f(a,b)
} //> f4: (a: String, b: String, f: (String, String) => String)String
f4("hello","world",(a:String,b:String)=>{a+b}) //> res2: String = helloworld
def f5(a:String,b:String,f:(String,String)=>Array[String])={
f(a,b)
} //> f5: (a: String, b: String, f: (String, String) => Array[String])Array[String
//| ]
f5("hello,world",",",(a:String,b:String)=>{a.split(b)})
//> res3: Array[String] = Array(hello, world)
}
/**
针对匿名函数和高阶函数的补充
知识点 通用的化简规则
1.如果匿名函数参数的类型可以推断出来,则参数类型可以省略
2.如果匿名函数的参数列表只有一个,则()可以省略
3.最终极的化简:可以通过_(占位符)来代替参数
**/
object Demo02 {
println("Welcome to the Scala worksheet") //> Welcome to the Scala worksheet
//--声明一个匿名函数
val addFunc=(a:Int,b:Int)=>{a+b} //> addFunc : (Int, Int) => Int = <function2>
def f1(a:Int,b:Int,f:(Int,Int)=>Int)={
f(a,b)
} //> f1: (a: Int, b: Int, f: (Int, Int) => Int)Int
f1(2,3,(a:Int,b:Int)=>{a+b}) //> res0: Int = 5
f1(2,3,addFunc) //> res1: Int = 5
f1(2,3,(a,b)=>{a+b}) //> res2: Int = 5
f1(2,3,(a,b)=>a+b) //> res3: Int = 5
f1(2,3,_+_) //> res4: Int = 5
def f2(a:String,f:(String)=>Array[String])={
f(a)
} //> f2: (a: String, f: String => Array[String])Array[String]
f2("hello,1810",(a:String)=>{a.split(",")}) //> res5: Array[String] = Array(hello, 1810)
f2("hello,1810",a=>a.split(",")) //> res6: Array[String] = Array(hello, 1810)
f2("hello,1810",_.split(",")) //> res7: Array[String] = Array(hello, 1810)
val a1=Array(1,2,3,4) //> a1 : Array[Int] = Array(1, 2, 3, 4)
a1.foreach { (x:Int) =>{println(x)}} //> 1
//| 2
//| 3
//| 4
a1.foreach { x =>println(x)} //> 1
//| 2
//| 3
//| 4
a1.foreach { println(_)} //> 1
//| 2
//| 3
//| 4
a1.filter { (x:Int)=>{x%2==0} } //> res8: Array[Int] = Array(2, 4)
a1.filter { x=>x%2==0 } //> res9: Array[Int] = Array(2, 4)
a1.filter { _%2==0 } //> res10: Array[Int] = Array(2, 4)
}
Scala函数
最新推荐文章于 2024-07-15 18:30:19 发布