Created
January 31, 2012 06:50
-
-
Save jordanlewis/1709288 to your computer and use it in GitHub Desktop.
PFDS CustomListStack
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 LIST[+T] | |
case object NIL extends LIST[Nothing] | |
final case class CONS[T](head: T, tail: LIST[T]) extends LIST[T] | |
object CustomListStack { | |
def apply[T] = new CustomListStack[T](NIL) | |
} | |
class CustomListStack[T] private (private val l: LIST[T]) extends Stack[T] { | |
def isEmpty = l match { | |
case NIL => true | |
case CONS(_, _) => false | |
} | |
def cons[U >: T](x: U) = new CustomListStack[U](new CONS[U](x, l)) | |
def head = l match { | |
case NIL => throw new IllegalArgumentException("Fail") | |
case CONS(x, _) => x | |
} | |
def tail = l match { | |
case NIL => throw new IllegalArgumentException("Fail") | |
case CONS(_, x) => new CustomListStack[T](x) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment