diff --git a/doc/purescala.rst b/doc/purescala.rst index bde72673c2894648b1921d58456d68eda50a978c..f26a5e228a620a6bfb0ee153a439dcf4bd5d4149 100644 --- a/doc/purescala.rst +++ b/doc/purescala.rst @@ -182,16 +182,32 @@ Pattern matching .. code-block:: scala expr match { - // Simple (nested) patterns: - case CaseClass( .. , .. , ..) => ... - case v @ CaseClass( .. , .. , ..) => ... - case v : CaseClass => ... - case (t1, t2) => ... - case 42 => ... - case _ => ... - - // can also be guarded, e.g. - case CaseClass(a, b, c) if a > b => ... + // Simple (nested) patterns: + case CaseClass( .. , .. , ..) => ... + case v @ CaseClass( .. , .. , ..) => ... + case v : CaseClass => ... + case (t1, t2) => ... + case 42 => ... + case _ => ... + + // can also be guarded, e.g. + case CaseClass(a, b, c) if a > b => ... + } + +Custom pattern matching with ``unapply`` methods are also supported: + +.. code-block:: scala + + object :: { + def unapply[A](l: List[A]): Option[(A, List[A])] = l match { + case Nil() => None() + case Cons(x, xs) => Some((x, xs)) + } + } + + def empty[A](l: List[A]) = l match { + case x :: xs => false + case Nil() => true } Values diff --git a/library/collection/List.scala b/library/collection/List.scala index c7a38dc6388be2db68adbf2d4a54fb5d42b1bc24..b3078c600d7e00430ea4100633ad9feacd1022cd 100644 --- a/library/collection/List.scala +++ b/library/collection/List.scala @@ -35,6 +35,11 @@ sealed abstract class List[T] { (that != Nil[T]() || res == this) } + def headOption: Option[T] = this match { + case Nil() => None[T]() + case Cons(h, _) => Some(h) + } + def head: T = { require(this != Nil[T]()) val Cons(h, _) = this @@ -537,6 +542,15 @@ object ListOps { case class Cons[T](h: T, t: List[T]) extends List[T] case class Nil[T]() extends List[T] +// 'Cons' Extractor +object :: { + def unapply[A](l: List[A]): Option[(A, List[A])] = l match { + case Nil() => None() + case Cons(x, xs) => Some((x, xs)) + } +} + + @library object ListSpecs { def snocIndex[T](l : List[T], t : T, i : BigInt) : Boolean = { diff --git a/library/lang/string/package.scala b/library/lang/string/package.scala index f09af7c6e5fe807389c479fe65af570309a5b16f..e3cd3d7aebda60a240fd47feba7c4e2552b26d64 100644 --- a/library/lang/string/package.scala +++ b/library/lang/string/package.scala @@ -16,7 +16,7 @@ package object string { @ignore def listToList[A](s: ScalaList[A]): List[A] = s match { - case h :: t => + case scala.::(h, t) => Cons(h, listToList(t)) case _ => Nil[A]()