This file contains 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
// Immutable Stack Type using List | |
class Stack[A] (val elems: List[A] = List.empty[A]) { | |
def push(v: A) : Stack[A] = new Stack(v :: elems) | |
def pushAll(xs: Iterable[A]) : Stack[A] = | |
xs.foldLeft (this) ((accStack, e) => accStack.push(e)) | |
def pop() : Stack[A] = new Stack(elems.tail) | |
def pop2() : (A, Stack[A]) = (elems.head, new Stack(elems.tail)) |