diff options
author | Josh Suereth <joshua.suereth@gmail.com> | 2012-03-16 20:59:38 -0400 |
---|---|---|
committer | Josh Suereth <joshua.suereth@gmail.com> | 2012-03-16 20:59:38 -0400 |
commit | b3efb3d493605d1c7e106e5f0a697b52ebb3d97c (patch) | |
tree | 1a141a2b741684b91d56c1288ab043623f2ce956 /src/library/scala/collection/SeqExtractors.scala | |
parent | d267988ddbf03c71fa1ef2fa51f2d218793632ed (diff) | |
download | scala-b3efb3d493605d1c7e106e5f0a697b52ebb3d97c.tar.gz scala-b3efb3d493605d1c7e106e5f0a697b52ebb3d97c.tar.bz2 scala-b3efb3d493605d1c7e106e5f0a697b52ebb3d97c.zip |
Added +: and :+ extractors to mirror append/prepend.
* +: does head/tail decomposition on any Seq
* :+ does init/last decomposition on any Seq
* Both preserve specific Seq types.
Review by @odersky
Diffstat (limited to 'src/library/scala/collection/SeqExtractors.scala')
-rw-r--r-- | src/library/scala/collection/SeqExtractors.scala | 21 |
1 files changed, 21 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) +} |