diff options
author | Lex Spoon <lex@lexspoon.org> | 2007-01-30 13:10:19 +0000 |
---|---|---|
committer | Lex Spoon <lex@lexspoon.org> | 2007-01-30 13:10:19 +0000 |
commit | d3d12d547f720c96d7b4734cc455ba7b798ad6a0 (patch) | |
tree | 792c2bdf0463261c5c0ebb1ce88161fe697d99c5 /src/library | |
parent | 434d46045496c964572fc38df873285c5464d89b (diff) | |
download | scala-d3d12d547f720c96d7b4734cc455ba7b798ad6a0.tar.gz scala-d3d12d547f720c96d7b4734cc455ba7b798ad6a0.tar.bz2 scala-d3d12d547f720c96d7b4734cc455ba7b798ad6a0.zip |
Added unapply
Diffstat (limited to 'src/library')
-rw-r--r-- | src/library/scala/Stream.scala | 41 |
1 files changed, 25 insertions, 16 deletions
diff --git a/src/library/scala/Stream.scala b/src/library/scala/Stream.scala index 6ecc9b768c..2e21fc27c1 100644 --- a/src/library/scala/Stream.scala +++ b/src/library/scala/Stream.scala @@ -32,23 +32,32 @@ object Stream { protected def addDefinedElems(buf: StringBuilder, prefix: String): StringBuilder = buf } - /** A stream consisting of a given first element and remaining elements - * @param hd The first element of the result stream - * @param tl The remaining elements of the result stream - */ - def cons[a](hd: a, tl: => Stream[a]) = new Stream[a] { - override def isEmpty = false - def head = hd - private var tlVal: Stream[a] = _ - private var tlDefined = false - def tail: Stream[a] = { - if (!tlDefined) { tlVal = tl; tlDefined = true } - tlVal - } - protected def addDefinedElems(buf: StringBuilder, prefix: String): StringBuilder = { - val buf1 = buf.append(prefix).append(hd) - if (tlDefined) tlVal.addDefinedElems(buf1, ", ") else buf1 append ", ?" + + object cons { + /** A stream consisting of a given first element and remaining elements + * @param hd The first element of the result stream + * @param tl The remaining elements of the result stream + */ + def apply[a](hd: a, tl: => Stream[a]) = new Stream[a] { + override def isEmpty = false + def head = hd + private var tlVal: Stream[a] = _ + private var tlDefined = false + def tail: Stream[a] = { + if (!tlDefined) { tlVal = tl; tlDefined = true } + tlVal + } + protected def addDefinedElems(buf: StringBuilder, prefix: String): StringBuilder = { + val buf1 = buf.append(prefix).append(hd) + if (tlDefined) tlVal.addDefinedElems(buf1, ", ") else buf1 append ", ?" + } } + + def unapply[a](str: Stream[a]): Option[{a,Stream[a]}] = + if(str.isEmpty) + None + else + Some({str.head, str.tail}) } /** A stream containing all elements of a given iterator, in the order they are produced. |