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

Size

We can implement a computed size property to calculate the size of a linked list as follows:

var size: Int {     switch self {     case .node(_, let next):         return 1 + next.size     case .end:         return 0     } } 

This method recursively goes through LinkedList and counts the number of nodes:

print(functionalLinkedList.size) 

The result is going to be 3 in this example.

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