diff options
author | Martin Odersky <odersky@gmail.com> | 2006-06-20 16:34:51 +0000 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2006-06-20 16:34:51 +0000 |
commit | 4d929158efa9a6e95df14c399091a0f213aebf2d (patch) | |
tree | 488270edb1eed025a96c1655e8cc139951d84839 /src/library | |
parent | 640ea6fc45b860e85b588d3217df61d087d674ba (diff) | |
download | scala-4d929158efa9a6e95df14c399091a0f213aebf2d.tar.gz scala-4d929158efa9a6e95df14c399091a0f213aebf2d.tar.bz2 scala-4d929158efa9a6e95df14c399091a0f213aebf2d.zip |
Fixed parsing problem for closures
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/PartialFunction.scala | 26 | ||||
-rw-r--r-- | src/library/scala/Responder.scala | 36 | ||||
-rw-r--r-- | src/library/scala/collection/mutable/HashTable.scala | 2 | ||||
-rw-r--r-- | src/library/scala/concurrent/Process.scala | 2 |
4 files changed, 45 insertions, 21 deletions
diff --git a/src/library/scala/PartialFunction.scala b/src/library/scala/PartialFunction.scala index 8af05728f1..316b62dea0 100644 --- a/src/library/scala/PartialFunction.scala +++ b/src/library/scala/PartialFunction.scala @@ -22,10 +22,24 @@ package scala; */ trait PartialFunction[-A, +B] extends AnyRef with (A => B) { - /** Checks if a value is contained in the functions domain. - * - * @param x the value to test - * @return true, iff <code>x</code> is in the domain of this function. - */ - def isDefinedAt(x: A): Boolean; + /** Checks if a value is contained in the functions domain. + * + * @param x the value to test + * @return true, iff <code>x</code> is in the domain of this function. + */ + def isDefinedAt(x: A): Boolean; + + def orElse[A1 <: A, B1 >: B](that: PartialFunction[A1, B1]) = new PartialFunction[A1, B1] { + def isDefinedAt(x: A1): Boolean = + PartialFunction.this.isDefinedAt(x) || that.isDefinedAt(x) + def apply(x: A1): B1 = + if (PartialFunction.this.isDefinedAt(x)) PartialFunction.this.apply(x) + else that.apply(x) + } + + def andThen[C](k: B => C) = new PartialFunction[A, C] { + def isDefinedAt(x: A): Boolean = PartialFunction.this.isDefinedAt(x) + def apply(x: A): C = k(PartialFunction.this.apply(x)) + } } + diff --git a/src/library/scala/Responder.scala b/src/library/scala/Responder.scala index a86f1d30b8..8d294e95ea 100644 --- a/src/library/scala/Responder.scala +++ b/src/library/scala/Responder.scala @@ -8,46 +8,56 @@ object Responder { /** creates a responder that answer continuations with the constant a */ def constant[a](x: a) = new Responder[a] { - def answer(k: a => unit) = k(x) + def foreach(k: a => unit) = k(x) } /** executes x and returns true, useful as syntactic convenience in - * for comprehensions - */ + * for comprehensions + */ def exec[a](x: => unit): boolean = { x; true } /** runs a responder, returning an optional result */ def run[a](r: Responder[a]): Option[a] = { var result: Option[a] = None - r.answer(x => result = Some(x)) + r.foreach(x => result = Some(x)) result } + + def loop[a](r: Responder[unit]): Responder[Nothing] = + for (val _ <- r; val y <- loop(r)) yield y + + def loopWhile[a](cond: => boolean)(r: Responder[unit]): Responder[unit] = + if (cond) for (val _ <- r; val y <- loopWhile(cond)(r)) yield y + else constant(()) + } -/** instances of responder are the building blocks of small programs +/** Instances of responder are the building blocks of small programs * written in continuation passing style. By using responder classes * in for comprehensions, one can embed domain-specific languages in * Scala while giving the impression that programs in these DSLs are * written in direct style. * @since revision 6897 (will be 2.1.1) */ -abstract class Responder[a] { +abstract class Responder[+a] { - def answer(k: a => unit): unit + def foreach(k: a => unit): unit def map[b](f: a => b) = new Responder[b] { - def answer(k: b => unit): unit = - Responder.this.answer(x => k(f(x))) + def foreach(k: b => unit): unit = + Responder.this.foreach(x => k(f(x))) } def flatMap[b](f: a => Responder[b]) = new Responder[b] { - def answer(k: b => unit): unit = - Responder.this.answer(x => f(x).answer(k)) + def foreach(k: b => unit): unit = + Responder.this.foreach(x => f(x).foreach(k)) } def filter(p: a => boolean) = new Responder[a] { - def answer(k: a => unit): unit = - Responder.this.answer(x => if (p(x)) k(x) else ()) + def foreach(k: a => unit): unit = + Responder.this.foreach(x => if (p(x)) k(x) else ()) } + } + diff --git a/src/library/scala/collection/mutable/HashTable.scala b/src/library/scala/collection/mutable/HashTable.scala index 6654340fa0..81eb283fe0 100644 --- a/src/library/scala/collection/mutable/HashTable.scala +++ b/src/library/scala/collection/mutable/HashTable.scala @@ -106,7 +106,7 @@ trait HashTable[A] extends AnyRef { } } - private def entryFor(key: A) = (e: Entry => elemEquals(entryKey(e), key)); + private def entryFor(key: A) = { e: Entry => elemEquals(entryKey(e), key) } protected def initTable(tb: Array[List[Entry]]): Unit = { var i = tb.length - 1; diff --git a/src/library/scala/concurrent/Process.scala b/src/library/scala/concurrent/Process.scala index 24e1185353..537b6c120c 100644 --- a/src/library/scala/concurrent/Process.scala +++ b/src/library/scala/concurrent/Process.scala @@ -59,7 +59,7 @@ class Process(body: => Unit) extends Actor() { }; private def signal(s: MailBox#Message) = - links.foreach(p: Process => p.send(Triple('EXIT, this, s))); + links.foreach { p: Process => p.send(Triple('EXIT, this, s)) } def !(msg: MailBox#Message) = send(msg); |