Skip to content

Instantly share code, notes, and snippets.

@Mahoney
Created September 28, 2012 15:43
Show Gist options
  • Save Mahoney/3800600 to your computer and use it in GitHub Desktop.
Save Mahoney/3800600 to your computer and use it in GitHub Desktop.
Useful Package Level Implicits for Option and ::
package object sandbox {
type ?[A] = Option[A]
// val a: ?[Int] = 1 Some(1): ?[Int]
implicit def instanceToOption[A](instance: A): ?[A] = Option(instance)
def Some[A](instance: A): ?[A] = Option(instance)
// val a: ?[List[Int]] = None None: ?[List[Int]]
// val b = a(_.sum) None: ?[Int]
// val c = Option(List(1, 2)) None: ?[List[Int]]
// val d = c(_.sum) Some(3): ?[Int]
class OptionExt[A](option: ?[A]) {
def apply[B](f: A => B): ?[B] = option.map(f)
}
implicit def option2ext[A](decorated: ?[A]) = new OptionExt(decorated)
// val a: ?[Int] = None None: ?[Int]
// val b = a ?: 2 2: Int
// val c = Option(5) Some(5): ?[Int]
// val d = a ?: 2 5: Int
implicit def elvisOperator[B](alt: => B) = new {
def ?:[A >: B](pred: ?[A]) = pred getOrElse alt
}
type NonEmptyList[A] = ::[A]
def NonEmptyList[A](first: A, rest: A*) = new NonEmptyList(first, rest.toList)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment