-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathscala_spec.test.scala
242 lines (191 loc) · 5.58 KB
/
scala_spec.test.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package org.scala.syntax
package test
class TreeMap[A <: Comparable[A], B] { }
class List[A] { }
class I extends Comparable[I] {}
class F[M[_], X] { }
class S[K <: String] {}
class G[M[ Z <: I ], I] { }
case class Bird (val name: String @suspendable) extends Object {
def fly(height: Int @suspendable) = ???
def walk[T1 : T2](_distance: Int, empty_? : Int, `yield`: String, αρετη: Boolean)(implicit fa: Functor[T1]) = ???
def c (x: Int) (y: String, z: String): String = ???
def union[A <: Comparable[A]] (x: Set[A], xs: Set[A]): Set[A]
}
trait Iterable[+X] {
def flatMap[newType[+X] <: Iterable[X], S](f: X => newType[S]): newType[S]
}
package org.scala {
class List[+T]
class Set[-T]
}
class A[+T] {}
class B extends A[B]
class C extends A[C]
abstract class AbstractClass
sealed trait Sealed
object Types {
val x : String @suspendable = ""
def infix() : T1 \/ T2 = ???
type Z1 = Ref[T] forSome { type T <: java.lang.Number }
type Z2 = Ref[x.T] forSome { val x: Outer }
type Z3 = Ref[x_type # T] forSome { type x_type <: Outer with Singleton }
def complexBounds[A, B >: A, C >: A <: B]()
def complexBounds2[M[X <: Bound[X]], Bound[_]]
def complexBounds3[@specialized T, U]()
def compare[T](a: T = 0)(b: T = a) = (a == b)
def f(a: Int = 0)(b: Int = a + 1) = b
def whileLoop (cond: => Boolean) (stat: => Unit): Unit
def sum(args: Int*) = {
var result = 0
for (arg <- args) result += arg
result
}
def write(str: String) { System.out.println(str) }
def write(str: String): Unit = { System.out.println(str) }
type Pair[+A, +B] = Tuple2[A, B]
object Pair {
{ import M.{one, z => zero, _}; add(zero, one) }
def apply[A, B](x: A, y: B) = Tuple2(x, y)
def unapply[A, B](x: Tuple2[A, B]): Option[Tuple2[A, B]] = Some(x)
}
}
object ValueDefinitions {
val pi = 3.1415
val pi: Double = 3.1415 // equivalent to first definition
val Some(x) = f() // a pattern definition
val x :: xs = mylist // an infix pattern definition
}
class Iter extends StringIterator with RichIterator { }
trait A extends Root { type T <: A }
class Modifiers {
final val x = e
override def f()
private val y = ""
abstract override def g()
private lazy final val h = 3
}
object m {
abstract sealed class C (x: Int) {
def nextC = new C(x + 1) {}
}
val empty = new C(0) {}
}
class LinkedList[A]() { self: List with Seq =>
var head = ???
var tail = null
def isEmpty = tail != null
def this(head: A) = { this(); this.head = head }
def this(head: A, tail: List[A]) = { this(head); this.tail = tail }
}
case class Lambda(x: String, e: Expr) extends Expr
trait Comparable[T <: Comparable[T]] { self: T =>
def < (that: T): Boolean
def <=(that: T): Boolean = this < that || this == that
def > (that: T): Boolean = that < this
def >=(that: T): Boolean = that <= this
}
class A extends Root { override def x = "A" ; def superA = super.x }
object Appl {
def sum(xs: Int*) = (0 /: xs) ((x, y) => x + y)
sum(List(1, 2, 3, 4): _*)
val x : S = new Z
val y : S = new Z {
val x = 5
}
def matmul(xss: Array[Array[Double]], yss: Array[Array[Double]]) = {
val zss: Array[Array[Double]] = new Array(xss.length, yss(0).length)
var i = 0
while (i < xss.length) {
var j = 0
while (j < yss(0).length) {
var acc = 0.0
var k = 0
while (k < yss.length) {
acc = acc + xss(i)(k) * yss(k)(j)
k += 1
}
zss(i)(j) = acc
j += 1
}
i += 1
}
zss
}
def whileLoop(cond: => Boolean)(body: => Unit): Unit =
if (cond) { body ; whileLoop(cond)(body) } else {}
do {x += 2} while (x < 100)
for { i <- 1 until n
j <- 1 until i
if isPrime(i+j)
} yield (i, j)
(1 until n)
.flatMap {
case i => (1 until i)
.withFilter { j => isPrime(i+j) }
.map { case j => (i, j) } }
try {
throw ex;
} catch {
case NonFatal(e) => throw e;
case other => throw other
}
}
object Inline {
val summ = (x: Int,y: Int) => x + y
}
object Monoids {
implicit object stringMonoid extends Monoid[String] {
def add(x: String, y: String): String = x.concat(y)
def unit: String = ""
}
}
object A1 {
def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
if (xs.isEmpty) m.unit
else m.add(xs.head, sum(xs.tail))
implicit def list2ordered[A](x: List[A])
(implicit elem2ordered: A => Ordered[A]): Ordered[List[A]]
}
object PatternMatching {
def f(x: Int, y: Int) = x match {
case `y` =>
case s @ Seq(_, _, _) =>
case Seq(first, tail @ _*) =>
case first +: tail =>
case 3 | 5 | 6 =>
case y: Number => y.n
case Lit(n) => n
case IsZero(u) => eval(u) == 0
case _ => 15
}
}
package p1 {
package p2 {
object Kitten
}
}
package a.b {
class A {
val x = new _root_.b.B
}
}
object HelloWorld {
def main(args: Array[String]) { println("Hello World") }
}
object HelloWorld extends App {
println("Hello World")
}
@SerialVersionUID(12345)
object Annotations {
@deprecated("Use D", "1.0") class C { }
@transient @volatile var m: Int
def f(x: Option[Int]) = (x: @unchecked) match {
case Some(y) => y
}
trait Function0[@specialized(Unit, Int, Double) T] {
def apply: T
}
@UserDefinedUpperCase def x
@userDefinedLowerCase def y
}