Struct sp_im::conslist::ConsList[][src]

pub struct ConsList<A>(_);
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

Construct an empty list.

Construct a list with a single element.

Test whether a list is empty.

Time: O(1)

Construct a list with a new value prepended to the front of the current list.

Time: O(1)

Get the first element of a list.

If the list is empty, None is returned.

Time: O(1)

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)

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)

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)

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());

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
]);

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]);

Get an iterator over a list.

Sort a list using a comparator function.

Time: O(n log n)

Compare the Arc pointers of two ConsList

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
]);

Sort a list.

Time: O(n log n)

Examples

assert_eq!(
  conslist![2, 8, 1, 6, 3, 7, 5, 4].sort(),
  ConsList::from_iter(1..9)
);

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

The resulting type after applying the + operator.

Performs the + operation. Read more

Clone a list.

Cons cells use Arc behind the scenes, so this is no more expensive than cloning an Arc reference.

Time: O(1)

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Default for lists is the empty list.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Creates a value from an iterator. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

Which kind of iterator are we turning this into?

The type of the elements being iterated over.

Creates an iterator from a value. Read more

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)

This method tests for !=.

Method which takes an iterator and generates Self from the elements by “summing up” the items. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Get a new Arc pointer for this value

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.