summaryrefslogtreecommitdiff
path: root/sources
diff options
context:
space:
mode:
authorburaq <buraq@epfl.ch>2005-06-09 15:24:33 +0000
committerburaq <buraq@epfl.ch>2005-06-09 15:24:33 +0000
commitd7499538cc0da7beb331a4550b7a6e85cd132876 (patch)
treed0aa961c6f4ee2629945eac6c15e60e1fe910ae0 /sources
parentfaf9183089b7425e0e86d4d34a807bce238dce86 (diff)
downloadscala-d7499538cc0da7beb331a4550b7a6e85cd132876.tar.gz
scala-d7499538cc0da7beb331a4550b7a6e85cd132876.tar.bz2
scala-d7499538cc0da7beb331a4550b7a6e85cd132876.zip
added useful methods for constant time concaten...
added useful methods for constant time concatenation
Diffstat (limited to 'sources')
-rw-r--r--sources/scala/Iterable.scala9
-rw-r--r--sources/scala/Seq.scala28
2 files changed, 37 insertions, 0 deletions
diff --git a/sources/scala/Iterable.scala b/sources/scala/Iterable.scala
index 838ba02821..7800782888 100644
--- a/sources/scala/Iterable.scala
+++ b/sources/scala/Iterable.scala
@@ -69,6 +69,15 @@ trait Iterable[+A] {
*/
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.
*
diff --git a/sources/scala/Seq.scala b/sources/scala/Seq.scala
index db05785b50..efc3996333 100644
--- a/sources/scala/Seq.scala
+++ b/sources/scala/Seq.scala
@@ -12,6 +12,17 @@ package scala;
import Predef._;
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 view[A <% Ordered[A]](xs: Seq[A]): Ordered[Seq[A]] = new Ordered[Seq[A]] with Proxy {
def self: Any = xs;
def compareTo[B >: Seq[A] <% Ordered[B]](that: B): Int = that match {
@@ -46,6 +57,22 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] {
*/
def length: Int;
+ /** Returns the concatenation of two sequences.
+ *
+ * @return concatenation of this sequence with argument
+ * @author buraq
+ */
+ def concat[B >: A](that:Seq[B]): Seq[B] = new Seq[B] {
+ def length = Seq.this.length + that.length;
+ def elements: Iterator[B] = Seq.this.elements.append(that.elements);
+ def apply(i:Int) = {
+ if(Seq.this.isDefinedAt(i))
+ Seq.this.apply(i)
+ else
+ that.apply(i - Seq.this.length);
+ }
+ }
+
/** Is this partial function defined for the index <code>x</code>?
*
* @return true, iff <code>x</code> is a legal sequence index.
@@ -164,3 +191,4 @@ trait Seq[+A] extends AnyRef with PartialFunction[Int, A] with Iterable[A] {
*/
protected def stringPrefix: String = "Seq";
}
+