# 部份函式（Partial function）

``val fibonacci: Int => Int = {    case 0 => 0    case 1 => 1    case n => fibonacci(n - 1) + fibonacci(n - 2)}println(fibonacci(10)) // 55``

``sealed abstract class Drawingcase class Point(x: Int, y: Int) extends Drawingcase class Circle(p: Point, r: Int) extends Drawingcase class Cylinder(c: Circle, h: Int) extends Drawingclass Graphic {    def show(how: Drawing => Any) = {        how(Point(1, 1))        how(Circle(Point(2, 2), 2))        how(Cylinder(Circle(Point(3, 3), 3), 3))    }}val g = new Graphicg.show {    case Point(_, _)                         => println("點")    case Circle(Point(_, _), _)              => println("圓")    case Cylinder(Circle(Point(_, _), _), _) => println("柱")}``

``import scala.actors.Actor._val caller = selfactor {    caller ! args(0)}receive {    case "some"  => println("do something...")    case "other" => println("do other...")}``

``case class Point(x: Int, y: Int)val what: Point => Int = {    case Point(1, 1) => 1    case Point(2, 2) => 2}println(what(Point(1, 1)))  // 1println(what(Point(2, 2)))  // 2println(what(Point(3, 3)))  // MatchError``

``case class Point(x: Int, y: Int)val what: PartialFunction[Point, Int] = {    case Point(1, 1) => 1    case Point(2, 2) => 2}    ``

PartialFunction有個isDefinedAt()方法，可以讓你測試某個案例是否存在，例如：
``if(what.isDefinedAt(Point(1, 1)))    println(what(Point(1, 1)))else    println("函式沒有定義此情況")    if(what.isDefinedAt(Point(3, 3)))    println(what(Point(3, 3)))else    println("函式沒有定義此情況")``

new PartialFunction[Point, Int] {
def apply(p: Point) = p match {
case Point(1, 1)  => 1
case Point(2, 2)  => 2
}

def isDefinedAt(p: Point) = p match {
case Point(1, 1)  => true
case Point(2, 2)  => true
_                 => false
}
}