? 1? if表達式 ? ? ?Scala中if...else..表達式是有返回值的,如果if和else返回值類型不一樣,則返回Any類型。 scala> val a3=10
a3: Int = 10scala> val a4=
| if(a3>20){
| "a3大于a4"
| }else{
| "a4大于a3"
| }
a4: String = a4大于a3scala> val a5=
| if(a3>20)"a3大于20"
a5: Any = ()scala> println(a5)
()
2? while表達式 scala> def gcdLoop(x:Long,y:Long):Long={| var a=x| var b=y| while(a!=0){| val temp=a| a=b%a| b=temp| }| b| }
gcdLoop: (x: Long, y: Long)Longscala> gcdLoop(8,9)
res0: Long = 1scala> gcdLoop(8,4)
res1: Long = 4
import scala.util.control.Breaks
object whilexample {def main(args:Array[String]): Unit ={var n=1;val loop=new Breaksloop.breakable{while(n<=20){n+=1;if(n==19){loop.break()}}}println(n)}
}
結果如下:
?
3? for表達式 scala> for(i <- 1 to 3; j <- 1 to 3){| print(i * j + " ")| }
1 2 3 2 4 6 3 6 9
scala> for(i <- 1 until 3; j <- 1 until 3) {| print(i * j + " ")| }
1 2 2 4
scala> for(i <- 1 to 3 if i != 2) {| print(i+" ")| }
1 3
scala>
scala> for(i <- 1 to 3; j = 4 - i) {| print(j+" ")| }
3 2 1
使用yield關鍵字,將遍歷過程處理結果返回一個值。 scala> val for5 = for(i <- 1 to 10) yield i
for5: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)scala> print(for5)
Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
scala> val for5 = for(i <- 1 to 10)| print(for5)
<console>:13: error: recursive value for5 needs typeprint(for5)^
過濾,通過for中的() 添加過濾器(filter),就是if子句。 object hello{def main(args:Array[String]){ val filesHere=(new java.io.File(".")).listFilesfor(file<-filesHere if(file.getName.endsWith(".scala")))println(file)}}D:\>scala hello.scala
.\a.scala
.\h.scala
.\hello.scala
.\s.scala
4? try表達式 scala> def ha(n:Int){| val half=if(n%2==0) print(n/2)| else| throw new RuntimeException("n num be even")| }
ha: (n: Int)Unitscala> ha(7)
java.lang.RuntimeException: n num be evenat .ha(<console>:14)... 28 elidedscala> ha(8)
4
捕獲異常的語法選擇catch子句的原因是與模式匹配保持一致。
object ExceptionSyllabus {def divider(x: Int, y: Int): Float= {if(y == 0) throw new Exception("0作為了除數")else x / y}def main(args: Array[String]): Unit = {try {println(divider(10, 3))} catch {case ex: Exception => println("捕獲了異常:" + ex)} finally {}}}D:\>scala ExceptionSyllabus.scala
3.0//當數字改為(10,0)后
D:\>scala ExceptionSyllabus.scala
捕獲了異常:java.lang.Exception: 0作為了除數
5? 匹配表達式 ?Scala中的match表達式類似于其它語言的switch語句,它可用提供多個備選項做選擇。 object frist{def main(args:Array[String]){ val firstArg=if(args.length>0)args(0) else ""firstArg match{case "salt"=>println("papper")case "chips"=>println("salsa")case "eggs"=>println("bacon")case _=>println("huh?")}}}D:\>scala frist.scala
huh?D:\>scala frist.scala salt
papper
?6? lazy懶值 當val被聲明為lazy時,初始化將被推遲,直到我們首次對此取值,適用于初始化開銷較大的場景。?通過lazy關鍵字的使用與否,來觀察執行過程 object Lazy {def init(): String = {println("init方法執行")"嘿嘿嘿,我來了~"}def main(args: Array[String]): Unit = {lazy val msg = init()println("lazy方法沒有執行")println(msg)}}
結果:
object Lazy {def init(): String = {println("init方法執行")"嘿嘿嘿,我來了~"}def main(args: Array[String]): Unit = {val msg = init()println("lazy方法沒有執行")println(msg)}}
結果:
?
7 柯里化
在函數編程中,將接受多個參數的函數轉化為接受單個參數的函數。這一過程稱為柯里化
scala> def mul(x: Int, y: Int) = x * y
mul: (x: Int, y: Int)Intscala> println(mul(10, 10))
100scala> def mulCurry(x: Int) = (y: Int) => x * y
mulCurry: (x: Int)Int => Intscala> println(mulCurry(10)(9))
90scala> def mulCurry2(x: Int)(y:Int) = x * y
mulCurry2: (x: Int)(y: Int)Intscala> println(mulCurry2(10)(8))
80
柯里化的應用:在忽略大小寫 的情況下,計較是否相等。 scala> val a = Array("Hello", "World")
a: Array[String] = Array(Hello, World)scala> val b = Array("hello", "world")
b: Array[String] = Array(hello, world)scala> println(a.corresponds(b)(_.equalsIgnoreCase(_)))
true
?
轉載于:https://www.cnblogs.com/0205gt/p/10983211.html
總結
以上是生活随笔 為你收集整理的Scala实践6 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。