summaryrefslogtreecommitdiff
path: root/test/files/run/t6448.scala
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2012-09-29 18:15:15 +0200
committerJason Zaugg <jzaugg@gmail.com>2012-11-04 21:21:51 +0100
commitbc3dda2b0222d3b7cf3db491728b98f9b6110856 (patch)
treed71d81b7c443e75e12da82e9d6e8c53c84d31f1d /test/files/run/t6448.scala
parent2c6777fd53b93b95a261aadc87a5cbc03c14d503 (diff)
downloadscala-bc3dda2b0222d3b7cf3db491728b98f9b6110856.tar.gz
scala-bc3dda2b0222d3b7cf3db491728b98f9b6110856.tar.bz2
scala-bc3dda2b0222d3b7cf3db491728b98f9b6110856.zip
SI-6448 Collecting the spoils of PartialFun#runWith
Avoids calling both `isDefinedAt` and `apply`. This pathological case that would benefit the most looks like: xs collect { case x if {expensive(); true} => x } The typical change looks like: - 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
Diffstat (limited to 'test/files/run/t6448.scala')
-rw-r--r--test/files/run/t6448.scala61
1 files changed, 61 insertions, 0 deletions
diff --git a/test/files/run/t6448.scala b/test/files/run/t6448.scala
new file mode 100644
index 0000000000..4d1528e500
--- /dev/null
+++ b/test/files/run/t6448.scala
@@ -0,0 +1,61 @@
+// Tests to show that various `collect` functions avoid calling
+// both `PartialFunction#isDefinedAt` and `PartialFunction#apply`.
+//
+object Test {
+ def f(i: Int) = { println("f(" + i + ")"); true }
+ class Counter {
+ var count = 0
+ def apply(i: Int) = synchronized {count += 1; true}
+ }
+
+ def testing(label: String)(body: => Any) {
+ println(s"\n=$label=")
+ println(body)
+ }
+
+ def main(args: Array[String]) {
+ testing("List.collect")(List(1, 2) collect { case x if f(x) && x < 2 => x})
+ testing("List.collectFirst")(List(1, 2) collectFirst { case x if f(x) && x < 2 => x})
+ testing("Option.collect")(Some(1) collect { case x if f(x) && x < 2 => x})
+ testing("Option.collect")(Some(2) collect { case x if f(x) && x < 2 => x})
+ testing("Stream.collect")((Stream(1, 2).collect { case x if f(x) && x < 2 => x}).toList)
+ testing("Stream.collectFirst")(Stream.continually(1) collectFirst { case x if f(x) && x < 2 => x})
+
+ import collection.parallel.ParIterable
+ import collection.parallel.immutable.ParVector
+ import collection.parallel.mutable.ParArray
+ testing("ParVector.collect") {
+ val counter = new Counter()
+ (ParVector(1, 2) collect { case x if counter(x) && x < 2 => x}, counter.synchronized(counter.count))
+ }
+
+ testing("ParArray.collect") {
+ val counter = new Counter()
+ (ParArray(1, 2) collect { case x if counter(x) && x < 2 => x}, counter.synchronized(counter.count))
+ }
+
+ object PendingTests {
+ testing("Iterator.collect")((Iterator(1, 2) collect { case x if f(x) && x < 2 => x}).toList)
+
+ testing("List.view.collect")((List(1, 2).view collect { case x if f(x) && x < 2 => x}).force)
+
+ // This would do the trick in Future.collect, but I haven't added this yet as there is a tradeoff
+ // with extra allocations to consider.
+ //
+ // pf.lift(v) match {
+ // case Some(x) => p success x
+ // case None => fail(v)
+ // }
+ testing("Future.collect") {
+ import concurrent.ExecutionContext.Implicits.global
+ import concurrent.Await
+ import concurrent.duration.Duration
+ val result = concurrent.future(1) collect { case x if f(x) => x}
+ Await.result(result, Duration.Inf)
+ }
+
+ // TODO Future.{onSuccess, onFailure, recoverWith, andThen}
+ }
+
+ }
+}