Created
December 28, 2012 21:21
-
-
Save arschles/4402021 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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