updated examples
git-svn-id: http://lampsvn.epfl.ch/svn-repos/scala/scala/trunk@11067 5e8d7ff9-d8ef-0310-90f0-a4852d11357a
This commit is contained in:
parent
4ace1792ad
commit
43b61c4370
|
@ -58,16 +58,16 @@ abstract class Parsers {
|
|||
|
||||
class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String] {
|
||||
|
||||
val EOI: char = 0;
|
||||
val EOI: char = 0
|
||||
|
||||
def nextChar() =
|
||||
if (in.hasNext) in.next else EOI;
|
||||
if (in.hasNext) in.next else EOI
|
||||
|
||||
private var ch = nextChar();
|
||||
|
||||
def isDelimiter(ch: Char) = {
|
||||
var i = 0;
|
||||
while (i < delimiters.length() && delimiters.charAt(i) != ch) { i = i + 1 }
|
||||
var i = 0
|
||||
while (i < delimiters.length() && delimiters.charAt(i) != ch) { i += 1 }
|
||||
i < delimiters.length()
|
||||
}
|
||||
|
||||
|
@ -76,7 +76,7 @@ class Tokenizer(in: Iterator[char], delimiters: String) extends Iterator[String]
|
|||
private val buf = new StringBuffer
|
||||
|
||||
def next: String = {
|
||||
while (ch <= ' ' && ch != EOI) nextChar();
|
||||
while (ch <= ' ' && ch != EOI) nextChar()
|
||||
if (ch == EOI) ""
|
||||
else {
|
||||
if (isDelimiter(ch)) ch.toString()
|
||||
|
@ -102,7 +102,7 @@ trait TokenParsers extends Parsers {
|
|||
trait CharParsers extends Parsers {
|
||||
def any: Parser[char]
|
||||
def chr(ch: char) =
|
||||
for (c <- any; if c == ch) yield c
|
||||
for (c <- any if c == ch) yield c
|
||||
def chr(p: char => boolean) =
|
||||
for (c <- any; if p(c)) yield c
|
||||
for (c <- any if p(c)) yield c
|
||||
}
|
||||
|
|
|
@ -12,13 +12,13 @@ object boundedbuffer {
|
|||
|
||||
def put(x: a) = synchronized {
|
||||
await (n < N)
|
||||
elems(in) = x; in = (in + 1) % N; n = n + 1
|
||||
elems(in) = x; in = (in + 1) % N; n += 1
|
||||
if (n == 1) notifyAll()
|
||||
}
|
||||
|
||||
def get: a = synchronized {
|
||||
await (n != 0)
|
||||
val x = elems(out); out = (out + 1) % N ; n = n - 1
|
||||
val x = elems(out); out = (out + 1) % N ; n -= 1
|
||||
if (n == N - 1) notifyAll()
|
||||
x
|
||||
}
|
||||
|
@ -27,17 +27,17 @@ object boundedbuffer {
|
|||
def kill(delay: Int) = new java.util.Timer().schedule(
|
||||
new java.util.TimerTask {
|
||||
override def run() = {
|
||||
Console.println("[killed]")
|
||||
println("[killed]")
|
||||
System.exit(0)
|
||||
}
|
||||
},
|
||||
delay) // in milliseconds
|
||||
|
||||
def main(args: Array[String]) = {
|
||||
def main(args: Array[String]) {
|
||||
val buf = new BoundedBuffer[String](10)
|
||||
var cnt = 0
|
||||
def produceString = { cnt = cnt + 1; cnt.toString() }
|
||||
def consumeString(ss: String) = Console.println(ss)
|
||||
def produceString = { cnt += 1; cnt.toString() }
|
||||
def consumeString(ss: String) = println(ss)
|
||||
spawn { while (true) { val ssss = produceString; buf.put(ssss) } }
|
||||
spawn { while (true) { val s = buf.get; consumeString(s) } }
|
||||
kill(1000)
|
||||
|
|
|
@ -15,7 +15,7 @@ class ComputeServer(n: Int) {
|
|||
private def processor(i: Int): Unit = {
|
||||
while (true) {
|
||||
val job = openJobs.read
|
||||
Console.println("read a job")
|
||||
println("read a job")
|
||||
job.ret(job.task)
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ object computeserver extends Application {
|
|||
def kill(delay: Int) = new java.util.Timer().schedule(
|
||||
new java.util.TimerTask {
|
||||
override def run() = {
|
||||
Console.println("[killed]")
|
||||
println("[killed]")
|
||||
System.exit(0)
|
||||
}
|
||||
},
|
||||
|
@ -48,6 +48,6 @@ object computeserver extends Application {
|
|||
|
||||
val server = new ComputeServer(1)
|
||||
val f = server.future(42)
|
||||
Console.println(f())
|
||||
println(f())
|
||||
kill(10000)
|
||||
}
|
||||
|
|
|
@ -16,7 +16,7 @@ object fors {
|
|||
printOlderThan20(xs.elements)
|
||||
|
||||
def printOlderThan20(xs: Iterator[Person]): Iterator[String] =
|
||||
for (p <- xs; if p.age > 20) yield p.name
|
||||
for (p <- xs if p.age > 20) yield p.name
|
||||
|
||||
val persons = List(
|
||||
new Person("John", 40),
|
||||
|
@ -24,13 +24,13 @@ object fors {
|
|||
)
|
||||
|
||||
def divisors(n: Int): List[Int] =
|
||||
for (i <- List.range(1, n+1); if n % i == 0) yield i
|
||||
for (i <- List.range(1, n+1) if n % i == 0) yield i
|
||||
|
||||
def isPrime(n: Int) = divisors(n).length == 2
|
||||
|
||||
def findNums(n: Int): Iterator[Pair[Int, Int]] =
|
||||
for (i <- Iterator.range(1, n);
|
||||
j <- Iterator.range(1, i-1);
|
||||
for (i <- 1 until n;
|
||||
j <- 1 until (i-1);
|
||||
if isPrime(i+j)) yield Pair(i, j)
|
||||
|
||||
def sum(xs: List[Double]): Double =
|
||||
|
@ -89,24 +89,24 @@ object fors {
|
|||
if (xs.isEmpty)
|
||||
xs
|
||||
else
|
||||
xs.head :: removeDuplicates(for (x <- xs.tail; if x != xs.head) yield x)
|
||||
xs.head :: removeDuplicates(for (x <- xs.tail if x != xs.head) yield x)
|
||||
|
||||
def main(args: Array[String]) = {
|
||||
Console.print("Persons over 20:")
|
||||
printOlderThan20(persons) foreach { x => Console.print(" " + x) }
|
||||
Console.println
|
||||
def main(args: Array[String]) {
|
||||
print("Persons over 20:")
|
||||
printOlderThan20(persons) foreach { x => print(" " + x) }
|
||||
println
|
||||
|
||||
Console.println("divisors(34) = " + divisors(34))
|
||||
println("divisors(34) = " + divisors(34))
|
||||
|
||||
Console.print("findNums(15) =");
|
||||
print("findNums(15) =");
|
||||
findNums(15) foreach { x => Console.print(" " + x); }
|
||||
Console.println
|
||||
println
|
||||
|
||||
val xs = List(3.5, 5.0, 4.5)
|
||||
Console.println("average(" + xs + ") = " + sum(xs) / xs.length)
|
||||
println("average(" + xs + ") = " + sum(xs) / xs.length)
|
||||
|
||||
val ys = List(2.0, 1.0, 3.0)
|
||||
Console.println("scalProd(" + xs + ", " + ys +") = " + scalProd(xs, ys))
|
||||
println("scalProd(" + xs + ", " + ys +") = " + scalProd(xs, ys))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,10 +8,10 @@ object futures {
|
|||
def f(x: Int) = x + x
|
||||
def g(x: Int) = x * x
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
def main(args: Array[String]) {
|
||||
val x = future(someLengthyComputation)
|
||||
anotherLengthyComputation
|
||||
val y = f(x()) + g(x())
|
||||
Console.println(y)
|
||||
println(y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package examples;
|
||||
package examples
|
||||
|
||||
object iterators {
|
||||
|
||||
def Array(elems: Double*): Array[Double] = {
|
||||
val ar = new Array[Double](elems.length)
|
||||
for (i <- Iterator.range(0, elems.length))
|
||||
for (i <- 0 until elems.length)
|
||||
ar(i) = elems(i)
|
||||
ar
|
||||
}
|
||||
|
||||
def printArray(xs: Array[Double]) =
|
||||
Iterator.fromArray(xs) foreach { x => Console.println(x) }
|
||||
Iterator.fromArray(xs) foreach { x => println(x) }
|
||||
|
||||
def findGreater(xs: Array[Double], limit: Double) =
|
||||
Iterator.fromArray(xs)
|
||||
|
@ -18,10 +18,10 @@ object iterators {
|
|||
.filter{case Pair(x, i) => x > limit }
|
||||
.map{case Pair(x, i) => i}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
def main(args: Array[String]) {
|
||||
val ar = Array/*[Double]*/(6, 2, 8, 5, 1)
|
||||
printArray(ar)
|
||||
Console.println("Elements greater than 3.0:")
|
||||
println("Elements greater than 3.0:")
|
||||
findGreater(ar, 3.0) foreach { x => Console.println(ar(x)) }
|
||||
}
|
||||
|
||||
|
|
|
@ -171,12 +171,12 @@ object maps {
|
|||
|
||||
override def equals(that: Any): Boolean =
|
||||
that.isInstanceOf[Date] && {
|
||||
val o = that.asInstanceOf[Date];
|
||||
day == o.day && month == o.month && year == o.year
|
||||
val o = that.asInstanceOf[Date];
|
||||
day == o.day && month == o.month && year == o.year
|
||||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]) = {
|
||||
def main(args: Array[String]) {
|
||||
val t = new OOBinTree[Date, String]()
|
||||
()
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package examples
|
|||
|
||||
object oneplacebuffer {
|
||||
|
||||
import scala.concurrent._;
|
||||
import scala.concurrent._
|
||||
|
||||
class OnePlaceBuffer {
|
||||
private val m = new MailBox() {} // An internal mailbox
|
||||
|
@ -33,17 +33,17 @@ object oneplacebuffer {
|
|||
},
|
||||
delay) // in milliseconds
|
||||
|
||||
def main(args: Array[String]) = {
|
||||
def main(args: Array[String]) {
|
||||
val buf = new OnePlaceBuffer
|
||||
val random = new java.util.Random()
|
||||
|
||||
def producer(n: int): unit = {
|
||||
def producer(n: int) {
|
||||
Thread.sleep(random.nextInt(1000))
|
||||
buf.write(n)
|
||||
producer(n + 1)
|
||||
}
|
||||
|
||||
def consumer: unit = {
|
||||
def consumer {
|
||||
Thread.sleep(random.nextInt(1000))
|
||||
val n = buf.read
|
||||
consumer
|
||||
|
|
|
@ -79,7 +79,7 @@ object parsers1 {
|
|||
}
|
||||
|
||||
object TestList {
|
||||
def main(args: Array[String]): Unit =
|
||||
def main(args: Array[String]) {
|
||||
Console.println(
|
||||
if (args.length == 1) {
|
||||
val ps = new ParseString(args(0)) with ListParsers
|
||||
|
@ -93,10 +93,11 @@ object parsers1 {
|
|||
else
|
||||
"usage: java examples.TestList <expr-string>"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
object TestExpr {
|
||||
def main(args: Array[String]): unit =
|
||||
def main(args: Array[String]) {
|
||||
Console.println(
|
||||
if (args.length == 1) {
|
||||
val ps = new ParseString(args(0)) with ExprParsers
|
||||
|
@ -110,9 +111,10 @@ object parsers1 {
|
|||
else
|
||||
"usage: java examples.TestExpr <expr-string>"
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
def main(args: Array[String]) {
|
||||
TestList.main(Array("(a,b,(1,2))"))
|
||||
TestExpr.main(Array("2+3+(4+1)"))
|
||||
}
|
||||
|
|
|
@ -52,7 +52,7 @@ object parsers2 {
|
|||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]): unit =
|
||||
def main(args: Array[String]) {
|
||||
Console.println(
|
||||
if (args.length == 1) {
|
||||
val ps = new ParseString(args(0)) with ListParsers
|
||||
|
@ -64,5 +64,6 @@ object parsers2 {
|
|||
else
|
||||
"usage: scala examples.parsers2 <expr-string>"
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -29,8 +29,8 @@ object patterns {
|
|||
case None => System.out.println("no match")
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
Console.println("sum of leafs=" + sumLeaves(tree1))
|
||||
def main(args: Array[String]) {
|
||||
println("sum of leafs=" + sumLeaves(tree1))
|
||||
printFinds(List(Pair(3, "three"), Pair(4, "four")), 4)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,23 +2,23 @@ package examples
|
|||
|
||||
object sort {
|
||||
|
||||
def sort(a: Array[Int]): Unit = {
|
||||
def sort(a: Array[Int]) {
|
||||
|
||||
def swap(i: Int, j: Int): Unit = {
|
||||
def swap(i: Int, j: Int) {
|
||||
val t = a(i); a(i) = a(j); a(j) = t
|
||||
}
|
||||
|
||||
def sort1(l: Int, r: Int): Unit = {
|
||||
def sort1(l: Int, r: Int) {
|
||||
val pivot = a((l + r) / 2)
|
||||
var i = l
|
||||
var j = r
|
||||
while (i <= j) {
|
||||
while (a(i) < pivot) { i = i + 1 }
|
||||
while (a(j) > pivot) { j = j - 1 }
|
||||
while (a(i) < pivot) { i += 1 }
|
||||
while (a(j) > pivot) { j -= 1 }
|
||||
if (i <= j) {
|
||||
swap(i, j)
|
||||
i = i + 1
|
||||
j = j - 1
|
||||
i += 1
|
||||
j -= 1
|
||||
}
|
||||
}
|
||||
if (l < j) sort1(l, j)
|
||||
|
@ -29,7 +29,7 @@ object sort {
|
|||
sort1(0, a.length - 1)
|
||||
}
|
||||
|
||||
def println(ar: Array[Int]) = {
|
||||
def println(ar: Array[Int]) {
|
||||
def print1 = {
|
||||
def iter(i: Int): String =
|
||||
ar(i) + (if (i < ar.length-1) "," + iter(i+1) else "")
|
||||
|
@ -38,7 +38,7 @@ object sort {
|
|||
Console.println("[" + print1 + "]")
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
def main(args: Array[String]) {
|
||||
val ar = Array(6, 2, 8, 5, 1)
|
||||
println(ar)
|
||||
sort(ar)
|
||||
|
|
|
@ -13,10 +13,10 @@ object sort1 {
|
|||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]) = {
|
||||
def main(args: Array[String]) {
|
||||
val xs = List(6, 2, 8, 5, 1)
|
||||
Console.println(xs)
|
||||
Console.println(sort(xs))
|
||||
println(xs)
|
||||
println(sort(xs))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -16,10 +16,10 @@ object sort2 {
|
|||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
def main(args: Array[String]) {
|
||||
val xs = List(6, 2, 8, 5, 1)
|
||||
Console.println(xs)
|
||||
Console.println(sort(xs))
|
||||
println(xs)
|
||||
println(sort(xs))
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ case class Tycon(k: String, ts: List[Type]) extends Type {
|
|||
object typeInfer {
|
||||
|
||||
private var n: Int = 0
|
||||
def newTyvar(): Type = { n = n + 1 ; Tyvar("a" + n) }
|
||||
def newTyvar(): Type = { n += 1; Tyvar("a" + n) }
|
||||
|
||||
trait Subst extends Function1[Type, Type] {
|
||||
def lookup(x: Tyvar): Type
|
||||
|
@ -169,7 +169,7 @@ object typeInfer {
|
|||
|
||||
/** Non-keyword identifiers */
|
||||
def ident: Parser[String] =
|
||||
for (s <- id; if s != "let" && s != "in") yield s
|
||||
for (s <- id if s != "let" && s != "in") yield s
|
||||
|
||||
/** term = '\' ident '.' term | term1 {term1} | let ident "=" term in term */
|
||||
def term: Parser[Term] = (
|
||||
|
@ -181,7 +181,7 @@ object typeInfer {
|
|||
yield Lam(x, t): Term )
|
||||
|||
|
||||
( for (
|
||||
letid <- id; if letid == "let";
|
||||
letid <- id if letid == "let";
|
||||
x <- ident;
|
||||
_ <- wschr('=');
|
||||
t <- term;
|
||||
|
@ -234,7 +234,7 @@ object typeInfer {
|
|||
"\n reason: " + msg
|
||||
}
|
||||
|
||||
def main(args: Array[String]): unit =
|
||||
def main(args: Array[String]) {
|
||||
Console.println(
|
||||
if (args.length == 1) {
|
||||
val ps = new ParseString(args(0)) with MiniMLParsers
|
||||
|
@ -248,5 +248,6 @@ object typeInfer {
|
|||
else
|
||||
"usage: java examples.typeinf <expr-string>"
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue