Interface LList<T>
- Type Parameters:
T
- type of the list items
- All Known Implementing Classes:
LList.ImmutableList
,LList.Nil
Defines an immutable list structure as used in Lisp, Scheme, or Clojure. The fundamental data structure in many functional languages is the immutable list. It is constructed
from two building blocks:
- Nil
- The empty list
- Cons
- A cell containing an element also known as first; and a pointer to the rest of the list also known as tail.
The term cons comes from functional programming language Lisp or Scheme. These kind of data structures are called persistent data structures. Since the data structure is immutable, one might think that a lot of memory would be consumed since a new list would be created when we try to perform various operations on list. On the contrary, these kind of data structures allow structural sharing by sharing nodes. A persistent data structure does preserve the previous version of itself when being modified and is therefore effectively immutable. Fully persistent data structures allow both updates and queries on any version.
-
Nested Class Summary
Modifier and TypeInterfaceDescriptionstatic final record
static class
Define a split iterator for the immutable persistent list structure.static final class
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> LList
<T> Return a new immutable list with a new item inserted in the front.copy()
first()
Return the first of the list.boolean
isEmpty()
Return true if the list is empty otherwise false.default int
length()
static <T> LList
<T> list
(T... items) default <R> LList
<R> static <T> LList
<T> nil()
Return an empty listrest()
Return the tail of the list.stream()
-
Method Details
-
nil
Return an empty list- Type Parameters:
T
- Type of the items in the list- Returns:
- empty list
-
cons
Return a new immutable list with a new item inserted in the front.- Type Parameters:
T
- Type of the items in the list- Parameters:
first
- new item to add at the beginning of the listtail
- list to which the item is added- Returns:
- immutable list with the new item at the beginning
-
list
-
isEmpty
boolean isEmpty()Return true if the list is empty otherwise false.- Returns:
- true if the list is empty
-
first
T first()Return the first of the list. Is equivalent to the car Scheme operation.- Returns:
- first item of the list
-
rest
-
length
default int length() -
copy
-
append
-
map
-
stream
-