Skip to content

Instantly share code, notes, and snippets.

@jordanlewis
Created January 31, 2012 07:02
Show Gist options
  • Save jordanlewis/1709315 to your computer and use it in GitHub Desktop.
Save jordanlewis/1709315 to your computer and use it in GitHub Desktop.
PFDS CustomStack
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