## 2016年5月17日 星期二

### [Scala 小學堂] Scala Gossic : 了解更多 - 定義函式 (一級函式)

Scala 是一個可直譯、編譯、靜態、運行於 JVM 之上、可與 Java 互操作、融合物件導向編程特性與函式編程風格的程式語言. Scala 本身具有擴充性，不過這必須了解更多語法特性與細節.

1. def max(m: Int, n: Int) = if(m > n) m else n

1. val max = (m: Int, n: Int) => if(m > n) m else n

val max: (Int, Int) => Int = (m: Int, n: Int) => if(m > n) m else n

- FirstFunc1.scala 代碼 :
1. def selection(number: Array[Int], order: (Int, Int) => Boolean) {
2.         def mm(m: Int, j: Int):Int= {
3.                 if(j==number.length) m
4.                 else if(order(number(j), number(m))) mm(j, j+1)
5.                 else mm(m, j+1)
6.         }
7.         for(i <- span="">0 until number.length-1) {
8.                 val m = mm(i, i+1)
9.                 if(i!=m)
10.                 swap(number, i, m)
11.         }
12. }
13.
14. def swap(number: Array[Int], i:Int, j: Int) {
15.         val t = number(i)
16.         number(i) = number(j)
17.         number(j) = t
18. }

1. val arr1 = Array(25178)
2. selection(arr1, (a: Int, b: Int) => a < b)
3. println(arr1.mkString(","))

1. val arr2 = Array(25178)
2. selection(arr2, (a: Int, b: Int) => a > b)
3. println(arr2.mkString(","))

1. val arr2 = Array(25178)
2. selection(arr2, (a, b) => a > b)
3. println(arr2.mkString(","))

1. val arr2 = Array(25178)
2. selection(arr2, (_: Int) > (_: Int))
3. println(arr2.mkString(","))

1. val arr2 = Array(25178)
2. selection(arr2, _ > _)
3. println(arr2.mkString(","))

1. val f = (_: Int) + (_: Int)    // 這樣 OK
2. println(f(12))               // 顯示 3

val f = _ + _ // 錯誤 missing parameter type for expanded function

Array("a", "b", "c").foreach(x => print(x.toUpperCase)) // 顯示 ABC

Array(4, 8, 1, 6, 3, 7, 9).filter(_ > 5).foreach(print(_)) // 顯示 8、6、7、9

1. val max = (m: Int, n: Int) => {
2.               if(m > n)
3.                  m
4.               else n
5.           }

1. val max = (m: Int, n: Int) => if(m > n) m else n
2. println(max(1020))          // 顯示 20
3. println(max.apply(1020))    // 顯示 20

- FirstFunc2.scala 代碼 :
1. def toOneByRow(array: Array[Array[Int]]) = {
2.     val arr = new Array[Int](array.length * array(0).length)
3.     for(row <- nbsp="" span="">0 until array.length; column <- nbsp="" span="">0 until array(0).length) {
4.         arr(row * array(0).length + column) = array(row)(column) // 差異部分
5.      }
6.     arr
7. }
8.
9. def toOneByColumn(array: Array[Array[Int]]) = {
10.     val arr = new Array[Int](array.length * array(0).length)
11.     for(row <- nbsp="" span="">0 until array.length; column <- nbsp="" span="">0 until array(0).length) {
12.         arr(row + column * array.length) = array(row)(column) // 差異部分
13.      }
14.     arr
15. }

- FirstFunc3.scala 代碼 :
1. def toOneByRow(array: Array[Array[Int]]) = {
2.     toOne(array, _ * array(0).length + _)
3. }
4.
5. def toOneByColumn(array: Array[Array[Int]]) = {
6.     toOne(array, _ + _ * array.length)
7. }
8.
9. def toOne(array: Array[Array[Int]], index: (Int, Int) => Int) = {
10.     val arr = new Array[Int](array.length * array(0).length)
11.     for(row <- nbsp="" span="">0 until array.length; column <- nbsp="" span="">0 until array(0).length) {
12.         arr(index(row, column)) = array(row)(column)
13.     }
14.     arr
15. }

## 關於我自己

Where there is a will, there is a way!