Comp202: Principles of Object-Oriented Programming II
Consider the following lists:
- (0, 1, 2, 3, 4, 5, ...)
- (7, 14, 21, 28, 35, 42, ...)
- (A, B, C, A, B, C, A, B, C, A, B, C, ...)
- (0, 1, 1, 2, 3, 5, 8, 13, ...)
- (a, b, ab, bab, abbab, bababbab, abbabbababbab, ...)
- (1, 2, 3, 5, 7, 11, 13, 17, 19, ...)
Question 1: What do all these lists have in common?
How about these lists:
Question 2: How are these lists similar to the first set of lists above?
So how can we represent such large, or possibly infinite, lists in our system?
Let us refer to our normal, fully instantiated lists as "eager" lists.
Consider first the following design constraints:
Consider this idea:
If an element of a list has not yet been accessed by any other object, can the rest of the system tell whether or not it even exists?
That is, if an object (the above mentioned list element) is not accessed by any other object, why bother instantiating it in the first place? That is, if no one is referencing an object, it does not need to be instantiated. This is very similar to the way in which the garbage collector works, except that the garbage collector un-instantiates an object after it has been used, and here we are talking about delaying an instantiation before an object is used.
The concept of delaying the instantiation of an object until just before it is used is called "lazy" instantiation. In general, the concept of delaying a computation until just before its result is needed is called "lazy evaluation". This is contrasted to the "eager" systems where the data is instantiated or computations are performed as soon as it is determined that such data or results might be needed later.
So, what we want to do is to design and build a lazy evaluated list where elements are instantiated only just before they are to be accessed for the first time.
The following is an updated presentation of our SIGCSE 2000 paper on "Design Patterns for Lazy Evaluation":
Please see our original SIGCSE 2000 paper on Design Patterns for Lazy Evaluation.
Last Revised Thursday, 03-Jun-2010 09:52:32 CDT
©2007 Stephen Wong and Dung Nguyen