diff options
author | Paul Phillips <paulp@improving.org> | 2012-09-20 09:58:20 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-09-20 09:58:46 -0700 |
commit | 08de8afde6c99fb1739eb9730e6fce4eef54f296 (patch) | |
tree | 30b73fcb7e97ac945a43d18c370bb4d2aa7d6949 /test/files/run/partialfun.scala | |
parent | e4e8578c5cf433648fa5428bfb2ad2ce8d347439 (diff) | |
download | scala-08de8afde6c99fb1739eb9730e6fce4eef54f296.tar.gz scala-08de8afde6c99fb1739eb9730e6fce4eef54f296.tar.bz2 scala-08de8afde6c99fb1739eb9730e6fce4eef54f296.zip |
Normalized line endings.
This brings all the files into line with the .gitattributes
settings, which should henceforth be automatically maintained
by git.
Diffstat (limited to 'test/files/run/partialfun.scala')
-rw-r--r-- | test/files/run/partialfun.scala | 172 |
1 files changed, 86 insertions, 86 deletions
diff --git a/test/files/run/partialfun.scala b/test/files/run/partialfun.scala index 4b360750c9..f3c53b94ae 100644 --- a/test/files/run/partialfun.scala +++ b/test/files/run/partialfun.scala @@ -1,86 +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()
- }
-}
+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() + } +} |