According to the ScalaDocs page:
It is the responsibility of the caller to call isDefinedAt
before calling apply
...
Let's try your r()
Partial Function in a context where isDefinedAt()
is called automatically.
val r = new PartialFunction[Int, Int] {
def isDefinedAt(q: Int) = q < 0
def apply(q: Int) = 12 * q
}
List(4,-3,22,-9,0).collect(r)
//res0: List[Int] = List(-36, -108)
Seems to work as expected.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…