diff options
-rw-r--r-- | src/library/scala/collection/SeqExtractors.scala | 21 | ||||
-rw-r--r-- | src/library/scala/package.scala | 3 | ||||
-rw-r--r-- | test/files/run/matchonseq.check | 2 | ||||
-rw-r--r-- | test/files/run/matchonseq.scala | 8 |
4 files changed, 34 insertions, 0 deletions
diff --git a/src/library/scala/collection/SeqExtractors.scala b/src/library/scala/collection/SeqExtractors.scala new file mode 100644 index 0000000000..cb3cb27f18 --- /dev/null +++ b/src/library/scala/collection/SeqExtractors.scala @@ -0,0 +1,21 @@ +package scala.collection + +/** An extractor used to head/tail deconstruct sequences. */ +object +: { + def unapply[T,Coll <: SeqLike[T, Coll]]( + t: Coll with SeqLike[T, Coll]): Option[(T, Coll)] = + if(t.isEmpty) None + else Some(t.head -> t.tail) +} + +/** An extractor used to init/last deconstruct sequences. */ +object :+ { + /** Splits a sequence into init :+ tail. + * @returns Some(init, tail) if sequence is non-empty. + * None otherwise. + */ + def unapply[T,Coll <: SeqLike[T, Coll]]( + t: Coll with SeqLike[T, Coll]): Option[(Coll, T)] = + if(t.isEmpty) None + else Some(t.init -> t.last) +} diff --git a/src/library/scala/package.scala b/src/library/scala/package.scala index 366af34ee9..1ef1911fd3 100644 --- a/src/library/scala/package.scala +++ b/src/library/scala/package.scala @@ -64,6 +64,9 @@ package object scala { type ::[A] = scala.collection.immutable.::[A] val :: = scala.collection.immutable.:: + val +: = scala.collection.+: + val :+ = scala.collection.:+ + type Stream[+A] = scala.collection.immutable.Stream[A] val Stream = scala.collection.immutable.Stream val #:: = scala.collection.immutable.Stream.#:: diff --git a/test/files/run/matchonseq.check b/test/files/run/matchonseq.check new file mode 100644 index 0000000000..3fe554095a --- /dev/null +++ b/test/files/run/matchonseq.check @@ -0,0 +1,2 @@ +It worked! head=1 +It worked! last=3 diff --git a/test/files/run/matchonseq.scala b/test/files/run/matchonseq.scala new file mode 100644 index 0000000000..49b406a6ec --- /dev/null +++ b/test/files/run/matchonseq.scala @@ -0,0 +1,8 @@ +object Test extends App{ + Vector(1,2,3) match { + case head +: tail => println("It worked! head=" + head) + } + Vector(1,2,3) match { + case init :+ last => println("It worked! last=" + last) + } +} |