Skip to content

Instantly share code, notes, and snippets.

View lambda-hacker's full-sized avatar

Syed M Madani lambda-hacker

  • Hyderabad, India
View GitHub Profile
@lambda-hacker
lambda-hacker / immutable-stack.scala
Last active April 26, 2017 07:59
Stack using List [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))