Skip to content

Instantly share code, notes, and snippets.

@arschles
Created December 28, 2012 21:21
Show Gist options
  • Save arschles/4402021 to your computer and use it in GitHub Desktop.
Save arschles/4402021 to your computer and use it in GitHub Desktop.
import scalaz._
import Scalaz._
//here we say that the default value for List[String] is "hello" :: "world" :: Nil
implicit val listZero = new Zero[List[String]] = {
override lazy val zero = List("hello", "world")
}
//scalaz uses Zero in its unary ~ operator on Option
val mbListString = none[List[String]]
~mbListString //the resulting value is "hello" :: "world" :: Nil
mbListString.orZero //the resulting value is "hello" :: "world" :: Nil
//here's how to use a Zero in your own code
def doSomething[T](mbT: Option[T])(implicit z: Zero[T]) = mbT.map { t: T =>
process(t)
}.getOrElse(z.zero)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment