Struct im::conslist::ConsList [] [src]

pub struct ConsList<A>(_);

An implementation of immutable proper cons lists.

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).

Items in the list are stored in Arcs, and insertion operations accept any value for which there's a Shared implementation into Arc<A>. Iterators and lookup operations, conversely, produce Arc<A>.

Methods

impl<A> ConsList<A>
[src]

[src]

Construct an empty list.

[src]

Construct a list with a single element.

[src]

Construct a list by consuming an IntoIterator.

Allows you to construct a list out of anything that implements the IntoIterator trait.

Time: O(n)

Examples

assert_eq!(
  ConsList::from(vec![1, 2, 3, 4, 5]),
  conslist![1, 2, 3, 4, 5]
);

[src]

Test whether a list is empty.

Time: O(1)

[src]

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

Time: O(1)

[src]

Get the first element of a list.

If the list is empty, None is returned.

Time: O(1)

[src]

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)

[src]

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)

[src]

[src]

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

[src]

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

[src]

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

[src]

Get an iterator over a list.

[src]

Sort a list using a comparator function.

Time: O(n log n)

impl ConsList<i32>
[src]

[src]

Construct a list of numbers between from and to inclusive.

Examples

assert_eq!(
  ConsList::range(1, 5),
  conslist![1, 2, 3, 4, 5]
);

impl<A> ConsList<A> where
    A: Ord
[src]

[src]

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

[src]

Sort a list.

Time: O(n log n)

Examples

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

Trait Implementations

impl<A> Clone for ConsList<A>
[src]

[src]

Clone a list.

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

Time: O(1)

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<A> Default for ConsList<A>
[src]

[src]

Default for lists is the empty list.

impl<A> PartialEq for ConsList<A> where
    A: PartialEq
[src]

[src]

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)

1.0.0
[src]

This method tests for !=.

impl<A> Eq for ConsList<A> where
    A: Eq
[src]

impl<A> Hash for ConsList<A> where
    A: Hash
[src]

[src]

Feeds this value into the given [Hasher]. Read more

1.3.0
[src]

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

impl<A> Debug for ConsList<A> where
    A: Debug
[src]

[src]

Formats the value using the given formatter.

impl<A> IntoIterator for ConsList<A>
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<A> Sum for ConsList<A>
[src]

[src]

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

impl<A, T> FromIterator<T> for ConsList<A> where
    T: Shared<A>, 
[src]

[src]

Creates a value from an iterator. Read more

impl<'a, A, R> From<&'a [R]> for ConsList<A> where
    &'a R: Shared<A>, 
[src]

[src]

Performs the conversion.

impl<A, R> From<Vec<R>> for ConsList<A> where
    R: Shared<A>, 
[src]

[src]

Performs the conversion.

impl<'a, A, R> From<&'a Vec<R>> for ConsList<A> where
    &'a R: Shared<A>, 
[src]

[src]

Performs the conversion.