-
求值策略 Call By Value Call By Name查看全部
-
匿名函數(shù) (參數(shù)列表) => {函數(shù)題};查看全部
-
不變性:為了獲得引用透明性,任務(wù)值都不能變化查看全部
-
Mark查看全部
-
map例子查看全部
-
例 子查看全部
-
綜合例子查看全部
-
求值策略的例子查看全部
-
求值策略的例子查看全部
-
val a = List(1, 2, 3, 4) val b = 0::a 表示0添加到a的頭部,并賦給b val c = "x"::"y"::"z" val d = a:::c 表示列表a連接列表b a.isEmpty 判斷列表是否為空 a.head 表示顯示列表a第一個(gè)元素 a.tail 表示顯示列表a除了第一個(gè)元素,以外的所有元素 def walkthru(l: List[Int]): String = { if(l.isEmpty) "" else l.head.toString + " " + walkthru(l.tail) } walkthru(a)查看全部
-
遞歸: 遞歸函數(shù)(Recurisive Function)在函數(shù)式編程中是實(shí)現(xiàn)循環(huán)的一種技術(shù) 例子: def factorial(n: Int): Int = { if (n <= 0) 1 else n* factorial(n - 1) } 當(dāng)遞歸層數(shù)過多時(shí),堆棧會(huì)溢出 尾遞歸函數(shù): @annotation.tailrec //這句話告訴scala,需要進(jìn)行尾遞歸優(yōu)化 def factorial(n: Int, m: Int):Int = if (n <= 0) m else factorial(n -1, n*m) factorial(5, 1)查看全部
-
科里化: 科里化函數(shù)(Curried Function)把具有多個(gè)參數(shù)的函數(shù)轉(zhuǎn)換為一條函數(shù)鏈,每個(gè)節(jié)點(diǎn)上是單一參數(shù)。 def add(x: Int, y: Int) = x + y //非科里化語法 def add(x: Int)(y: Int) = x + y //科里化語法 def curriedAdd(a: Int)(b: Int) = a + b curriedAdd(2)(6) def addOne = curriedAdd(1)_ //Int => Int addOne(2) //3查看全部
-
Scala語言支持: 把函數(shù)作為實(shí)參傳給另一個(gè)函數(shù) 把函數(shù)作為返回值 把函數(shù)賦值給變量 把函數(shù)存儲(chǔ)在數(shù)據(jù)結(jié)構(gòu)中 函數(shù)類型: A => B Int => String /*把一個(gè)Int類型的參數(shù),通過一系列的函數(shù)映射,轉(zhuǎn)換為一個(gè)字符串 類型的值返回*/ 高階函數(shù): 用函數(shù)作為形參或返回值的函數(shù) 匿名函數(shù): 在Scala里,匿名函數(shù)的定義格式為: (形參列表) => {函數(shù)體}查看全部
-
代碼塊Block: {exp1;exp2} { exp1 exp2 } 函數(shù)(多個(gè)輸入?yún)?shù)用逗號(hào)分隔): def functionName(param: ParamType): ReturnType = { //function block }查看全部
-
_是個(gè)通配符查看全部
舉報(bào)
0/150
提交
取消