From 42c6fb8ce814ed109f6418bcc5809d23e1db1965 Mon Sep 17 00:00:00 2001 From: Pavel Pavlov Date: Tue, 21 Aug 2012 01:40:14 +0700 Subject: PartialFunction polishing - ScalaDocs added - TODOs fixed - controversive method `run` deleted - not used class runtime.AbstractTotalFunction removed - small corrections & fixes - tests for `orElse` & `runWith` --- test/files/run/partialfun.check | 6 +++ test/files/run/partialfun.scala | 86 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 test/files/run/partialfun.check create mode 100644 test/files/run/partialfun.scala (limited to 'test') diff --git a/test/files/run/partialfun.check b/test/files/run/partialfun.check new file mode 100644 index 0000000000..a317f7b150 --- /dev/null +++ b/test/files/run/partialfun.check @@ -0,0 +1,6 @@ +47 +147 +100 +0:isDefinedAt +1:isDefinedAt +2:apply diff --git a/test/files/run/partialfun.scala b/test/files/run/partialfun.scala new file mode 100644 index 0000000000..4b360750c9 --- /dev/null +++ b/test/files/run/partialfun.scala @@ -0,0 +1,86 @@ +import collection._ +import collection.generic._ + +object Test { + def collectIDA[A, B, Repr, That](_this: TraversableLike[A, Repr])(pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { + val repr: Repr = _this.asInstanceOf[Repr] + val b = bf(repr) + _this foreach { x => if (pf isDefinedAt x) b += pf(x) } + b.result + } + + def collectRW[A, B, Repr, That](_this: TraversableLike[A, Repr])(pf: PartialFunction[A, B])(implicit bf: CanBuildFrom[Repr, B, That]): That = { + val repr: Repr = _this.asInstanceOf[Repr] + val b = bf(repr) + val f = pf runWith { b += _ } + _this foreach f + b.result + } + + var cnt = 0 + + object Ex1 { + def unapply(x: Int) : Option[Int] = { + cnt += 1 + if ((x % 3) == 0) Some(-x) else None + } + } + + object Ex2 { + def unapply(x: Int) : Option[Int] = { + //cnt += 1 + if ((x % 5) == 0) Some(x) else None + } + } + + def resetCnt() = { val r = cnt; cnt = 0; r } + + val pf: PartialFunction[Int,Int] = { + case Ex1(result) => result + case Ex2(result) => result + } + + def collectTest() { + val xs = 1 to 100 + resetCnt() + + val ysIDA = collectIDA(xs)(pf) + val cntIDA = resetCnt() + + val ysRW = collectRW(xs)(pf) + val cntRW = resetCnt() + + val ys = xs collect pf + + assert(ys == ysIDA) + assert(ys == ysRW) + assert(cntIDA == xs.length + ys.length) + assert(cntRW == xs.length) + println(ys.length) + println(cntIDA) + println(cntRW) + } + + def orElseTest() { + val pf0 = new PartialFunction[Unit, Unit] { + def apply(u: Unit) { println("0:apply") } + def isDefinedAt(u: Unit) = { println("0:isDefinedAt"); false } + } + val pf1 = new PartialFunction[Unit, Unit] { + def apply(u: Unit) { println("1:apply") } + def isDefinedAt(u: Unit) = { println("1:isDefinedAt"); false } + } + val pf2 = new PartialFunction[Unit, Unit] { + def apply(u: Unit) { println("2:apply") } + def isDefinedAt(u: Unit) = { println("2:isDefinedAt"); true } + } + + val chained = pf0 orElse pf1 orElse pf2 + chained() + } + + def main(args: Array[String]): Unit = { + collectTest() + orElseTest() + } +} -- cgit v1.2.3