标签:编译 实例 就是 one 通过 return 代码 最简 imp
scala> def sayHello(name:String){println("Hello, "+name)}
sayHello: (name: String)Unit
scala> sayHello("tom")
Hello, tom
scala> val sayHelloFunc = sayHello _
sayHelloFunc: String => Unit = $$Lambda$1061/1964697764@235d659c
scala> sayHelloFunc("leo")
Hello, leo
scala> val sayHelloFunc = (name:String) => println("hello, "+name)
sayHelloFunc: String => Unit = $$Lambda$1070/547507935@29962b2f
scala> sayHelloFunc("leo")
hello, leo
scala> val sayHelloFunc = (name:String)=>println("Hello, "+name)
sayHelloFunc: String => Unit = $$Lambda$1167/1510403823@1c0cf193
scala> def greeting(func:(String)=> Unit,name:String){func(name)}
greeting: (func: String => Unit, name: String)Unit
scala> greeting(sayHelloFunc,"leo")
Hello, leo
scala> Array(1,2,3,4,5).map((num:Int)=>num*num)
res5: Array[Int] = Array(1, 4, 9, 16, 25)
scala> def getGreetingFunc(msg:String)=(name:String)=>println(msg+","+name)
getGreetingFunc: (msg: String)String => Unit
scala> val greetingFunc = getGreetingFunc("hello")
greetingFunc: String => Unit = $$Lambda$1292/1385099824@ae85aad
scala> greetingFunc("leo")
hello,leo
scala> def greeting(func:(String)=>Unit,name:String){func(name)}
greeting: (func: String => Unit, name: String)Unit
scala> greeting((name:String)=>println("Hello, "+name),"leo")
Hello, leo
scala> greeting((name)=>println("Hello, "+name),"leo")
Hello, leo
scala> greeting(name => println("Hello, "+name),"leo")
Hello, leo
scala> def triple(func:(Int)=>Int) = {func(3)}
triple: (func: Int => Int)Int
scala> triple(3* _)
res10: Int = 9
scala> triple(2+_)
res11: Int = 5
scala> Array(1,2,3,4,5).map(2* _)
res13: Array[Int] = Array(2, 4, 6, 8, 10)
scala> (1 to 9).map("*"*_).foreach(println _)
*
**
***
****
*****
******
*******
********
*********
scala> (1 to 20).filter(_ % 2 == 0)
res16: scala.collection.immutable.IndexedSeq[Int] = Vector(2, 4, 6, 8, 10, 12, 14, 16, 18, 20)
//从左侧元素开始,进行reduce操作,即先对元素1和元素2进行处理,然后将结果与元素3处理,再将结果与元素4处理,依次类推,即为reduce
// 1*2*3*4*5*6*7*8*9
scala> (1 to 9).reduceLeft(_*_)
res17: Int = 362880
//sortWith:对元素进行两两相比,进行排序
scala> Array(3,2,5,4,10,1).sortWith(_ < _)
res19: Array[Int] = Array(1, 2, 3, 4, 5, 10)
scala> def getGreetingFunc(msg:String) = (name:String)=>println(msg+","+name)
getGreetingFunc: (msg: String)String => Unit
scala> val greetingFuncHello = getGreetingFunc("hello")
greetingFuncHello: String => Unit = $$Lambda$1377/116345573@1f238bc9
scala> val greetingFuncHi = getGreetingFunc("hi")
greetingFuncHi: String => Unit = $$Lambda$1377/116345573@3f33316e
scala> greetingFuncHello("leo")
hello,leo
scala> greetingFuncHi("leo")
hi,leo
scala> import javax.swing._
import javax.swing._
scala> import java.awt.event._
import java.awt.event._
scala> val button = new JButton("Click")
scala> button.addActionListener(new ActionListener{
| override def actionPerformed(event:ActionEvent){
| println("Click Me!!")
| }
| })
//隐式转换
scala> implicit def getActionListener(actionProcessFunc:(ActionEvent)=>Unit)=new ActionListener{
| override def actionPerformed(event:ActionEvent){
| actionProcessFunc(event)
| }
| }
scala> button.addActionListener((event:ActionEvent)=>println("Click Me!!!"))
scala> def sum(a:Int,b:Int) = a+b
sum: (a: Int, b: Int)Int
scala> sum(1,2)
res24: Int = 3
scala> def sum2(a:Int) = (b:Int)=>a+b
sum2: (a: Int)Int => Int
scala> sum2(1)(2)
res25: Int = 3
scala> def sum3(a:Int)(b:Int) = a+b
sum3: (a: Int)(b: Int)Int
scala> sum3(1)(2)
res26: Int = 3
scala> :paste
// Entering paste mode (ctrl-D to finish)
def greeting(name:String)={
def sayHello(name:String):String={
return "Hello, "+name
}
sayHello(name)
}
// Exiting paste mode, now interpreting.
greeting: (name: String)String
scala> greeting("leo")
res27: String = Hello, leo
标签:编译 实例 就是 one 通过 return 代码 最简 imp
原文地址:https://www.cnblogs.com/sky-chen/p/10105445.html