課程內容:
[TOC=2,2]
## 函數組合
讓我們創建兩個函數:
~~~
scala> def f(s: String) = "f(" + s + ")"
f: (String)java.lang.String
scala> def g(s: String) = "g(" + s + ")"
g: (String)java.lang.String
~~~
### compose
`compose`?組合其他函數形成一個新的函數?`f(g(x))`
~~~
scala> val fComposeG = f _ compose g _
fComposeG: (String) => java.lang.String = <function>
scala> fComposeG("yay")
res0: java.lang.String = f(g(yay))
~~~
### andThen
`andThen`?和?`compose`很像,但是調用順序是先調用第一個函數,然后調用第二個,即`g(f(x))`
~~~
scala> val fAndThenG = f _ andThen g _
fAndThenG: (String) => java.lang.String = <function>
scala> fAndThenG("yay")
res1: java.lang.String = g(f(yay))
~~~
## 柯里化 vs 偏應用
### case 語句
#### 那么究竟什么是case語句?
這是一個名為PartialFunction的函數的子類。
#### 多個case語句的集合是什么?
他們是共同組合在一起的多個PartialFunction。
## 理解PartialFunction(偏函數)
對給定的輸入參數類型,函數可接受該類型的任何值。換句話說,一個`(Int) => String`?的函數可以接收任意Int值,并返回一個字符串。
對給定的輸入參數類型,偏函數只能接受該類型的某些特定的值。一個定義為`(Int) => String`?的偏函數可能不能接受所有Int值為輸入。
`isDefinedAt`?是PartialFunction的一個方法,用來確定PartialFunction是否能接受一個給定的參數。
*注意*?偏函數`PartialFunction`?和我們前面提到的部分應用函數是無關的。
**參考**?Effective Scala 對[PartialFunction](http://twitter.github.com/effectivescala/#Functional programming-Partial functions)的意見。
~~~
scala> val one: PartialFunction[Int, String] = { case 1 => "one" }
one: PartialFunction[Int,String] = <function1>
scala> one.isDefinedAt(1)
res0: Boolean = true
scala> one.isDefinedAt(2)
res1: Boolean = false
~~~
您可以調用一個偏函數。
~~~
scala> one(1)
res2: String = one
~~~
PartialFunctions可以使用`orElse`組成新的函數,得到的PartialFunction反映了是否對給定參數進行了定義。
~~~
scala> val two: PartialFunction[Int, String] = { case 2 => "two" }
two: PartialFunction[Int,String] = <function1>
scala> val three: PartialFunction[Int, String] = { case 3 => "three" }
three: PartialFunction[Int,String] = <function1>
scala> val wildcard: PartialFunction[Int, String] = { case _ => "something else" }
wildcard: PartialFunction[Int,String] = <function1>
scala> val partial = one orElse two orElse three orElse wildcard
partial: PartialFunction[Int,String] = <function1>
scala> partial(5)
res24: String = something else
scala> partial(3)
res25: String = three
scala> partial(2)
res26: String = two
scala> partial(1)
res27: String = one
scala> partial(0)
res28: String = something else
~~~
### case 之謎
上周我們看到一些新奇的東西。我們在通常應該使用函數的地方看到了一個case語句。
~~~
scala> case class PhoneExt(name: String, ext: Int)
defined class PhoneExt
scala> val extensions = List(PhoneExt("steve", 100), PhoneExt("robey", 200))
extensions: List[PhoneExt] = List(PhoneExt(steve,100), PhoneExt(robey,200))
scala> extensions.filter { case PhoneExt(name, extension) => extension < 200 }
res0: List[PhoneExt] = List(PhoneExt(steve,100))
~~~
為什么這段代碼可以工作?
filter使用一個函數。在這個例子中是一個謂詞函數(PhoneExt) => Boolean。
PartialFunction是Function的子類型,所以filter也可以使用PartialFunction!
Built at?[@twitter](http://twitter.com/twitter)?by?[@stevej](http://twitter.com/stevej),?[@marius](http://twitter.com/marius), and?[@lahosken](http://twitter.com/lahosken)?with much help from?[@evanm](http://twitter.com/evanm),?[@sprsquish](http://twitter.com/sprsquish),?[@kevino](http://twitter.com/kevino),?[@zuercher](http://twitter.com/zuercher),?[@timtrueman](http://twitter.com/timtrueman),?[@wickman](http://twitter.com/wickman), and[@mccv](http://twitter.com/mccv); Russian translation by?[appigram](https://github.com/appigram); Chinese simple translation by?[jasonqu](https://github.com/jasonqu); Korean translation by?[enshahar](https://github.com/enshahar);
Licensed under the?[Apache License v2.0](http://www.apache.org/licenses/LICENSE-2.0).