O'Reilly logo

Swift Functional Programming - Second Edition by Dr. Fatih Nayebi

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Stacks

A stack is a collection that is based on the Last In First Out (LIFO) policy.

The following figure presents a sample stack:

To implement a simple functional stack, we need to provide push, pop, isEmpty, and size operations. We implemented a functional LinkedList in the previous section, which can be used to implement a simple functional stack with the following operations:

  • push: The cons operation in LinkedList
  • pop:
  • isEmpty: The isEmpty operation in LinkedList
  • size: The size method in LinkedList

As seen here, the only operation that is missing is pop. Let's implement that:

func pop() -> (element: Element, list: Stack)? {  switch ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required