public class LazyList extends java.lang.Object implements java.util.Collection<java.lang.Object>, HeadTail<java.lang.Object>
A lazy list behaves like a linked list, but each next element is represented by a closure that is evaluated only if needed. The value is cached, so that the closure representing the tail is evaluated only once. Since the tail closure will be called at most once, and we can't guarantee when, or even if, it will be called, this closure must be a pure, side-effect free, function.
Modifier and Type | Field and Description |
---|---|
static LazyList |
EMPTY
Represents the empty list.
|
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
java.lang.Object element) |
boolean |
add(java.lang.Object e) |
boolean |
addAll(java.util.Collection<?> c) |
boolean |
addAll(int index,
java.util.Collection<?> c) |
java.util.List<java.lang.Object> |
asList()
Convert the lazy list into a regular list.
|
void |
clear() |
static LazyList |
cons(java.lang.Object head,
FunctionReference tail)
Create a new list from the head and tail values.
|
boolean |
contains(java.lang.Object o)
Check if the list contains the given object.
|
boolean |
containsAll(java.util.Collection<?> c)
Check if the list contains all the objects in the given collection.
|
Tuple |
destruct()
Destructuration helper.
|
boolean |
equals(java.lang.Object o)
Compares the specified object with this list.
|
java.lang.Object |
get(int index)
Returns the element at the specified position in this list.
|
int |
hashCode()
Compute the hashCode of this list.
|
java.lang.Object |
head()
Gets the first element of the list (its head).
|
int |
indexOf(java.lang.Object o)
Returns the position of the first occurrence of the given element in the
list.
|
boolean |
isEmpty()
Checks whether the list is empty or not.
|
java.util.Iterator<java.lang.Object> |
iterator()
Creates an iterator over the list.
|
java.lang.Object |
remove(int index) |
boolean |
remove(java.lang.Object e) |
boolean |
removeAll(java.util.Collection<?> c) |
boolean |
retainAll(java.util.Collection<?> c) |
java.lang.Object |
set(int index,
java.lang.Object element) |
int |
size()
Returns the number of elements in this list.
|
LazyList |
tail()
Gets the rest of the list (its tail).
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this list.
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list with a type
of the given array.
|
java.lang.String |
toString() |
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
toIterable
public static LazyList cons(java.lang.Object head, FunctionReference tail)
head
- the first value of the list.tail
- a FunctionReference
that returns a LazyList when invoked.LazyList
public java.lang.Object head()
public boolean isEmpty()
public java.util.Iterator<java.lang.Object> iterator()
The iterator does not support removal.
iterator
in interface java.lang.Iterable<java.lang.Object>
iterator
in interface java.util.Collection<java.lang.Object>
public java.util.List<java.lang.Object> asList()
Note that it evaluates the whole list. Take care to not use this method on infinite lists, since no check is done.
LinkedList
public int size()
Note that it evaluates the whole list. Take care to not use this method on infinite lists, since no check is done.
size
in interface java.util.Collection<java.lang.Object>
public boolean equals(java.lang.Object o)
This is a value comparison. Note that it may evaluate the whole list. Take care to not use this method on infinite lists, since no check is done.
equals
in interface java.util.Collection<java.lang.Object>
equals
in class java.lang.Object
o
- the object to be compared for equality with this listtrue
if the specified object is equal to this list.public int hashCode()
Note that it evaluates the whole list. Take care to not use this method on infinite lists, since no check is done.
hashCode
in interface java.util.Collection<java.lang.Object>
hashCode
in class java.lang.Object
hashCode
.public java.lang.Object[] toArray()
Note that it evaluates the whole list. Take care to not use this method on infinite lists, since no check is done.
toArray
in interface java.util.Collection<java.lang.Object>
public <T> T[] toArray(T[] a)
Note that it evaluates the whole list. Take care to not use this method on infinite lists, since no check is done.
toArray
in interface java.util.Collection<java.lang.Object>
public java.lang.Object get(int index)
Note that it evaluates the list up to the required element.
index
- index of the element to returnpublic int indexOf(java.lang.Object o)
Note that it evaluates the list up to the given element. Take care to not use this method on infinite lists, since no check is done.
o
- element to search forpublic boolean contains(java.lang.Object o)
Note that it evaluates the list up to the given element. Take care to not use this method on infinite lists, since no check is done.
contains
in interface java.util.Collection<java.lang.Object>
o
- element to search fortrue
if the element is in the list, false
otherwise.public boolean containsAll(java.util.Collection<?> c)
Note that it evaluates the list up to the given element, *for each* element in the collection (at worse). This implementation is highly inefficient.
Take care to not use this method on infinite lists, since no check is done.
containsAll
in interface java.util.Collection<java.lang.Object>
c
- collection of elements to search fortrue
if all the elements are in the list, false
otherwise.public java.lang.String toString()
toString
in class java.lang.Object
public boolean add(java.lang.Object e)
add
in interface java.util.Collection<java.lang.Object>
public void add(int index, java.lang.Object element)
public boolean addAll(java.util.Collection<?> c)
addAll
in interface java.util.Collection<java.lang.Object>
public boolean addAll(int index, java.util.Collection<?> c)
public boolean remove(java.lang.Object e)
remove
in interface java.util.Collection<java.lang.Object>
public java.lang.Object remove(int index)
public boolean removeAll(java.util.Collection<?> c)
removeAll
in interface java.util.Collection<java.lang.Object>
public boolean retainAll(java.util.Collection<?> c)
retainAll
in interface java.util.Collection<java.lang.Object>
public void clear()
clear
in interface java.util.Collection<java.lang.Object>
public java.lang.Object set(int index, java.lang.Object element)