模式匹配與函數(shù)組合

2018-02-24 15:49 更新

課程內(nèi)容:

函數(shù)組合

讓我們創(chuàng)建兩個函數(shù):

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?組合其他函數(shù)形成一個新的函數(shù)?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很像,但是調(diào)用順序是先調(diào)用第一個函數(shù),然后調(diào)用第二個,即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 偏應(yīng)用

case 語句

那么究竟什么是case語句?

這是一個名為PartialFunction的函數(shù)的子類。

多個case語句的集合是什么?

他們是共同組合在一起的多個PartialFunction。

理解PartialFunction(偏函數(shù))

對給定的輸入?yún)?shù)類型,函數(shù)可接受該類型的任何值。換句話說,一個(Int) => String?的函數(shù)可以接收任意Int值,并返回一個字符串。

對給定的輸入?yún)?shù)類型,偏函數(shù)只能接受該類型的某些特定的值。一個定義為(Int) => String?的偏函數(shù)可能不能接受所有Int值為輸入。

isDefinedAt?是PartialFunction的一個方法,用來確定PartialFunction是否能接受一個給定的參數(shù)。

注意?偏函數(shù)PartialFunction?和我們前面提到的部分應(yīng)用函數(shù)是無關(guān)的。

參考?Effective Scala 對PartialFunction的意見。

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

您可以調(diào)用一個偏函數(shù)。

scala> one(1)
res2: String = one

PartialFunctions可以使用orElse組成新的函數(shù),得到的PartialFunction反映了是否對給定參數(shù)進行了定義。

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 之謎

上周我們看到一些新奇的東西。我們在通常應(yīng)該使用函數(shù)的地方看到了一個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使用一個函數(shù)。在這個例子中是一個謂詞函數(shù)(PhoneExt) => Boolean。

PartialFunction是Function的子類型,所以filter也可以使用PartialFunction!

Built at?@twitter?by?@stevej,?@marius, and?@lahosken?with much help from?@evanm,?@sprsquish,?@kevino,?@zuercher,?@timtrueman,?@wickman, and@mccv; Russian translation by?appigram; Chinese simple translation by?jasonqu; Korean translation by?enshahar;

Licensed under the?Apache License v2.0.

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號