April 2017
Intermediate to advanced
316 pages
9h 33m
English
We can implement a computed property to provide an array of elements as follows:
var elements: [Element] { switch self { case .node(let data, let next): return [data] + next.elements case .end: return [] } }
Here, we recur through LinkedList and return an array of data. We will be able to use this property as follows:
print(functionalLinkedList.elements)
This statement prints [3, 2, 1].
Read now
Unlock full access