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.