scala/scala — pull request #1570 by retronym (2012-11-04 20:28:58+00:00)
Title: SI-6448 Collecting the spoils of PartialFun#runWith

Avoids calling both `isDefinedAt` and `apply`. This pathological
case that would benefit the most looks like:

```scala
xs collect {
case x if {expensive(); true} => x
}
```

The typical change looks like:

```diff
- for (x <- this) if (pf.isDefinedAt(x)) b += pf(x)
+ foreach(pf.runWith(b += _))
```

Incorporates feedback provided by Pavel Pavlov:

https://github.com/retronym/scala/commit/ef5430

A few more opportunities for optimization are noted in the
`Pending` section of the enclosed test. `Iterator.collect`
would be nice, but a solution eludes me.

Calling the guard less frequently does change the behaviour
of these functions in an obervable way, but not contravene
the documented semantics. That said, there is an alternative
opinion on the comment of the ticket:

https://issues.scala-lang.org/browse/SI-6448

Review by @axel22 @pavelpavlov