diff options
-rw-r--r-- | src/library/scala/RandomAccessSeq.scala | 9 | ||||
-rw-r--r-- | src/library/scala/Seq.scala | 3 | ||||
-rw-r--r-- | test/files/run/randomAccessSeq-apply.check | 1 | ||||
-rw-r--r-- | test/files/run/randomAccessSeq-apply.scala | 15 |
4 files changed, 28 insertions, 0 deletions
diff --git a/src/library/scala/RandomAccessSeq.scala b/src/library/scala/RandomAccessSeq.scala index 8608456f12..7cb46393d1 100644 --- a/src/library/scala/RandomAccessSeq.scala +++ b/src/library/scala/RandomAccessSeq.scala @@ -14,6 +14,15 @@ import collection.mutable.ArrayBuffer object RandomAccessSeq { + /** Create read only sequence of specified elements */ + def apply[A](xs: A*): RandomAccessSeq[A] = + if (xs.isEmpty) empty + else { + val buffer = new ArrayBuffer[A] + xs copyToBuffer buffer + buffer.readOnly + } + /** The empty sequence */ val empty : RandomAccessSeq[Nothing] = new RandomAccessSeq.Projection[Nothing] { def length = 0 diff --git a/src/library/scala/Seq.scala b/src/library/scala/Seq.scala index 763a2e8ec2..968d32ee57 100644 --- a/src/library/scala/Seq.scala +++ b/src/library/scala/Seq.scala @@ -26,6 +26,9 @@ object Seq { */ def unapplySeq[A](x: Seq[A]): Some[Seq[A]] = Some(x) + /** Create read only sequence of specified elements */ + def apply[A](xs: A*): Seq[A] = RandomAccessSeq(xs: _*) + case class singleton[A](value: A) extends RandomAccessSeq[A] { override def length = 1 override def isDefinedAt(idx: Int): Boolean = idx == 0 diff --git a/test/files/run/randomAccessSeq-apply.check b/test/files/run/randomAccessSeq-apply.check new file mode 100644 index 0000000000..d86bac9de5 --- /dev/null +++ b/test/files/run/randomAccessSeq-apply.check @@ -0,0 +1 @@ +OK diff --git a/test/files/run/randomAccessSeq-apply.scala b/test/files/run/randomAccessSeq-apply.scala new file mode 100644 index 0000000000..863a4d42a2 --- /dev/null +++ b/test/files/run/randomAccessSeq-apply.scala @@ -0,0 +1,15 @@ +object Test extends Application { + val empty = RandomAccessSeq() + assert(empty.isEmpty) + + val single = RandomAccessSeq(1) + assert(List(1) == single.toList) + + val two = RandomAccessSeq("a", "b") + assert("a" == two.first) + assert("b" == two.apply(1)) + + println("OK") +} + +// vim: set ts=2 sw=2 et: |