Skip to content

Instantly share code, notes, and snippets.


Syed M Madani lambda-hacker

  • Hyderabad, India
View GitHub Profile
lambda-hacker / immutable-stack.scala
Last active Apr 26, 2017
Stack using List [Scala]
View immutable-stack.scala
// 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))