Created
August 24, 2011 03:02
-
-
Save fmpwizard/1167211 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
package com.fmpwizard.sample.code | |
package myScalaz | |
import net.liftweb.common.{Box, Full, Empty} | |
import scalaz._ | |
sealed trait BoxW[A] extends PimpedType[Box[A]] { | |
import Scalaz._ | |
/** | |
* Catamorphism over the option. Returns the provided function `some` applied to item contained in the Option | |
* if it is defined, otherwise, the provided value `none`. | |
*/ | |
def cata[X](some: A => X, none: => X): X = value match { | |
case net.liftweb.common.Empty => none | |
case Full(a) => some(a) | |
} | |
/** Alias for `cata` */ | |
def fold[X](some: A => X, none: => X): X = cata(some, none) | |
sealed trait Fold[X] { | |
def none(s: => X): X | |
} | |
/** | |
* Returns the provided function `s` applied to item contained in the Option if it is defined, | |
* otherwise, the provided value `n`. | |
* <p/> | |
* This is a syntactic alternative to { @link scalaz.OptionW # cata } | |
* <p/> | |
* Example: | |
* <code> | |
* option.some(_ * 2).none(0) | |
* </code> | |
*/ | |
def some[X](s: A => X): Fold[X] = new Fold[X] { | |
def none(n: => X): X = cata(s, n) | |
} | |
sealed trait Conditional[X] { | |
def |(n: => X): X | |
} | |
/** | |
* Ternary operator. Note that the arguments s and n are call-by-name. | |
* <p/> | |
* Example | |
* <code> | |
* option ? "defined" | "undefined" | |
* </code> | |
*/ | |
def ?[X](s: => X): Conditional[X] = new Conditional[X] { | |
def |(n: => X): X = value match { | |
case net.liftweb.common.Empty => n | |
case Full(_) => s | |
} | |
} | |
/** | |
* Executes the provided side effect if the Option if it is undefined. | |
*/ | |
def ifNone(n: => Unit): Unit = if (value.isEmpty) n | |
/** | |
* Returns the item contained in the Option if it is defined, otherwise, raises an error with the provided message. | |
*/ | |
def err(message: => String): A = value getOrElse (error(message)) | |
//for scala 2.9.x | |
//def err(message: => String): A = value getOrElse (sys.error(message)) | |
/** | |
* Returns the item contained in the Option if it is defined, otherwise, the provided argument. | |
*/ | |
def |(a: => A): A = value getOrElse a | |
/** | |
* Returns the item contained in the Option if it is defined, otherwise, the zero element for the type A | |
* <p/> | |
* For example: | |
* <pre> | |
* val o: Option = None | |
* val a: List[String] = ~o | |
* </pre> | |
*/ | |
def unary_~(implicit z: Zero[A]): A = value getOrElse z.zero | |
def orZero(implicit z: Zero[A]): A = ~this | |
def toSuccess[E](e: => E): Validation[E, A] = value match { | |
case Full(a) => Success(a) | |
case net.liftweb.common.Empty => Failure(e) | |
} | |
def toFailure[B](b: => B): Validation[A, B] = value match { | |
case Full(e) => Failure(e) | |
case net.liftweb.common.Empty => Success(b) | |
} | |
//def fst: FirstOption[A] = value | |
//def lst: LastOption[A] = value | |
/** | |
* Returns the item contained in the Option wrapped in type M if the Option is defined, | |
* otherwise, the empty value for type M. | |
*/ | |
def orEmpty[M[_] : Pure : Empty]: M[A] = value match { | |
case Full(a) => a η | |
case net.liftweb.common.Empty => <∅> | |
} | |
/** | |
* Returns the given value if None, otherwise lifts the Some value and passes it to the given function. | |
*/ | |
def foldLift[F[_], B](b: => B, k: F[A] => B)(implicit p: Pure[F]): B = value match { | |
case net.liftweb.common.Empty => b | |
case Full(a) => k(a.η[F]) | |
} | |
/** | |
* Returns the given value if None, otherwise lifts the Some value to Option and passes it to the given function. | |
*/ | |
//def foldLiftOpt[B](b: => B, k: Box[A] => B): B = foldLift[Box, B](b, k) | |
/** | |
* Returns a Done iteratee with the given value if the Option is not defined, otherwise runs the given function. | |
*/ | |
def iterDoneOr[B](b: => B, f: A => IterV[A, B]): IterV[A, B] = value match { | |
case net.liftweb.common.Empty => IterV.Done(b, IterV.EOF.apply) | |
case Full(a) => f(a) | |
} | |
} | |
trait Boxes { | |
implicit def BoxTo[A](o: Box[A]): BoxW[A] = new BoxW[A] { | |
val value = o | |
} | |
def some[A](a: A): Box[A] = Full(a) | |
def none[A]: Box[A] = net.liftweb.common.Empty | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment