Skip to content

Instantly share code, notes, and snippets.

@stew
Created August 6, 2014 02:40
Show Gist options
  • Save stew/a5e29f6a58200b961b61 to your computer and use it in GitHub Desktop.
Save stew/a5e29f6a58200b961b61 to your computer and use it in GitHub Desktop.
Changes from HEAD to working tree
2 files changed, 19 insertions(+), 10 deletions(-)
core/src/main/scala/scalaz/IList.scala | 16 ++++++++++------
core/src/main/scala/scalaz/Maybe.scala | 13 +++++++++----
Modified core/src/main/scala/scalaz/IList.scala
diff --git a/core/src/main/scala/scalaz/IList.scala b/core/src/main/scala/scalaz/IList.scala
index 946ad98..7e383c7 100644
--- a/core/src/main/scala/scalaz/IList.scala
+++ b/core/src/main/scala/scalaz/IList.scala
@@ -186,8 +186,7 @@ sealed abstract class IList[A] extends Product with Serializable {
intersperse0(empty, this).reverse
}
- def isEmpty: Boolean =
- uncons(true, (_, _) => false)
+ def isEmpty: Boolean
def lastIndexOf(a:A)(implicit ev: Equal[A]): Option[Int] =
reverse.indexOf(a).map((length - 1) - _)
@@ -441,10 +440,15 @@ sealed abstract class IList[A] extends Product with Serializable {
}
-// In order to get exhaustiveness checking and a sane unapply in both 2.9 and 2.10 it seems
-// that we need to use bare case classes. Sorry. Suggestions welcome.
-final case class INil[A]() extends IList[A]
-final case class ICons[A](head: A, tail: IList[A]) extends IList[A]
+final case object INil extends IList[Nothing] {
+ final override def isEmpty = true
+ def unapply[A](l: IList[A]) = l.isEmpty
+ def apply[A]() = this.asInstanceOf[IList[A]] // YOLO
+}
+
+final case class ICons[A](head: A, tail: IList[A]) extends IList[A] {
+ final override def isEmpty = false
+}
object IList extends IListInstances with IListFunctions{
private[this] val nil: IList[Nothing] = INil()
Modified core/src/main/scala/scalaz/Maybe.scala
diff --git a/core/src/main/scala/scalaz/Maybe.scala b/core/src/main/scala/scalaz/Maybe.scala
index caad9f6..bf127e8 100644
--- a/core/src/main/scala/scalaz/Maybe.scala
+++ b/core/src/main/scala/scalaz/Maybe.scala
@@ -68,8 +68,7 @@ sealed abstract class Maybe[A] {
cata(_ => true, false)
/** True if no underlying value is present */
- final def isEmpty: Boolean =
- cata(_ => false, true)
+ def isEmpty: Boolean
final def map[B](f: A => B): Maybe[B] =
cata(f andThen just[B], empty[B])
@@ -147,9 +146,15 @@ sealed abstract class Maybe[A] {
object Maybe extends MaybeInstances with MaybeFunctions {
- final case class Empty[A]() extends Maybe[A]
+ final case object Empty extends Maybe[Nothing] {
+ final override def isEmpty = true
+ def unapply[A](m: Maybe[A]) = m.isEmpty
+ def apply[A](): Maybe[A] = this.asInstanceOf[Maybe[A]] // YOLO
+ }
- final case class Just[A](a: A) extends Maybe[A]
+ final case class Just[A](a: A) extends Maybe[A] {
+ final override def isEmpty = false
+ }
val optionMaybeIso: Option <~> Maybe =
new IsoFunctorTemplate[Option, Maybe] {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment