Created
October 2, 2014 19:33
-
-
Save bbq2100/06ed45e3a281e8ff7458 to your computer and use it in GitHub Desktop.
Scala-Options -> Type-variances, type bounds
This file contains hidden or 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
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