web/zh_cn/pattern-matching-and-functional-composition.textile (98 lines of code) (raw):

--- prev: collections.textile next: type-basics.textile title: 模式匹配与函数组合 layout: post --- 课程内容: * "函数组合":#composition ** compose ** andThen * "柯里化 vs 偏应用":#curryvspartial * "偏函数 PartialFunctions":#PartialFunction ** 范围和域 range and domain ** 使用orElse进行组合 * "case 之谜":#case h2(#composition). 函数组合 让我们创建两个函数: <pre> scala> def f(s: String) = "f(" + s + ")" f: (String)java.lang.String scala> def g(s: String) = "g(" + s + ")" g: (String)java.lang.String </pre> h3. compose <code>compose</code> 组合其他函数形成一个新的函数 <code>f(g(x))</code> <pre> scala> val fComposeG = f _ compose g _ fComposeG: (String) => java.lang.String = <function> scala> fComposeG("yay") res0: java.lang.String = f(g(yay)) </pre> h3. andThen <code>andThen</code> 和 <code>compose</code>很像,但是调用顺序是先调用第一个函数,然后调用第二个,即<code>g(f(x))</code> <pre> scala> val fAndThenG = f _ andThen g _ fAndThenG: (String) => java.lang.String = <function> scala> fAndThenG("yay") res1: java.lang.String = g(f(yay)) </pre> h2(#curryvspartial). 柯里化 vs 偏应用 h3. case 语句 h4. 那么究竟什么是case语句? 这是一个名为PartialFunction的函数的子类。 h4. 多个case语句的集合是什么? 他们是共同组合在一起的多个PartialFunction。 h2(#PartialFunction). 理解PartialFunction(偏函数) 对给定的输入参数类型,函数可接受该类型的任何值。换句话说,一个<code>(Int) => String</code> 的函数可以接收任意Int值,并返回一个字符串。 对给定的输入参数类型,偏函数只能接受该类型的某些特定的值。一个定义为<code>(Int) => String</code> 的偏函数可能不能接受所有Int值为输入。 <code>isDefinedAt</code> 是PartialFunction的一个方法,用来确定PartialFunction是否能接受一个给定的参数。 __注意__ 偏函数<code>PartialFunction</code> 和我们前面提到的部分应用函数是无关的。 *参考* Effective Scala 对<a href="https://twitter.github.com/effectivescala/#Functional programming-Partial functions">PartialFunction</a>的意见。 <pre> 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 </pre> 您可以调用一个偏函数。 <pre> scala> one(1) res2: String = one </pre> PartialFunctions可以使用<code>orElse</code>组成新的函数,得到的PartialFunction反映了是否对给定参数进行了定义。 <pre> 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 </pre> h3(#case). case 之谜 上周我们看到一些新奇的东西。我们在通常应该使用函数的地方看到了一个case语句。 <pre> 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)) </pre> 为什么这段代码可以工作? filter使用一个函数。在这个例子中是一个谓词函数(PhoneExt) => Boolean。 PartialFunction是Function的子类型,所以filter也可以使用PartialFunction!