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

Cons

We need to have a way to append items to LinkedList; we name it cons.

cons refers to a fundamental function in most dialects of the Lisp programming language that constructs memory objects that hold two values or pointers to values.

It is loosely related to the object-oriented notion of a constructor/initializer, and more closely related to the constructor function of an algebraic data type system.

In FP jargon, operators that have a similar purpose, especially in the context of list or collection processing, are pronounced cons.

We implement cons as follows:

func cons(_ element: Element) -> LinkedList {     return .node(data: element, next: self) } 

This simple method appends the data to the front of LinkedList; in other words, it is ...

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