E
- the type of the elements held in this structure.public interface HeadTail<E> extends java.lang.Iterable<E>
This interface can be used to define any recursive sequence of object,
such as lists or generators. It is well suited to recursive algorithms, and
thus can be seen as an alternative to the Iterable
interface.
For example, one can compute the size of such an object with:
static int size(HeadTail> o) {
if (o.isEmpty()) { return 0; }
return 1 + size(o.tail());
}
or in Golo:
function size = |ht| -> match {
when ht: isEmpty() then 0
otherwhise 1 + size(ht: tail())
}
Note that the size
method is not provided since this interface
can be implemented by infinite generators.
A List
augmentation is provided, as well as corresponding special
methods on arrays (Object[]
/array[]
)
Modifier and Type | Method and Description |
---|---|
E |
head()
Get the head of the structure.
|
boolean |
isEmpty()
Checks if the structure is empty or not.
|
HeadTail<E> |
tail()
Get the tail of the structure.
|
static <E> java.lang.Iterable<E> |
toIterable(HeadTail<E> headTail)
Util method to wrap a
HeadTail instance into an Iterable |
E head()
null
if the structure is empty.HeadTail<E> tail()
To be side effect free, this method should return a deep copy of the original structure or an immutable view on it.
HeadTail
containing all the elements but the first, or a empty one
if no elements remains.boolean isEmpty()
true
if the structure contains no element, false
otherwise.static <E> java.lang.Iterable<E> toIterable(HeadTail<E> headTail)
HeadTail
instance into an Iterable
headTail
- the instance to wrap