Created
January 31, 2012 07:02
-
-
Save jordanlewis/1709315 to your computer and use it in GitHub Desktop.
PFDS CustomStack
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
sealed abstract class CustomStack[+T] extends Stack[T] | |
case object EmptyCustomStack extends CustomStack[Nothing] { | |
def isEmpty = true | |
def cons[T](x: T) = new NonEmptyCustomStack[T](x, EmptyCustomStack) | |
def head = throw new IllegalArgumentException("Can't take head of an empty list") | |
def tail = throw new IllegalArgumentException("Can't take tail of an empty list") | |
} | |
final case class NonEmptyCustomStack[+T](head: T, tail: CustomStack[T]) extends CustomStack[T] { | |
def isEmpty = false | |
def cons[U >: T](x: U) = new NonEmptyCustomStack[U](x, this) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment