April 2017
Intermediate to advanced
316 pages
9h 33m
English
So far, we have implemented a linked list and a stack as a list. One of the key concepts in FP is the concept of lazy evaluation. We can make our list lazy so that the elements will be evaluated once we access them. We need to change node in such a way that it will return a function containing list as next, instead of the list itself. The function will be evaluated when it is called; therefore, our list will be lazy.
We start with modifying our node case. In our LinkedList example, next was of the LinkedList<Element> type. To make our list lazy, we will modify next to be a function that returns our list:
enum LazyList<Element: Equatable> { case end case node(data: Element, next: () -> LazyList<Element>) }
As we can see in the ...
Read now
Unlock full access