Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

cleaned up code in library/scala/**/*.scala

  • Loading branch information...
commit 5a8391bb88f3dd65458a77539a1475eaa84cc434 1 parent 33637d5
michelou authored
Showing with 2,112 additions and 1,968 deletions.
  1. +12 −12 src/library/scala/Application.scala
  2. +38 −23 src/library/scala/Array.scala
  3. +7 −7 src/library/scala/BufferedIterator.scala
  4. +12 −7 src/library/scala/CaseClass.scala
  5. +2 −2 src/library/scala/Cell.scala
  6. +278 −275 src/library/scala/Console.scala
  7. +73 −74 src/library/scala/Enumeration.scala
  8. +101 −97 src/library/scala/Iterable.scala
  9. +82 −77 src/library/scala/IterableProxy.scala
  10. +187 −172 src/library/scala/Iterator.scala
  11. +10 −8 src/library/scala/MatchError.scala
  12. +5 −5 src/library/scala/Proxy.scala
  13. +30 −4 src/library/scala/Responder.scala
  14. +68 −66 src/library/scala/Seq.scala
  15. +81 −70 src/library/scala/SeqProxy.scala
  16. +19 −20 src/library/scala/Stream.scala
  17. +28 −28 src/library/scala/collection/BitSet.scala
  18. +38 −36 src/library/scala/collection/Map.scala
  19. +12 −12 src/library/scala/collection/MapProxy.scala
  20. +8 −8 src/library/scala/collection/Set.scala
  21. +9 −9 src/library/scala/collection/SetProxy.scala
  22. +5 −5 src/library/scala/collection/immutable/BitSet.scala
  23. +100 −96 src/library/scala/collection/immutable/ListMap.scala
  24. +68 −68 src/library/scala/collection/immutable/ListSet.scala
  25. +119 −110 src/library/scala/collection/immutable/Map.scala
  26. +22 −22 src/library/scala/collection/immutable/Queue.scala
  27. +61 −56 src/library/scala/collection/immutable/Set.scala
  28. +26 −26 src/library/scala/collection/immutable/Stack.scala
  29. +103 −106 src/library/scala/collection/immutable/Tree.scala
  30. +88 −80 src/library/scala/collection/immutable/TreeMap.scala
  31. +44 −41 src/library/scala/collection/immutable/TreeSet.scala
  32. +115 −109 src/library/scala/util/automata/BaseBerrySethi.scala
  33. +44 −28 src/library/scala/util/automata/DetWordAutom.scala
  34. +36 −30 src/library/scala/util/automata/Inclusion.scala
  35. +49 −49 src/library/scala/util/automata/NondetWordAutom.scala
  36. +132 −130 src/library/scala/util/automata/WordBerrySethi.scala
View
24 src/library/scala/Application.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala;
+package scala
/** The <code>Application</code> class can be used to quickly turn objects
@@ -38,16 +38,16 @@ package scala;
trait Application {
- /** The time when execution of this program started.
- */
- val executionStart: Long = java.lang.System.currentTimeMillis();
-
- /** The default main method.
- */
- def main(args: Array[String]) = {
- if (java.lang.System.getProperty("scala.time") != null)
- java.lang.System.out.println("[total " +
- (java.lang.System.currentTimeMillis()
- - executionStart) + "ms]");
+ /** The time when execution of this program started.
+ */
+ val executionStart: Long = java.lang.System.currentTimeMillis()
+
+ /** The default main method.
+ */
+ def main(args: Array[String]) = {
+ if (java.lang.System.getProperty("scala.time") != null) {
+ val total = java.lang.System.currentTimeMillis() - executionStart
+ java.lang.System.out.println("[total " + total + "ms]")
}
+ }
}
View
61 src/library/scala/Array.scala
@@ -8,31 +8,41 @@
// $Id$
+package scala
-package scala;
-
-
-import runtime._
-
+/** This object ...
+ *
+ * @author Martin Odersky
+ * @version 1.0
+ */
object Array {
- /** Copy one array to another
- * Equaivalent to System.arraycopy(src, srcPos, dest, destPos, length),
- * except that this works also for plymorphic and boxed arrays
+ /** Copy one array to another.
+ * Equivalent to
+ * <code>System.arraycopy(src, srcPos, dest, destPos, length)</code>,
+ * except that this works also for polymorphic and boxed arrays.
+ *
+ * @param src ...
+ * @param srcPos ...
+ * @param dest ...
+ * @param destPos ...
+ * @param length ...
*/
def copy(src: AnyRef, srcPos: Int, dest: AnyRef, destPos: Int, length: Int): Unit = src match {
- case xs: BoxedArray =>
+ case xs: runtime.BoxedArray =>
xs.copyTo(srcPos, dest, destPos, length)
case _ =>
dest match {
- case xs: BoxedArray =>
+ case xs: runtime.BoxedArray =>
xs.copyFrom(src, srcPos, destPos, length)
case _ =>
System.arraycopy(src, srcPos, dest, destPos, length)
}
}
- /** Concatenate all argument arrays into a single array
+ /** Concatenate all argument arrays into a single array.
+ *
+ * @param xs ...
*/
def concat[T](xs: Array[T]*) = {
var len = 0
@@ -55,22 +65,27 @@ object Array {
* @return the sorted array of all integers in range [from;end).
*/
def range(start: Int, end: Int): Array[Int] = {
- val result = new Array[Int](end - start);
- for (val i <- Iterator.range(start, end)) result(i - start) = i
+ val result = new Array[Int](end - start)
+ for (val i <- start until end) result(i - start) = i
result
}
}
+/** This class ...
+ *
+ * @author Martin Odersky
+ * @version 1.0
+ */
[cloneable,serializable]
final class Array[a](_length: Int) extends Seq[a] {
- def length: Int = throw new Error();
- def apply(i: Int): a = throw new Error();
- def update(i: Int, x: a): Unit = throw new Error();
- def elements: Iterator[a] = throw new Error();
- def subArray(from: Int, end: Int): Array[a] = throw new Error();
- def filter(p: a => Boolean): Array[a] = throw new Error();
- def map[b](f: a => b): Array[b] = throw new Error();
- def flatMap[b](f: a => Array[b]): Array[b] = throw new Error();
- def zip[b](that: Array[b]): Array[Tuple2[a,b]] = throw new Error();
- def zipWithIndex: Array[Tuple2[a,Int]] = throw new Error();
+ def length: Int = throw new Error()
+ def apply(i: Int): a = throw new Error()
+ def update(i: Int, x: a): Unit = throw new Error()
+ def elements: Iterator[a] = throw new Error()
+ def subArray(from: Int, end: Int): Array[a] = throw new Error()
+ def filter(p: a => Boolean): Array[a] = throw new Error()
+ def map[b](f: a => b): Array[b] = throw new Error()
+ def flatMap[b](f: a => Array[b]): Array[b] = throw new Error()
+ def zip[b](that: Array[b]): Array[Tuple2[a,b]] = throw new Error()
+ def zipWithIndex: Array[Tuple2[a,Int]] = throw new Error()
}
View
14 src/library/scala/BufferedIterator.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala;
+package scala
/** Buffered iterators are iterators which llow to inspect the next
@@ -20,11 +20,11 @@ package scala;
*/
trait BufferedIterator[+A] extends Iterator[A] {
- /** Checks what the next available element is.
- *
- * @return the current element
- */
- def head: A;
+ /** Checks what the next available element is.
+ *
+ * @return the current element
+ */
+ def head: A
- override def buffered: BufferedIterator[A] = this;
+ override def buffered: BufferedIterator[A] = this
}
View
19 src/library/scala/CaseClass.scala
@@ -9,28 +9,33 @@
// $Id$
-package scala;
+package scala
/** defines an access function for instances of case classes
*
* @author Burak Emir
+ * @version 1.0
*/
trait CaseClass extends AnyRef {
/** for a case class A(x_0,...,x_(k-1)), returns x_i for 0 &lt;= i &lt; k,
- ** null otherwise
- */
- def caseElement(n: Int): Any ;
+ * <code>null</code> otherwise.
+ *
+ * @param n ...
+ */
+ def caseElement(n: Int): Any
/** need also, for reflection
def setCaseElement(n: Int, v: Any): unit
*/
/** for a case class A(x_0,...,x_(k-1)), returns k
- */
- def caseArity: Int ;
+ */
+ def caseArity: Int
- def caseName: String ;
+ /**
+ */
+ def caseName: String
}
View
4 src/library/scala/Cell.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala;
+package scala
/** A <code>Cell</code> is a generic wrapper which completely
@@ -19,4 +19,4 @@ package scala;
* @author Martin Odersky
* @version 1.0, 08/08/2003
*/
-case class Cell[+T](elem: T);
+case class Cell[+T](elem: T)
View
553 src/library/scala/Console.scala
@@ -9,8 +9,9 @@
// $Id$
-package scala;
-import scala.util.Fluid;
+package scala
+
+import scala.util.Fluid
/** The <code>Console</code> object implements functionality for
@@ -22,283 +23,285 @@ import scala.util.Fluid;
* @version 1.0, 03/09/2003
*/
object Console {
- import java.io._;
- import java.text._;
-
- // ANSI colors foreground
- final val BLACK = "\033[30m";
- final val RED = "\033[31m";
- final val GREEN = "\033[32m";
- final val YELLOW = "\033[33m";
- final val BLUE = "\033[34m";
- final val MAGENTA = "\033[35m";
- final val CYAN = "\033[36m";
- final val WHITE = "\033[37m";
-
- // ANSI colors background
- final val BLACK_B = "\033[40m";
- final val RED_B = "\033[41m";
- final val GREEN_B = "\033[42m";
- final val YELLOW_B = "\033[43m";
- final val BLUE_B = "\033[44m";
- final val MAGENTA_B = "\033[45m";
- final val CYAN_B = "\033[46m";
- final val WHITE_B = "\033[47m";
-
- // ANSI styles
- final val RESET = "\033[0m";
- final val BOLD = "\033[1m";
- final val UNDERLINED = "\033[4m";
- final val BLINK = "\033[5m";
- final val REVERSED = "\033[7m";
- final val INVISIBLE = "\033[8m";
-
- private val outFluid = new Fluid[PrintStream](java.lang.System.out);
- private val inFluid = new Fluid[BufferedReader](
- new BufferedReader(new InputStreamReader(java.lang.System.in)));
-
- private def out = outFluid.value
- private def in = inFluid.value
-
- /** Set the default output stream.
- *
- * @param out the new output stream.
- */
- def setOut(out: PrintStream): Unit = outFluid.value = out
-
- /** Set the default output stream for the duration
- * of execution of one thunk.
- *
- * @param out the new output stream.
- * @param thunk the code to execute with
- * the new output stream active
- */
- def withOut[T](out: PrintStream)(thunk: =>T): T =
- outFluid.withValue(out)(thunk)
-
- /** Set the default output stream.
- *
- * @param@ out the new output stream.
- */
- def setOut(out: OutputStream): Unit =
- setOut(new PrintStream(out))
-
- /** Set the default output stream for the duration
- * of execution of one thunk.
- *
- * @param out the new output stream.
- * @param thunk the code to execute with
- * the new output stream active
- */
- def withOut[T](out: OutputStream)(thunk: =>T): T =
- withOut(new PrintStream(out))(thunk)
-
-
- /** Set the default input stream.
- *
- * @param reader specifies the new input stream.
- */
- def setIn(reader: Reader): Unit = {
- inFluid.value = new BufferedReader(reader)
- }
-
- /** Set the default input stream for the duration
- * of execution of one thunk.
- *
- * @param in the new input stream.
- * @param thunk the code to execute with
- * the new input stream active
- */
+ import java.io._
+ import java.text._
+
+ // ANSI colors foreground
+ final val BLACK = "\033[30m"
+ final val RED = "\033[31m"
+ final val GREEN = "\033[32m"
+ final val YELLOW = "\033[33m"
+ final val BLUE = "\033[34m"
+ final val MAGENTA = "\033[35m"
+ final val CYAN = "\033[36m"
+ final val WHITE = "\033[37m"
+
+ // ANSI colors background
+ final val BLACK_B = "\033[40m"
+ final val RED_B = "\033[41m"
+ final val GREEN_B = "\033[42m"
+ final val YELLOW_B = "\033[43m"
+ final val BLUE_B = "\033[44m"
+ final val MAGENTA_B = "\033[45m"
+ final val CYAN_B = "\033[46m"
+ final val WHITE_B = "\033[47m"
+
+ // ANSI styles
+ final val RESET = "\033[0m"
+ final val BOLD = "\033[1m"
+ final val UNDERLINED = "\033[4m"
+ final val BLINK = "\033[5m"
+ final val REVERSED = "\033[7m"
+ final val INVISIBLE = "\033[8m"
+
+ private val outFluid = new Fluid[PrintStream](java.lang.System.out)
+ private val inFluid = new Fluid[BufferedReader](
+ new BufferedReader(new InputStreamReader(java.lang.System.in)))
+
+ private def out = outFluid.value
+ private def in = inFluid.value
+
+ /** Set the default output stream.
+ *
+ * @param out the new output stream.
+ */
+ def setOut(out: PrintStream): Unit = outFluid.value = out
+
+ /** Set the default output stream for the duration
+ * of execution of one thunk.
+ *
+ * @param out the new output stream.
+ * @param thunk the code to execute with
+ * the new output stream active
+ * @return ...
+ */
+ def withOut[T](out: PrintStream)(thunk: =>T): T =
+ outFluid.withValue(out)(thunk)
+
+ /** Set the default output stream.
+ *
+ * @param@ out the new output stream.
+ */
+ def setOut(out: OutputStream): Unit =
+ setOut(new PrintStream(out))
+
+ /** Set the default output stream for the duration
+ * of execution of one thunk.
+ *
+ * @param out the new output stream.
+ * @param thunk the code to execute with
+ * the new output stream active
+ * @return ...
+ */
+ def withOut[T](out: OutputStream)(thunk: =>T): T =
+ withOut(new PrintStream(out))(thunk)
+
+
+ /** Set the default input stream.
+ *
+ * @param reader specifies the new input stream.
+ */
+ def setIn(reader: Reader): Unit = {
+ inFluid.value = new BufferedReader(reader)
+ }
+
+ /** Set the default input stream for the duration
+ * of execution of one thunk.
+ *
+ * @param in the new input stream.
+ * @param thunk the code to execute with
+ * the new input stream active
+ */
def withIn[T](reader: Reader)(thunk: =>T): T =
inFluid.withValue(new BufferedReader(reader))(thunk)
- /** Set the default input stream.
- *
- * @param in the new input stream.
- */
- def setIn(in: InputStream): Unit =
- setIn(new InputStreamReader(in))
-
- /** Set the default input stream for the duration
- * of execution of one thunk.
- *
- * @param in the new input stream.
- * @param thunk the code to execute with
- * the new input stream active
- */
- def withIn[T](in: InputStream)(thunk: =>T): T =
- withIn(new InputStreamReader(in))(thunk)
-
- /** Print an object on the terminal.
- *
- * @param obj the object to print.
- */
- def print(obj: Any): Unit = {
- if (obj == null)
- out.print("null");
- else
- out.print(obj.toString());
- }
-
- /** Flush the output stream. This function is required when partial
- * output (i.e. output not terminated by a new line character) has
- * to be made visible on the terminal.
- */
- def flush: Unit = out.flush();
-
- /** Print a new line character on the terminal.
- */
- def println: Unit = {
- out.println();
- }
-
- /** Print out an object followed by a new line character.
- *
- * @param x the object to print.
- */
- def println(x: Any): Unit = {
- out.println(x);
- }
-
- /** Format and print out some text (in a fashion similar to printf in C).
- * The format of the text to print is specified by the parameter
- * <code>text</code>. The arguments that are inserted into specific
- * locations in <code>text</code> are provided with parameter
- * <code>args</code>. See class <code>java.text.MessageFormat</code>
- * for a full specification of the format syntax.
- *
- * @param text the format of the text to print out.
- * @param args the parameters used to instantiate the format.
- */
- def printf(text: String)(args: Any*): Unit = {
- // todo: Uncurry
- if (text == null)
- out.print("null");
- else
- out.print(MessageFormat.format(text, textParams(args)));
+ /** Set the default input stream.
+ *
+ * @param in the new input stream.
+ */
+ def setIn(in: InputStream): Unit =
+ setIn(new InputStreamReader(in))
+
+ /** Set the default input stream for the duration
+ * of execution of one thunk.
+ *
+ * @param in the new input stream.
+ * @param thunk the code to execute with
+ * the new input stream active
+ */
+ def withIn[T](in: InputStream)(thunk: =>T): T =
+ withIn(new InputStreamReader(in))(thunk)
+
+ /** Print an object on the terminal.
+ *
+ * @param obj the object to print.
+ */
+ def print(obj: Any): Unit =
+ out.print(if (obj == null) "null" else obj.toString())
+
+ /** Flush the output stream. This function is required when partial
+ * output (i.e. output not terminated by a new line character) has
+ * to be made visible on the terminal.
+ */
+ def flush: Unit = out.flush()
+
+ /** Print a new line character on the terminal.
+ */
+ def println: Unit = out.println()
+
+ /** Print out an object followed by a new line character.
+ *
+ * @param x the object to print.
+ */
+ def println(x: Any): Unit = out.println(x)
+
+ /** Format and print out some text (in a fashion similar to printf in C).
+ * The format of the text to print is specified by the parameter
+ * <code>text</code>. The arguments that are inserted into specific
+ * locations in <code>text</code> are provided with parameter
+ * <code>args</code>. See class <code>java.text.MessageFormat</code>
+ * for a full specification of the format syntax.
+ *
+ * @param text the format of the text to print out.
+ * @param args the parameters used to instantiate the format.
+ */
+ // todo: Uncurry
+ def printf(text: String)(args: Any*): Unit =
+ out.print(
+ if (text == null) "null"
+ else MessageFormat.format(text, textParams(args))
+ )
+
+ /** Read a full line from the terminal.
+ *
+ * @return the string read from the terminal.
+ */
+ def readLine: String = in.readLine()
+
+ /** Read a boolean value from the terminal.
+ *
+ * @return the boolean value read from the terminal.
+ */
+ def readBoolean: Boolean = in.readLine().toLowerCase() match {
+ case "true" => true
+ case "t" => true
+ case "yes" => true
+ case "y" => true
+ case _ => false
+ }
+
+ /** Read a byte value from the terminal.
+ */
+ def readByte: Byte = java.lang.Byte.decode(in.readLine()).byteValue()
+
+ /** Read a short value from the terminal.
+ */
+ def readShort: Short = java.lang.Short.decode(in.readLine()).shortValue()
+
+ /** Read a char value from the terminal.
+ */
+ def readChar: Char = in.readLine().charAt(0)
+
+ /** Read an int value from the terminal.
+ */
+ def readInt: Int = java.lang.Integer.decode(in.readLine()).intValue()
+
+ /** Read a float value from the terminal.
+ */
+ def readFloat: Float =
+ scala.runtime.compat.Platform.parseFloat(in.readLine())
+
+ /** Read a double value from the terminal.
+ */
+ def readDouble: Double =
+ scala.runtime.compat.Platform.parseDouble(in.readLine())
+
+ /** Read in some structured input, specified by a format specifier.
+ * See class <code>java.text.MessageFormat</code> for details of
+ * the format specification.
+ *
+ * @param format the format of the input.
+ * @return a list of all extracted values.
+ */
+ def readf(format: String): List[Any] =
+ textComponents(new MessageFormat(format).parse(in.readLine()))
+
+ /** Read in some structured input, specified by a format specifier.
+ * Opposed to <code>readf</code>, this function only returns the
+ * first value extracted from the input according to the format
+ * specification.
+ *
+ * @param format ...
+ * @return ...
+ */
+ def readf1(format: String): Any = readf(format).head
+
+ /** Read in some structured input, specified by a format specifier.
+ * Opposed to <code>readf</code>, this function only returns the
+ * first two values extracted from the input according to the format
+ * specification.
+ *
+ * @param format ...
+ * @return ...
+ */
+ def readf2(format: String): Pair[Any, Any] = {
+ val res = readf(format)
+ Pair(res.head, res.tail.head)
+ }
+
+ /** Read in some structured input, specified by a format specifier.
+ * Opposed to <code>readf</code>, this function only returns the
+ * first three values extracted from the input according to the format
+ * specification.
+ *
+ * @param format ...
+ * @return ...
+ */
+ def readf3(format: String): Triple[Any, Any, Any] = {
+ val res = readf(format)
+ Triple(res.head, res.tail.head, res.tail.tail.head)
+ }
+
+ private def textComponents(a: Array[AnyRef]): List[Any] = {
+ var i: Int = a.length - 1
+ var res: List[Any] = Nil
+ while (i >= 0) {
+ res = (a(i) match {
+ case x: java.lang.Boolean => x.booleanValue()
+ case x: java.lang.Byte => x.byteValue()
+ case x: java.lang.Short => x.shortValue()
+ case x: java.lang.Character => x.charValue()
+ case x: java.lang.Integer => x.intValue()
+ case x: java.lang.Long => x.longValue()
+ case x: java.lang.Float => x.floatValue()
+ case x: java.lang.Double => x.doubleValue()
+ case x => x
+ }) :: res;
+ i = i - 1
}
-
- /** Read a full line from the terminal.
- *
- * @return the string read from the terminal.
- */
- def readLine: String = in.readLine();
-
- /** Read a boolean value from the terminal.
- *
- * @return the boolean value read from the terminal.
- */
- def readBoolean: Boolean = in.readLine().toLowerCase() match {
- case "true" => true
- case "t" => true
- case "yes" => true
- case "y" => true
- case _ => false
- }
-
- /** Read a byte value from the terminal.
- */
- def readByte: Byte = java.lang.Byte.decode(in.readLine()).byteValue();
-
- /** Read a short value from the terminal.
- */
- def readShort: Short = java.lang.Short.decode(in.readLine()).shortValue();
-
- /** Read a char value from the terminal.
- */
- def readChar: Char = in.readLine().charAt(0);
-
- /** Read an int value from the terminal.
- */
- def readInt: Int = java.lang.Integer.decode(in.readLine()).intValue();
-
- /** Read a float value from the terminal.
- */
- def readFloat: Float =
- scala.runtime.compat.Platform.parseFloat(in.readLine());
-
- /** Read a double value from the terminal.
- */
- def readDouble: Double =
- scala.runtime.compat.Platform.parseDouble(in.readLine());
-
- /** Read in some structured input, specified by a format specifier.
- * See class <code>java.text.MessageFormat</code> for details of
- * the format specification.
- *
- * @param format the format of the input.
- * @return a list of all extracted values.
- */
- def readf(format: String): List[Any] =
- textComponents(new MessageFormat(format).parse(in.readLine()));
-
- /** Read in some structured input, specified by a format specifier.
- * Opposed to <code>readf</code>, this function only returns the
- * first value extracted from the input according to the format
- * specification.
- */
- def readf1(format: String): Any = readf(format).head;
-
- /** Read in some structured input, specified by a format specifier.
- * Opposed to <code>readf</code>, this function only returns the
- * first two values extracted from the input according to the format
- * specification.
- */
- def readf2(format: String): Pair[Any, Any] = {
- val res = readf(format);
- Pair(res.head, res.tail.head)
- }
-
- /** Read in some structured input, specified by a format specifier.
- * Opposed to <code>readf</code>, this function only returns the
- * first three values extracted from the input according to the format
- * specification.
- */
- def readf3(format: String): Triple[Any, Any, Any] = {
- val res = readf(format);
- Triple(res.head, res.tail.head, res.tail.tail.head)
- }
-
- private def textComponents(a: Array[AnyRef]): List[Any] = {
- var i: Int = a.length - 1;
- var res: List[Any] = Nil;
- while (i >= 0) {
- res = (a(i) match {
- case x: java.lang.Boolean => x.booleanValue()
- case x: java.lang.Byte => x.byteValue()
- case x: java.lang.Short => x.shortValue()
- case x: java.lang.Character => x.charValue()
- case x: java.lang.Integer => x.intValue()
- case x: java.lang.Long => x.longValue()
- case x: java.lang.Float => x.floatValue()
- case x: java.lang.Double => x.doubleValue()
- case x => x
- }) :: res;
- i = i - 1;
- }
- res
- }
-
- private def textParams(s: Seq[Any]): Array[AnyRef] = {
- val res = new Array[AnyRef](s.length);
- var i: Int = 0;
- val iter = s.elements;
- while (iter.hasNext) {
- res(i) = iter.next match {
- case x: Boolean => new java.lang.Boolean(x);
- case x: Byte => new java.lang.Byte(x);
- case x: Short => new java.lang.Short(x);
- case x: Char => new java.lang.Character(x);
- case x: Int => new java.lang.Integer(x);
- case x: Long => new java.lang.Long(x);
- case x: Float => new java.lang.Float(x);
- case x: Double => new java.lang.Double(x);
- case x: Unit => "()";
- case x: AnyRef => x;
- }
- i = i + 1;
- }
- res
+ res
+ }
+
+ private def textParams(s: Seq[Any]): Array[AnyRef] = {
+ val res = new Array[AnyRef](s.length);
+ var i: Int = 0;
+ val iter = s.elements;
+ while (iter.hasNext) {
+ res(i) = iter.next match {
+ case x: Boolean => new java.lang.Boolean(x)
+ case x: Byte => new java.lang.Byte(x)
+ case x: Short => new java.lang.Short(x)
+ case x: Char => new java.lang.Character(x)
+ case x: Int => new java.lang.Integer(x)
+ case x: Long => new java.lang.Long(x)
+ case x: Float => new java.lang.Float(x)
+ case x: Double => new java.lang.Double(x)
+ case x: Unit => "()"
+ case x: AnyRef => x
+ }
+ i = i + 1
}
+ res
+ }
}
View
147 src/library/scala/Enumeration.scala
@@ -9,10 +9,10 @@
// $Id$
-package scala;
+package scala
-import scala.collection.mutable._;
+import scala.collection.mutable._
/**
* <p>The class <code>Enumeration</code> provides the same functionality as the
@@ -40,100 +40,99 @@ import scala.collection.mutable._;
*/
abstract class Enumeration(initial: Int, names: String*) {
- def this() = this(0, null);
+ def this() = this(0, null)
- def this(names: String*) = this(0, names: _*);
+ def this(names: String*) = this(0, names: _*)
- def name = {
- val cname = scala.runtime.compat.Platform.getClassName(this);
- if (cname.endsWith("$"))
- cname.substring(0, cname.length() - 1);
- else if (cname.endsWith("$class"))
- cname.substring(0, cname.length() - 6);
- else
- cname;
- }
+ def name = {
+ val cname = scala.runtime.compat.Platform.getClassName(this)
+ if (cname.endsWith("$"))
+ cname.substring(0, cname.length() - 1)
+ else if (cname.endsWith("$class"))
+ cname.substring(0, cname.length() - 6)
+ else
+ cname
+ }
- /**
- * A mapping between the enumeration value id and the enumeration
- * object.
- */
- private var values: Map[Int, Value] = new HashMap;
+ /**
+ * A mapping between the enumeration value id and the enumeration
+ * object.
+ */
+ private var values: Map[Int, Value] = new HashMap
- /**
- * A cache listing all values of this enumeration.
- */
- private var vcache: List[Value] = null;
+ /**
+ * A cache listing all values of this enumeration.
+ */
+ private var vcache: List[Value] = null
- private def updateCache: List[Value] =
- if (vcache == null) {
- vcache = values.values.toList.sort((p1, p2) => p1.id < p2.id);
- vcache
- } else
- vcache;
+ private def updateCache: List[Value] =
+ if (vcache == null) {
+ vcache = values.values.toList.sort((p1, p2) => p1.id < p2.id);
+ vcache
+ } else
+ vcache;
- protected var nextId = initial;
+ protected var nextId = initial
- protected var nextName = names.elements;
+ protected var nextName = names.elements
- private var topId = initial;
+ private var topId = initial
- final def maxId = topId;
+ final def maxId = topId
- /**
- * Returns the enumeration value for the given id.
- */
- final def apply(x: Int): Value = values(x);
+ /**
+ * Returns the enumeration value for the given id.
+ */
+ final def apply(x: Int): Value = values(x)
- /**
- * Returns all values of this enumeration.
- */
- final def elements: Iterator[Value] = updateCache.elements;
+ /**
+ * Returns all values of this enumeration.
+ */
+ final def elements: Iterator[Value] = updateCache.elements
- def foreach(f: Value => Unit): Unit = elements foreach f;
+ def foreach(f: Value => Unit): Unit = elements foreach f
- def forall(p: Value => Boolean): Boolean = elements forall p;
+ def forall(p: Value => Boolean): Boolean = elements forall p
- def exists(p: Value => Boolean): Boolean = elements exists p;
+ def exists(p: Value => Boolean): Boolean = elements exists p
- def map[b](f: Value => b): Iterator[b] = elements map f;
+ def map[b](f: Value => b): Iterator[b] = elements map f
- def flatMap[b](f: Value => Iterator[b]): Iterator[b] = elements flatMap f;
+ def flatMap[b](f: Value => Iterator[b]): Iterator[b] = elements flatMap f
- def filter(p: Value => Boolean): Iterator[Value] = elements filter p;
+ def filter(p: Value => Boolean): Iterator[Value] = elements filter p
- override def toString(): String = updateCache.mkString("{", ", ", "}");
+ override def toString(): String = updateCache.mkString("{", ", ", "}")
- protected final def Value: Value =
- new Val(nextId, if (nextName.hasNext) nextName.next else null);
+ protected final def Value: Value =
+ new Val(nextId, if (nextName.hasNext) nextName.next else null)
- protected final def Value(i: Int): Value =
- new Val(i, if (nextName.hasNext) nextName.next else null);
+ protected final def Value(i: Int): Value =
+ new Val(i, if (nextName.hasNext) nextName.next else null)
- protected final def Value(name: String): Value = new Val(nextId, name);
+ protected final def Value(name: String): Value = new Val(nextId, name)
- protected final def Value(i: Int, name: String): Value = new Val(i, name);
+ protected final def Value(i: Int, name: String): Value = new Val(i, name)
- abstract class Value extends Ordered[Value] {
- def id: Int;
- override def compare(that: Value): Int = this.id - that.id
- }
+ abstract class Value extends Ordered[Value] {
+ def id: Int
+ override def compare(that: Value): Int = this.id - that.id
+ }
- protected class Val(i: Int, name: String) extends Value {
- def this(i: Int) =
- this(i, if (nextName.hasNext) nextName.next else i.toString());
- def this(name: String) = this(nextId, name);
- def this() =
- this(nextId, if (nextName.hasNext) nextName.next else nextId.toString());
- assert(!values.isDefinedAt(i));
- values(i) = this;
- nextId = i + 1;
- if (nextId > topId)
- topId = nextId;
- def id = i;
- override def toString() = if (name == null)
- Enumeration.this.name + "(" + i + ")";
- else
- name;
- }
+ protected class Val(i: Int, name: String) extends Value {
+ def this(i: Int) =
+ this(i, if (nextName.hasNext) nextName.next else i.toString())
+ def this(name: String) = this(nextId, name)
+ def this() =
+ this(nextId, if (nextName.hasNext) nextName.next else nextId.toString())
+ assert(!values.isDefinedAt(i))
+ values(i) = this
+ nextId = i + 1
+ if (nextId > topId)
+ topId = nextId
+ def id = i
+ override def toString() =
+ if (name == null) Enumeration.this.name + "(" + i + ")"
+ else name
+ }
}
View
198 src/library/scala/Iterable.scala
@@ -9,10 +9,14 @@
// $Id$
-package scala;
+package scala
-import Predef.error;
+/** This object ...
+ *
+ * @author Matthias Zenger
+ * @version 1.1, 04/02/2004
+ */
object Iterable {
/*
implicit def view[A <% Ordered[A]](x: Iterable[A]): Ordered[Iterable[A]] =
@@ -35,11 +39,11 @@ object Iterable {
*/
/** The minimum element of a non-empty sequence of ordered elements */
def min[A <% Ordered[A]](seq: Iterable[A]): A = {
- val xs = seq.elements;
+ val xs = seq.elements
if (!xs.hasNext) error("min(<empty>)");
- var min = xs.next;
+ var min = xs.next
while (xs.hasNext) {
- val x = xs.next;
+ val x = xs.next
if (x < min) min = x;
}
min
@@ -47,11 +51,11 @@ object Iterable {
/** The maximum element of a non-empty sequence of ordered elements */
def max[A <% Ordered[A]](seq: Iterable[A]): A = {
- val xs = seq.elements;
+ val xs = seq.elements
if (!xs.hasNext) error("max(<empty>)");
- var max = xs.next;
+ var max = xs.next
while (xs.hasNext) {
- val x = xs.next;
+ val x = xs.next
if (max < x) max = x;
}
max
@@ -68,95 +72,95 @@ object Iterable {
*/
trait Iterable[+A] {
- /** Creates a new iterator over all elements contained in this
- * object.
- *
- * @return the new iterator
- */
- def elements: Iterator[A];
-
- /** Concatenates two iterable objects
- *
- * @return the new iterable object
- * @author buraq
- */
- def concat[B >: A](that:Iterable[B]): Iterable[B] = new Iterable[B] {
- def elements: Iterator[B] = Iterable.this.elements.append(that.elements);
- }
+ /** Creates a new iterator over all elements contained in this
+ * object.
+ *
+ * @return the new iterator
+ */
+ def elements: Iterator[A]
+
+ /** Concatenates two iterable objects
+ *
+ * @return the new iterable object
+ * @author buraq
+ */
+ def concat[B >: A](that: Iterable[B]): Iterable[B] = new Iterable[B] {
+ def elements: Iterator[B] = Iterable.this.elements.append(that.elements)
+ }
- /** Apply a function <code>f</code> to all elements of this
- * iterable object.
- *
- * @param f a function that is applied to every element.
- */
- def foreach(f: A => Unit): Unit = elements.foreach(f);
-
- /** Apply a predicate <code>p</code> to all elements of this
- * iterable object and return true, iff the predicate yields
- * true for all elements.
- *
- * @param p the predicate
- * @returns true, iff the predicate yields true for all elements.
- */
- def forall(p: A => Boolean): Boolean = elements.forall(p);
-
- /** Apply a predicate <code>p</code> to all elements of this
- * iterable object and return true, iff there is at least one
- * element for which <code>p</code> yields true.
- *
- * @param p the predicate
- * @returns true, iff the predicate yields true for at least one element.
- */
- def exists(p: A => Boolean): Boolean = elements.exists(p);
-
- /** Find and return the first element of the iterable object satisfying a
- * predicate, if any.
- *
- * @param p the predicate
- * @return the first element in the iterable object satisfying <code>p</code>,
- * or <code>None</code> if none exists.
- */
- def find(p: A => Boolean): Option[A] = elements.find(p);
-
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from left to right, and starting with
- * the value <code>z</code>.
- * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
- * is <code>List(a0, a1, ..., an)</code>.
- */
- def foldLeft[B](z: B)(op: (B, A) => B): B = elements.foldLeft(z)(op);
-
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from rigth to left, and starting with
- * the value <code>z</code>.
- * @return <code>a0 op (... op (an op z)...)</code> if the list
- * is <code>[a0, a1, ..., an]</code>.
- */
- def foldRight[B](z: B)(op: (A, B) => B): B = elements.foldRight(z)(op);
-
- /** Similar to <code>foldLeft</code> but can be used as
- * an operator with the order of list and zero arguments reversed.
- * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
- */
- def /:[B](z: B)(f: (B, A) => B): B = foldLeft(z)(f);
-
- /** An alias for <code>foldRight</code>.
- * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
- */
- def :\[B](z: B)(f: (A, B) => B): B = foldRight(z)(f);
-
- /** Checks if the other iterable object contains the same elements.
- *
- * @param that the other iterable object
- * @return true, iff both iterable objects contain the same elements.
- */
- def sameElements[B >: A](that: Iterable[B]): Boolean = {
- val ita = this.elements;
- val itb = that.elements;
- var res = true;
- while (res && ita.hasNext && itb.hasNext) {
- res = (ita.next == itb.next);
- }
- !ita.hasNext && !itb.hasNext && res
+ /** Apply a function <code>f</code> to all elements of this
+ * iterable object.
+ *
+ * @param f a function that is applied to every element.
+ */
+ def foreach(f: A => Unit): Unit = elements.foreach(f)
+
+ /** Apply a predicate <code>p</code> to all elements of this
+ * iterable object and return true, iff the predicate yields
+ * true for all elements.
+ *
+ * @param p the predicate
+ * @returns true, iff the predicate yields true for all elements.
+ */
+ def forall(p: A => Boolean): Boolean = elements.forall(p)
+
+ /** Apply a predicate <code>p</code> to all elements of this
+ * iterable object and return true, iff there is at least one
+ * element for which <code>p</code> yields true.
+ *
+ * @param p the predicate
+ * @returns true, iff the predicate yields true for at least one element.
+ */
+ def exists(p: A => Boolean): Boolean = elements.exists(p)
+
+ /** Find and return the first element of the iterable object satisfying a
+ * predicate, if any.
+ *
+ * @param p the predicate
+ * @return the first element in the iterable object satisfying <code>p</code>,
+ * or <code>None</code> if none exists.
+ */
+ def find(p: A => Boolean): Option[A] = elements.find(p)
+
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from left to right, and starting with
+ * the value <code>z</code>.
+ * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
+ * is <code>List(a0, a1, ..., an)</code>.
+ */
+ def foldLeft[B](z: B)(op: (B, A) => B): B = elements.foldLeft(z)(op)
+
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from rigth to left, and starting with
+ * the value <code>z</code>.
+ * @return <code>a0 op (... op (an op z)...)</code> if the list
+ * is <code>[a0, a1, ..., an]</code>.
+ */
+ def foldRight[B](z: B)(op: (A, B) => B): B = elements.foldRight(z)(op)
+
+ /** Similar to <code>foldLeft</code> but can be used as
+ * an operator with the order of list and zero arguments reversed.
+ * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
+ */
+ def /:[B](z: B)(f: (B, A) => B): B = foldLeft(z)(f)
+
+ /** An alias for <code>foldRight</code>.
+ * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
+ */
+ def :\[B](z: B)(f: (A, B) => B): B = foldRight(z)(f)
+
+ /** Checks if the other iterable object contains the same elements.
+ *
+ * @param that the other iterable object
+ * @return true, iff both iterable objects contain the same elements.
+ */
+ def sameElements[B >: A](that: Iterable[B]): Boolean = {
+ val ita = this.elements
+ val itb = that.elements
+ var res = true
+ while (res && ita.hasNext && itb.hasNext) {
+ res = (ita.next == itb.next)
}
+ !ita.hasNext && !itb.hasNext && res
+ }
}
View
159 src/library/scala/IterableProxy.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala;
+package scala
/** This class implements a proxy for iterable objects. It forwards
@@ -20,80 +20,85 @@ package scala;
*/
trait IterableProxy[+A] extends Iterable[A] with Proxy {
- def self: Iterable[A];
-
- /** Creates a new iterator over all elements contained in this
- * object.
- *
- * @return the new iterator
- */
- def elements: Iterator[A] = self.elements;
-
- /** Apply a function <code>f</code> to all elements of this
- * iterable object.
- *
- * @param f a function that is applied to every element.
- */
- override def foreach(f: A => Unit): Unit = self.foreach(f);
-
- /** Apply a predicate <code>p</code> to all elements of this
- * iterable object and return true, iff the predicate yields
- * true for all elements.
- *
- * @param p the predicate
- * @returns true, iff the predicate yields true for all elements.
- */
- override def forall(p: A => Boolean): Boolean = self.forall(p);
-
- /** Apply a predicate <code>p</code> to all elements of this
- * iterable object and return true, iff there is at least one
- * element for which <code>p</code> yields true.
- *
- * @param p the predicate
- * @returns true, iff the predicate yields true for at least one element.
- */
- override def exists(p: A => Boolean): Boolean = self.exists(p);
-
- /** Find and return the first element of the iterable object satisfying a
- * predicate, if any.
- *
- * @param p the predicate
- * @return the first element in the iterable object satisfying <code>p</code>,
- * or <code>None</code> if none exists.
- */
- override def find(p: A => Boolean): Option[A] = self.find(p);
-
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from left to right, and starting with
- * the value <code>z</code>.
- * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
- * is <code>List(a0, a1, ..., an)</code>.
- */
- override def foldLeft[B](z: B)(op: (B, A) => B): B = self.foldLeft(z)(op);
-
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from rigth to left, and starting with
- * the value <code>z</code>.
- * @return <code>a0 op (... op (an op z)...)</code> if the list
- * is <code>[a0, a1, ..., an]</code>.
- */
- override def foldRight[B](z: B)(op: (A, B) => B): B = self.foldRight(z)(op);
-
- /** Similar to <code>foldLeft</code> but can be used as
- * an operator with the order of list and zero arguments reversed.
- * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
- */
- override def /:[B](z: B)(f: (B, A) => B): B = self./:(z)(f);
-
- /** An alias for <code>foldRight</code>.
- * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
- */
- override def :\[B](z: B)(f: (A, B) => B): B = self.:\(z)(f);
-
- /** Checks if the other iterable object contains the same elements.
- *
- * @param that the other iterable object
- * @return true, iff both iterable objects contain the same elements.
- */
- override def sameElements[B >: A](that: Iterable[B]): Boolean = self.sameElements(that);
+ def self: Iterable[A]
+
+ /** Creates a new iterator over all elements contained in this
+ * object.
+ *
+ * @return the new iterator
+ */
+ def elements: Iterator[A] = self.elements
+
+ /** Apply a function <code>f</code> to all elements of this
+ * iterable object.
+ *
+ * @param f a function that is applied to every element.
+ */
+ override def foreach(f: A => Unit): Unit = self.foreach(f)
+
+ /** Apply a predicate <code>p</code> to all elements of this
+ * iterable object and return true, iff the predicate yields
+ * true for all elements.
+ *
+ * @param p the predicate
+ * @returns true, iff the predicate yields true for all elements.
+ */
+ override def forall(p: A => Boolean): Boolean = self.forall(p)
+
+ /** Apply a predicate <code>p</code> to all elements of this
+ * iterable object and return true, iff there is at least one
+ * element for which <code>p</code> yields true.
+ *
+ * @param p the predicate
+ * @returns true, iff the predicate yields true for at least one element.
+ */
+ override def exists(p: A => Boolean): Boolean = self.exists(p)
+
+ /** Find and return the first element of the iterable object satisfying a
+ * predicate, if any.
+ *
+ * @param p the predicate
+ * @return the first element in the iterable object satisfying <code>p</code>,
+ * or <code>None</code> if none exists.
+ */
+ override def find(p: A => Boolean): Option[A] = self.find(p)
+
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from left to right, and starting with
+ * the value <code>z</code>.
+ * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
+ * is <code>List(a0, a1, ..., an)</code>.
+ */
+ override def foldLeft[B](z: B)(op: (B, A) => B): B = self.foldLeft(z)(op)
+
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from rigth to left, and starting with
+ * the value <code>z</code>.
+ * @return <code>a0 op (... op (an op z)...)</code> if the list
+ * is <code>[a0, a1, ..., an]</code>.
+ */
+ override def foldRight[B](z: B)(op: (A, B) => B): B = self.foldRight(z)(op)
+
+ /** Similar to <code>foldLeft</code> but can be used as
+ * an operator with the order of list and zero arguments reversed.
+ * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
+ */
+ override def /:[B](z: B)(f: (B, A) => B): B = self./:(z)(f)
+
+ /** An alias for <code>foldRight</code>.
+ * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
+ *
+ * @param z ...
+ * @param f ...
+ * @return ...
+ */
+ override def :\[B](z: B)(f: (A, B) => B): B = self.:\(z)(f)
+
+ /** Checks if the other iterable object contains the same elements.
+ *
+ * @param that the other iterable object
+ * @return true, iff both iterable objects contain the same elements.
+ */
+ override def sameElements[B >: A](that: Iterable[B]): Boolean =
+ self.sameElements(that)
}
View
359 src/library/scala/Iterator.scala
@@ -9,10 +9,10 @@
// $Id$
-package scala;
+package scala
-import Predef._;
+import Predef._
/** The <code>Iterator</code> object provides various functions for
* creating specialized iterators.
@@ -24,45 +24,53 @@ import Predef._;
object Iterator {
def empty[a] = new Iterator[a] {
- def hasNext: Boolean = false;
- def next: a = Predef.error("next on empty iterator");
+ def hasNext: Boolean = false
+ def next: a = Predef.error("next on empty iterator")
}
def single[a](x: a) = new Iterator[a] {
- private var hasnext = true;
- def hasNext: Boolean = hasnext;
- def next: a = if (hasnext) { hasnext = false; x } else Predef.error("next on empty iterator");
+ private var hasnext = true
+ def hasNext: Boolean = hasnext
+ def next: a =
+ if (hasnext) { hasnext = false; x }
+ else Predef.error("next on empty iterator")
}
- def fromValues[a](xs: a*) = xs.elements;
+ def fromValues[a](xs: a*) = xs.elements
def fromArray[a](xs: Array[a]): Iterator[a] =
- fromArray(xs, 0, xs.length);
+ fromArray(xs, 0, xs.length)
+ /**
+ * @param xs ...
+ * @param start ...
+ * @param length ...
+ * @return ...
+ */
def fromArray[a](xs: Array[a], start: Int, length: Int): Iterator[a] =
new BufferedIterator[a] {
- private var i = start;
- val end = if ((start + length) < xs.length) start else xs.length;
- def hasNext: Boolean = i < end;
+ private var i = start
+ val end = if ((start + length) < xs.length) start else xs.length
+ def hasNext: Boolean = i < end
def next: a = if (hasNext) { val x = xs(i) ; i = i + 1 ; x }
- else Predef.error("next on empty iterator");
+ else Predef.error("next on empty iterator")
def head: a = if (hasNext) xs(i);
- else Predef.error("head on empty iterator");
+ else Predef.error("head on empty iterator")
}
def fromString(str: String): Iterator[Char] =
new BufferedIterator[Char] {
- private var i = 0;
- private val len = str.length();
- def hasNext = i < len;
- def next = { val c = str charAt i; i = i + 1; c };
- def head = str charAt i;
+ private var i = 0
+ private val len = str.length()
+ def hasNext = i < len
+ def next = { val c = str charAt i; i = i + 1; c }
+ def head = str charAt i
}
def fromCaseClass(n:CaseClass): Iterator[Any] = new Iterator[Any] {
- private var c:Int = 0;
- private val cmax = n.caseArity;
- def hasNext = c < cmax;
+ private var c: Int = 0
+ private val cmax = n.caseArity
+ def hasNext = c < cmax
def next = { val a = n caseElement c; c = c + 1; a }
}
@@ -76,7 +84,7 @@ object Iterator {
* @return the iterator with values in range [lo;end).
*/
def range(lo: Int, end: Int): Iterator[Int] =
- range(lo, end, 1);
+ range(lo, end, 1)
/** Create an iterator with elements
* <code>e<sub>n+1</sub> = e<sub>n</sub> + step</code>
@@ -89,14 +97,16 @@ object Iterator {
* @return the iterator with values in range [lo;end).
*/
def range(lo: Int, end: Int, step: Int): Iterator[Int] = {
- assert(step != 0);
+ assert(step != 0)
new BufferedIterator[Int] {
- private var i = lo;
- def hasNext: Boolean = if (step > 0) i < end else i > end;
+ private var i = lo
+ def hasNext: Boolean = if (step > 0) i < end else i > end
def next: Int =
- if (hasNext) { val j = i; i = i + step; j } else Predef.error("next on empty iterator");
+ if (hasNext) { val j = i; i = i + step; j }
+ else Predef.error("next on empty iterator")
def head: Int =
- if (hasNext) i else Predef.error("head on empty iterator");
+ if (hasNext) i
+ else Predef.error("head on empty iterator")
}
}
@@ -112,12 +122,14 @@ object Iterator {
*/
def range(lo: Int, end: Int, step: Int => Int): Iterator[Int] =
new BufferedIterator[Int] {
- private var i = lo;
- def hasNext: Boolean = i < end;
+ private var i = lo
+ def hasNext: Boolean = i < end
def next: Int =
- if (i < end) { val j = i; i = step(i); j } else Predef.error("next on empty iterator");
+ if (i < end) { val j = i; i = step(i); j }
+ else Predef.error("next on empty iterator")
def head: Int =
- if (i < end) i else Predef.error("head on empty iterator");
+ if (i < end) i
+ else Predef.error("head on empty iterator")
}
/** Create an iterator with elements
@@ -128,7 +140,7 @@ object Iterator {
* @return the iterator starting at value <code>lo</code>.
*/
def from(lo: Int): Iterator[Int] =
- from(lo, 1);
+ from(lo, 1)
/** Create an iterator with elements
* <code>e<sub>n+1</sub> = e<sub>n</sub> + step</code>
@@ -140,10 +152,10 @@ object Iterator {
*/
def from(lo: Int, step: Int): Iterator[Int] =
new BufferedIterator[Int] {
- private var i = lo;
- def hasNext: Boolean = true;
+ private var i = lo
+ def hasNext: Boolean = true
def next: Int = { val j = i; i = i + step; j }
- def head: Int = i;
+ def head: Int = i
}
/** Create an iterator with elements
@@ -156,10 +168,10 @@ object Iterator {
*/
def from(lo: Int, step: Int => Int): Iterator[Int] =
new BufferedIterator[Int] {
- private var i = lo;
- def hasNext: Boolean = true;
+ private var i = lo
+ def hasNext: Boolean = true
def next: Int = { val j = i; i = step(i); j }
- def head: Int = i;
+ def head: Int = i
}
}
@@ -177,33 +189,33 @@ trait Iterator[+A] {
/** Does this iterator provide another element?
*/
- def hasNext: Boolean;
+ def hasNext: Boolean
/** Returns the next element.
*/
- def next: A;
+ def next: A
/** Returns a new iterator that iterates only over the first <code>n</code>
* elements.
*/
def take(n: Int) = new Iterator[A] {
- var remaining = n;
- def hasNext = remaining > 0 && Iterator.this.hasNext;
+ var remaining = n
+ def hasNext = remaining > 0 && Iterator.this.hasNext
def next: A =
if (hasNext) { remaining = remaining - 1; Iterator.this.next }
- else error("next on empty iterator");
+ else error("next on empty iterator")
}
/** Removes the first <code>n</code> elements from this iterator.
*/
def drop(n: Int): Iterator[A] =
- if (n > 0) { next; drop(n - 1) } else this;
+ if (n > 0) { next; drop(n - 1) } else this
/** Returns a new iterator that maps all elements of this iterator
* to new elements using function <code>f</code>.
*/
def map[B](f: A => B): Iterator[B] = new Iterator[B] {
- def hasNext = Iterator.this.hasNext;
+ def hasNext = Iterator.this.hasNext
def next = f(Iterator.this.next)
}
@@ -211,8 +223,8 @@ trait Iterator[+A] {
* iterator followed by the elements provided by iterator <code>that</code>.
*/
def append[B >: A](that: Iterator[B]) = new Iterator[B] {
- def hasNext = Iterator.this.hasNext || that.hasNext;
- def next = if (Iterator.this.hasNext) Iterator.this.next else that.next;
+ def hasNext = Iterator.this.hasNext || that.hasNext
+ def next = if (Iterator.this.hasNext) Iterator.this.next else that.next
}
/** Applies the given function <code>f</code> to each element of
@@ -223,19 +235,19 @@ trait Iterator[+A] {
* yields the elements <code>a0, ..., an</code>.
*/
def flatMap[B](f: A => Iterator[B]): Iterator[B] = new Iterator[B] {
- private var cur: Iterator[B] = Iterator.empty;
+ private var cur: Iterator[B] = Iterator.empty
def hasNext: Boolean =
if (cur.hasNext) true
else if (Iterator.this.hasNext) {
- cur = f(Iterator.this.next);
+ cur = f(Iterator.this.next)
hasNext
- } else false;
+ } else false
def next: B =
if (cur.hasNext) cur.next
else if (Iterator.this.hasNext) {
- cur = f(Iterator.this.next);
+ cur = f(Iterator.this.next)
next
- } else Predef.error("next on empty iterator");
+ } else Predef.error("next on empty iterator")
}
/** Returns an iterator over all the elements of this iterator that
@@ -246,13 +258,13 @@ trait Iterator[+A] {
* @return the elements of this iterator satisfying <code>p</code>.
*/
def filter(p: A => Boolean): Iterator[A] = new BufferedIterator[A] {
- private var hd: A = _;
- private var ahead: Boolean = false;
- private var hasMore = Iterator.this.hasNext;
+ private var hd: A = _
+ private var ahead: Boolean = false
+ private var hasMore = Iterator.this.hasNext
private def skip: Unit =
while (!ahead && hasMore) {
- hd = Iterator.this.next;
- hasMore = Iterator.this.hasNext;
+ hd = Iterator.this.next
+ hasMore = Iterator.this.hasNext
ahead = p(hd)
}
def hasNext: Boolean = { skip; ahead || hasMore }
@@ -273,8 +285,8 @@ trait Iterator[+A] {
* <code>bi</code> are the elements from iterator <code>that</code>.
*/
def zip[B](that: Iterator[B]) = new Iterator[Pair[A, B]] {
- def hasNext = Iterator.this.hasNext && that.hasNext;
- def next = Pair(Iterator.this.next, that.next);
+ def hasNext = Iterator.this.hasNext && that.hasNext
+ def next = Pair(Iterator.this.next, that.next)
}
@@ -301,7 +313,7 @@ trait Iterator[+A] {
*
* @param f a function that is applied to every element.
*/
- def foreach(f: A => Unit): Unit = while (hasNext) { f(next) };
+ def foreach(f: A => Unit): Unit = while (hasNext) { f(next) }
/** Apply a predicate <code>p</code> to all elements of this
* iterable object and return true, iff the predicate yields
@@ -311,7 +323,7 @@ trait Iterator[+A] {
* @returns true, iff the predicate yields true for all elements.
*/
def forall(p: A => Boolean): Boolean = {
- var res = true;
+ var res = true
while (res && hasNext) { res = p(next) }
res
}
@@ -324,7 +336,7 @@ trait Iterator[+A] {
* @returns true, iff the predicate yields true for at least one element.
*/
def exists(p: A => Boolean): Boolean = {
- var res = false;
+ var res = false
while (!res && hasNext) { res = p(next) }
res
}
@@ -335,132 +347,135 @@ trait Iterator[+A] {
* @return True iff there is an element of this list which is
* equal (w.r.t. <code>==</code>) to <code>elem</code>.
*/
- def contains(elem: Any): Boolean = exists { x => x == elem };
-
- /** Find and return the first element of the iterable object satisfying a
- * predicate, if any.
- *
- * @param p the predicate
- * @return the first element in the iterable object satisfying <code>p</code>,
- * or <code>None</code> if none exists.
- */
- def find(p: A => Boolean): Option[A] = {
- var res: Option[A] = None;
- while (res.isEmpty && hasNext) {
- val e = next;
- if (p(e)) res = Some(e);
- }
- res
- }
+ def contains(elem: Any): Boolean = exists { x => x == elem }
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from left to right, and starting with
- * the value <code>z</code>.
- * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
- * is <code>List(a0, a1, ..., an)</code>.
- */
- def foldLeft[B](z: B)(op: (B, A) => B): B = {
- var acc = z;
- while (hasNext) { acc = op(acc, next) }
- acc
+ /** Find and return the first element of the iterable object satisfying a
+ * predicate, if any.
+ *
+ * @param p the predicate
+ * @return the first element in the iterable object satisfying <code>p</code>,
+ * or <code>None</code> if none exists.
+ */
+ def find(p: A => Boolean): Option[A] = {
+ var res: Option[A] = None
+ while (res.isEmpty && hasNext) {
+ val e = next
+ if (p(e)) res = Some(e);
}
+ res
+ }
- /** Combines the elements of this list together using the binary
- * operator <code>op</code>, from rigth to left, and starting with
- * the value <code>z</code>.
- * @return <code>a0 op (... op (an op z)...)</code> if the list
- * is <code>[a0, a1, ..., an]</code>.
- */
- def foldRight[B](z: B)(op: (A, B) => B): B = {
- def fold(z: B): B =
- if (hasNext) op(next, fold(z)) else z;
- fold(z)
- }
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from left to right, and starting with
+ * the value <code>z</code>.
+ * @return <code>op(... (op(op(z,a0),a1) ...), an)</code> if the list
+ * is <code>List(a0, a1, ..., an)</code>.
+ */
+ def foldLeft[B](z: B)(op: (B, A) => B): B = {
+ var acc = z
+ while (hasNext) { acc = op(acc, next) }
+ acc
+ }
- /** Similar to <code>foldLeft</code> but can be used as
- * an operator with the order of list and zero arguments reversed.
- * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
- */
- def /:[B](z: B)(f: (B, A) => B): B = foldLeft(z)(f);
-
- /** An alias for <code>foldRight</code>.
- * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
- */
- def :\[B](z: B)(f: (A, B) => B): B = foldRight(z)(f);
-
- /** Returns a buffered iterator from this iterator.
- */
- def buffered: BufferedIterator[A] = new BufferedIterator[A] {
- private var hd: A = _;
- private var ahead: Boolean = false;
- def head: A = {
- if (!ahead) {
- hd = Iterator.this.next;
- ahead = true
- }
- hd
- }
- def next: A =
- if (ahead) { ahead = false; hd } else head;
- def hasNext: Boolean = ahead || Iterator.this.hasNext;
- }
+ /** Combines the elements of this list together using the binary
+ * operator <code>op</code>, from rigth to left, and starting with
+ * the value <code>z</code>.
+ * @return <code>a0 op (... op (an op z)...)</code> if the list
+ * is <code>[a0, a1, ..., an]</code>.
+ */
+ def foldRight[B](z: B)(op: (A, B) => B): B = {
+ def fold(z: B): B =
+ if (hasNext) op(next, fold(z)) else z;
+ fold(z)
+ }
- /** Returns a counted iterator from this iterator.
- */
- def counted = new CountedIterator[A] {
- private var cnt = -1;
- def count = cnt;
- def hasNext: Boolean = Iterator.this.hasNext;
- def next: A = { cnt = cnt + 1; Iterator.this.next }
+ /** Similar to <code>foldLeft</code> but can be used as
+ * an operator with the order of list and zero arguments reversed.
+ * That is, <code>z /: xs</code> is the same as <code>xs foldLeft z</code>
+ */
+ def /:[B](z: B)(f: (B, A) => B): B = foldLeft(z)(f)
+
+ /** An alias for <code>foldRight</code>.
+ * That is, <code>xs :\ z</code> is the same as <code>xs foldRight z</code>
+ */
+ def :\[B](z: B)(f: (A, B) => B): B = foldRight(z)(f)
+
+ /** Returns a buffered iterator from this iterator.
+ */
+ def buffered: BufferedIterator[A] = new BufferedIterator[A] {
+ private var hd: A = _
+ private var ahead: Boolean = false
+ def head: A = {
+ if (!ahead) {
+ hd = Iterator.this.next;
+ ahead = true
+ }
+ hd
}
+ def next: A =
+ if (ahead) { ahead = false; hd } else head;
+ def hasNext: Boolean = ahead || Iterator.this.hasNext
+ }
+
+ /** Returns a counted iterator from this iterator.
+ */
+ def counted = new CountedIterator[A] {
+ private var cnt = -1
+ def count = cnt
+ def hasNext: Boolean = Iterator.this.hasNext
+ def next: A = { cnt = cnt + 1; Iterator.this.next }
+ }
- /** Creates two new iterators that both iterate over the same elements
- * than this iterator (in the same order).
- */
- def duplicate: Pair[Iterator[A], Iterator[A]] = {
- var xs: List[A] = Nil;
- var ahead: Iterator[A] = null;
- class Partner extends Iterator[A] {
- var ys: List[A] = Nil;
- def hasNext: Boolean = Iterator.this.synchronized (
- ((this == ahead) && Iterator.this.hasNext) ||
- ((this != ahead) && (!xs.isEmpty || !ys.isEmpty || Iterator.this.hasNext))
- );
- def next: A = Iterator.this.synchronized {
- if (this == ahead) {
- val e = Iterator.this.next;
- xs = e :: xs; e
- } else {
- if (ys.isEmpty) {
- ys = xs.reverse;
- xs = Nil;
- }
- ys match {
- case Nil => val e = Iterator.this.next;
- ahead = this;
- xs = e :: xs; e
- case z :: zs => ys = zs; z
- }
- }
- }
+ /** Creates two new iterators that both iterate over the same elements
+ * than this iterator (in the same order).
+ */
+ def duplicate: Pair[Iterator[A], Iterator[A]] = {
+ var xs: List[A] = Nil
+ var ahead: Iterator[A] = null
+ class Partner extends Iterator[A] {
+ var ys: List[A] = Nil
+ def hasNext: Boolean = Iterator.this.synchronized (
+ ((this == ahead) && Iterator.this.hasNext) ||
+ ((this != ahead) && (!xs.isEmpty || !ys.isEmpty || Iterator.this.hasNext))
+ )
+ def next: A = Iterator.this.synchronized {
+ if (this == ahead) {
+ val e = Iterator.this.next
+ xs = e :: xs; e
+ } else {
+ if (ys.isEmpty) {
+ ys = xs.reverse
+ xs = Nil
+ }
+ ys match {
+ case Nil =>
+ val e = Iterator.this.next
+ ahead = this
+ xs = e :: xs; e
+ case z :: zs =>
+ ys = zs; z
+ }
}
- ahead = new Partner;
- Pair(ahead, new Partner)
+ }
}
+ ahead = new Partner
+ Pair(ahead, new Partner)
+ }
/** Fills the given array <code>xs</code> with the elements of
* this sequence starting at position <code>start</code>.
*
* @param xs the array to fill.
* @param start starting index.
- * @return the given array <code>xs</code> filled with the elements of this iterator.
+ * @return the given array <code>xs</code> filled with the elements of
+ * this iterator.
* @pre The array must be large enough to hold all elements.
*/
def copyToArray[B >: A](xs: Array[B], start: Int): Array[B] = {
- var i = start;
+ var i = start
while (hasNext) {
- xs(i) = next;
- i = i + 1;
+ xs(i) = next
+ i = i + 1
}
xs
}
@@ -470,9 +485,9 @@ trait Iterator[+A] {
* @return a list which enumerates all elements of this iterator.
*/
def toList: List[A] = {
- val res = new collection.mutable.ListBuffer[A];
+ val res = new collection.mutable.ListBuffer[A]
while (hasNext) {
- res += next;
+ res += next
}
res.toList
}
View
18 src/library/scala/MatchError.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala;
+package scala
import Predef._
@@ -24,7 +24,8 @@ import Predef._
object MatchError {
// todo: change pattern matcher so that dummy type parameter T can be removed.
- def fail[T](source: String, line: Int): Nothing = throw new MatchError(source, line);
+ def fail[T](source: String, line: Int): Nothing =
+ throw new MatchError(source, line)
def report(source: String, line: Int, obj: Any) =
try {
@@ -36,11 +37,12 @@ object MatchError {
}
final class MatchError(msg: String) extends Error(msg) {
- def this(source: String, line: Int) =
- this(" in '" + source + "' at line " + line);
- def this(source: String, line: Int, obj: String) =
- this("for object " + obj + " in '" + source + "' at line " + line);
+ def this(source: String, line: Int) =
+ this(" in '" + source + "' at line " + line)
- def this(ob:Any) =
- this(ob.toString());
+ def this(source: String, line: Int, obj: String) =
+ this("for object " + obj + " in '" + source + "' at line " + line)
+
+ def this(ob: Any) =
+ this(ob.toString())
}
View
10 src/library/scala/Proxy.scala
@@ -9,7 +9,7 @@
// $Id$
-package scala;
+package scala
/** This class implements a simple proxy that forwards all calls to
@@ -21,8 +21,8 @@ package scala;
* @version 1.0, 26/04/2004
*/
trait Proxy {
- def self: Any;
- override def hashCode(): Int = self.hashCode();
- override def equals(y: Any): Boolean = self.equals(y);
- override def toString(): String = self.toString();
+ def self: Any
+ override def hashCode(): Int = self.hashCode()
+ override def equals(y: Any): Boolean = self.equals(y)
+ override def toString(): String = self.toString()
}
View
34 src/library/scala/Responder.scala
@@ -1,18 +1,40 @@
+/* __ *\
+** ________ ___ / / ___ Scala API **
+** / __/ __// _ | / / / _ | (c) 2005-2006, LAMP/EPFL **
+** __\ \/ /__/ __ |/ /__/ __ | **
+** /____/\___/_/ |_/____/_/ | | **
+** |/ **
+\* */
+
+// $Id$
+
package scala
-/** contains utility methods to build responders
+/** This object contains utility methods to build responders.
+ *
+ * @author Burak Emir
+ * @version 1.0
+ *
* @see class Responder
* @since revision 6897 (will be 2.1.1)
*/
object Responder {
- /** creates a responder that answer continuations with the constant a */
+ /** Creates a responder that answer continuations with the constant
+ * <code>a</code>.
+ *
+ * @param x ...
+ * @return ...
+ */
def constant[a](x: a) = new Responder[a] {
def respond(k: a => unit) = k(x)
}
- /** executes x and returns true, useful as syntactic convenience in
- * for comprehensions
+ /** Executes <code>x</code> and returns <code>true</code>, useful
+ * as syntactic convenience in for comprehensions.
+ *
+ * @param x ...
+ * @return ...
*/
def exec[a](x: => unit): boolean = { x; true }
@@ -38,6 +60,10 @@ object Responder {
* 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.
+ *
+ * @author Burak Emir
+ * @version 1.0
+ *
* @since revision 6897 (will be 2.1.1)
*/
abstract class Responder[+a] {
View
134 src/library/scala/Seq.scala
@@ -9,20 +9,20 @@
// $Id$
-package scala;
+package scala
-import scala.runtime.compat.StringBuilder;
+import scala.runtime.compat.StringBuilder
object Seq {
/** builds a singleton sequence
* @author buraq
*/
- def single[A](x:A) = new Seq[A] {
- def length = 1;
- def elements = Iterator.single(x);
- override def isDefinedAt(x: Int): Boolean = (x == 0);
- def apply(i:Int) = x; // caller's responsibility to check isDefinedAt
+ def single[A](x: A) = new Seq[A] {
+ def length = 1
+ def elements = Iterator.single(x)
+ override def isDefinedAt(x: Int): Boolean = (x == 0)
+ def apply(i: Int) = x // caller's responsibility to check isDefinedAt
}
/*
implicit def view[A <% Ordered[A]](xs: Seq[A]): Ordered[Seq[A]] =