forked from scala/scala
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partialfun.scala
155 lines (127 loc) · 4.7 KB
/
partialfun.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import collection._
import collection.generic._
import scala.language.higherKinds
object Test {
def collectIDA[A, B, CC[_], Repr, That](_this: IterableOps[A, CC, Repr])(pf: PartialFunction[A, B])(implicit bf: BuildFrom[Repr, B, That]): That = {
val repr: Repr = _this.asInstanceOf[Repr]
val b = bf.newBuilder(repr)
_this foreach { x => if (pf isDefinedAt x) b += pf(x) }
b.result
}
def collectRW[A, B, CC[_], Repr, That](_this: IterableOps[A, CC, Repr])(pf: PartialFunction[A, B])(implicit bf: BuildFrom[Repr, B, That]): That = {
val repr: Repr = _this.asInstanceOf[Repr]
val b = bf.newBuilder(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(): Unit = {
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(): Unit = {
val pf0 = new PartialFunction[Unit, Unit] {
def apply(u: Unit): Unit = { println("0:apply") }
def isDefinedAt(u: Unit) = { println("0:isDefinedAt"); false }
}
val pf1 = new PartialFunction[Unit, Unit] {
def apply(u: Unit): Unit = { println("1:apply") }
def isDefinedAt(u: Unit) = { println("1:isDefinedAt"); false }
}
val pf2 = new PartialFunction[Unit, Unit] {
def apply(u: Unit): Unit = { println("2:apply") }
def isDefinedAt(u: Unit) = { println("2:isDefinedAt"); true }
}
val chained = pf0 orElse pf1 orElse pf2
chained(())
}
def fromFunctionLiteralTest(): Unit = {
def isEven(n: Int): Boolean = PartialFunction.cond(n)(_ % 2 == 0)
println(isEven(1))
println(isEven(2))
println((1 to 5).map(_.toString))
println((1 to 5).collect(_.toString))
}
def takeFunction1(fn: String => String) = println("fn only")
def takePartialFunction(pf: PartialFunction[String, String]) = println("pf only")
def takeFunctionLike(fn: String => String) = println("fn wins")
def takeFunctionLike(pf: PartialFunction[String, String]) = println("pf wins")
def testOverloadingWithFunction1(): Unit = {
println("testing function literal syntax with methods overloaded for Function1 and PartialFunction")
println("base case: a method that takes a Function1, so no overloading")
takeFunction1(_.reverse)
takeFunction1 { case s => s.reverse }
takeFunction1 { case s: String => s.reverse }
println()
println("base case: a method that takes a PartialFunction, so no overloading")
takePartialFunction(_.reverse)
takePartialFunction { case s => s.reverse }
takePartialFunction { case s: String => s.reverse }
println()
println("test case: a method that is overloaded for Funtion1 and PartialFunction")
takeFunctionLike(_.reverse)
takeFunctionLike { case s => s.reverse }
takeFunctionLike { case s: String => s.reverse }
}
def reverse(s: String): String = s.reverse
def testEtaExpansion(): Unit = {
println("testing eta-expansion with methods overloaded for Function1 and PartialFunction")
println("base case: a method that takes a Function1, so no overloading")
takeFunction1(x => reverse(x))
takeFunction1(reverse(_))
takeFunction1(reverse _)
takeFunction1(reverse)
println()
println("base case: a method that takes a PartialFunction, so no overloading")
takePartialFunction(x => reverse(x))
takePartialFunction(reverse(_))
takePartialFunction(reverse _)
//takePartialFunction(reverse) // can't pass a method to a method that takes a PartialFunction
println()
println("test case: a method that is overloaded for Funtion1 and PartialFunction")
takeFunctionLike(x => reverse(x))
takeFunctionLike(reverse(_))
takeFunctionLike(reverse _)
//takeFunctionLike(reverse) // can't pass a method to a method overloaded to take a Function1 or a PartialFunction
}
def main(args: Array[String]): Unit = {
collectTest()
orElseTest()
println()
fromFunctionLiteralTest()
println()
testOverloadingWithFunction1()
println()
testEtaExpansion()
}
}