pub struct ConsList<A>(/* private fields */);Expand description
An immutable proper cons lists.
The cons list is perhaps the most basic immutable data structure:
a singly linked list built out of ‘cons cells,’ which are cells
containing two values, the left hand value being the head of the
list and the right hand value being a reference to the rest of the
list, or a Nil value denoting the end of the list.
Structure can be shared between lists (and is reference counted), and append to the front of a list is O(1). Cons cells keep track of the length of the list at the current position, as an extra optimisation, so getting the length of a list is also O(1). Otherwise, operations are generally O(n).
Implementations§
Source§impl<A> ConsList<A>
impl<A> ConsList<A>
Sourcepub fn singleton<R>(v: R) -> ConsList<A>where
R: Shared<A>,
pub fn singleton<R>(v: R) -> ConsList<A>where
R: Shared<A>,
Construct a list with a single element.
Sourcepub fn cons<R>(&self, car: R) -> ConsList<A>where
R: Shared<A>,
pub fn cons<R>(&self, car: R) -> ConsList<A>where
R: Shared<A>,
Construct a list with a new value prepended to the front of the current list.
Time: O(1)
Sourcepub fn head(&self) -> Option<Arc<A>>
pub fn head(&self) -> Option<Arc<A>>
Get the first element of a list.
If the list is empty, None is returned.
Time: O(1)
Sourcepub fn tail(&self) -> Option<ConsList<A>>
pub fn tail(&self) -> Option<ConsList<A>>
Get the tail of a list.
The tail means all elements in the list after the first item
(the head). If the list only has one element, the result is an
empty list. If the list is empty, the result is None.
Time: O(1)
Sourcepub fn uncons(&self) -> Option<(Arc<A>, ConsList<A>)>
pub fn uncons(&self) -> Option<(Arc<A>, ConsList<A>)>
Get the head and the tail of a list.
This function performs both the head function and
the tail function in one go, returning a tuple of
the head and the tail, or None if the list is empty.
§Examples
This can be useful when pattern matching your way through a list:
fn walk_through_list<A>(list: &ConsList<A>)
where A: Debug {
match list.uncons() {
None => (),
Some((ref head, ref tail)) => {
print!("{:?}", head);
walk_through_list(tail)
}
}
}Time: O(1)
Sourcepub fn uncons2(&self) -> Option<(Arc<A>, Arc<A>, ConsList<A>)>
pub fn uncons2(&self) -> Option<(Arc<A>, Arc<A>, ConsList<A>)>
Get the two first elements and the tail of a list.
This function performs both the head function twice and
the tail function in one go, returning a tuple of
the double head and the tail, or None if the list is empty.
§Examples
This can be useful when pattern matching your way through a list:
fn walk_through_list<A>(list: &ConsList<A>)
where A: Debug {
match list.uncons2() {
None => (),
Some((ref head, ref head2, ref tail)) => {
print!("{:?} {:?}", head, head2);
walk_through_list(tail)
}
}
}Time: O(1)
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Get the length of a list.
This operation is instant, because cons cells store the length of the list they’re the head of.
Time: O(1)
§Examples
assert_eq!(5, conslist![1, 2, 3, 4, 5].len());Sourcepub fn append<R>(&self, right: R) -> Selfwhere
R: Borrow<Self>,
pub fn append<R>(&self, right: R) -> Selfwhere
R: Borrow<Self>,
Append the list right to the end of the current list.
Time: O(n)
§Examples
assert_eq!(conslist![1, 2, 3].append(conslist![7, 8, 9]), conslist![
1, 2, 3, 7, 8, 9
]);Sourcepub fn reverse(&self) -> ConsList<A>
pub fn reverse(&self) -> ConsList<A>
Construct a list which is the reverse of the current list.
Time: O(n)
§Examples
assert_eq!(conslist![1, 2, 3, 4, 5].reverse(), conslist![5, 4, 3, 2, 1]);Sourcepub fn sort_by<F>(&self, cmp: F) -> ConsList<A>
pub fn sort_by<F>(&self, cmp: F) -> ConsList<A>
Sort a list using a comparator function.
Time: O(n log n)
Sourcepub fn insert<T>(&self, item: T) -> ConsList<A>
pub fn insert<T>(&self, item: T) -> ConsList<A>
Insert an item into a sorted list.
Constructs a new list with the new item inserted before the
first item in the list which is larger than the new item,
as determined by the Ord trait.
Time: O(n)
§Examples
assert_eq!(conslist![2, 4, 6].insert(5).insert(1).insert(3), conslist![
1, 2, 3, 4, 5, 6
]);Trait Implementations§
Source§impl<A, T> FromIterator<T> for ConsList<A>where
T: Shared<A>,
impl<A, T> FromIterator<T> for ConsList<A>where
T: Shared<A>,
Source§fn from_iter<I>(source: I) -> Selfwhere
I: IntoIterator<Item = T>,
fn from_iter<I>(source: I) -> Selfwhere
I: IntoIterator<Item = T>,
Source§impl<A> IntoIterator for ConsList<A>
impl<A> IntoIterator for ConsList<A>
Source§impl<A> PartialEq for ConsList<A>where
A: PartialEq,
impl<A> PartialEq for ConsList<A>where
A: PartialEq,
Source§fn eq(&self, other: &ConsList<A>) -> bool
fn eq(&self, other: &ConsList<A>) -> bool
Test if two lists are equal.
This could potentially be an expensive operation, as we need to walk both lists to test for equality. We can very quickly determine equality if the lists have different lengths (can’t be equal). Otherwise, we walk the lists to compare values.
Time: O(n)
impl<A> Eq for ConsList<A>where
A: Eq,
Auto Trait Implementations§
impl<A> Freeze for ConsList<A>
impl<A> RefUnwindSafe for ConsList<A>where
A: RefUnwindSafe,
impl<A> Send for ConsList<A>
impl<A> Sync for ConsList<A>
impl<A> Unpin for ConsList<A>
impl<A> UnwindSafe for ConsList<A>where
A: RefUnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more