Skip to content

Instantly share code, notes, and snippets.

@bbq2100
Created October 2, 2014 19:33
Show Gist options
  • Save bbq2100/06ed45e3a281e8ff7458 to your computer and use it in GitHub Desktop.
Save bbq2100/06ed45e3a281e8ff7458 to your computer and use it in GitHub Desktop.
Scala-Options -> Type-variances, type bounds
def position[A](list: List[A], element: A) = {
val index: Int = list indexOf (element)
if (index == -1) Nil
else Just(index)
}
sealed abstract class Maybe[+A] {
def isEmpty: Boolean
def get: A
def getOrElse[B >: A](default: B): B = {
if(isEmpty) default else get
}
}
final case class Just[A](value: A) extends Maybe[A] {
override def isEmpty: Boolean = false
override def get = value
}
case object Nil extends Maybe[Nothing] {
override def isEmpty: Boolean = true
override def get: Nothing = throw new NoSuchElementException("Nil.Get^^")
}
position(List("A", "B", "C"), "B").getOrElse(-1)
val strings: List[String] = List("A", "B")
val everything: List[Any] = strings
1 :: everything
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment