Enum seax_util::list::List [] [src]

pub enum List<T> {
    Cons(T, Box<List<T>>),
    Nil,
}

Singly-linked cons list.

This is used internally to represent list primitives in the Seax virtual machine.

Variants

Cons cell containing a T and a link to the tail

The empty list.

Methods

impl<T> List<T>
[src]

Public implementation for List.

Creates a new empty list

Prepends the given item to the list.

This is an O(1) operation.

Arguments

  • item - the item to prepend

Return Value

  • The list with the new head item prepended

Examples

let mut a_list: List<isize> = List::new();
a_list = a_list.prepend(1);
assert_eq!(a_list, list![1]);

a_list = a_list.prepend(2);
assert_eq!(a_list, list![2,1]);

Appends an item to the end of the list.

This is an O(n) operation.

Arguments

  • item - the item to append

Examples

let mut a_list: List<isize> = List::new();
a_list.append(1);
assert_eq!(a_list, list![1]);

a_list.append(2);
assert_eq!(a_list, list![1,2]);

Appends an item to the end of the list.

Returns the last element of the list to support 'append chaining' of a large number of items; this allows you to omit a complete traversal of the list for every append and should be used in situations such as folds.

The first append is still O(n), but long as you hang on to your pointer to the tail, subsequent appends should all be O(1). However, this requires you to keep a &mut pointer to the list, so use it sparingly, especially in situations of concurrent access.

Arguments

  • item - the item to append

Examples

let mut a_list: List<isize> = List::new();

    // this is a function so that the `&mut` borrow is released.
    fn append_two_items<T>(l: &mut List<T>, first: T, second: T) {
        l.append_chain(first).append_chain(second);
    }

append_two_items(&mut a_list, 1, 2);
assert_eq!(a_list, list![1,2]);

Returns the length of the list.

Examples

let a_list = list!(1,2,3,4);
assert_eq!(a_list.length(), 4)

Returns true if the list is empty, false otherwise.

Examples

let list = list!(1,2,3,4);
assert_eq!(list.is_empty(), false)
let list: List<isize> = Nil;
assert_eq!(list.is_empty(), true)

Returns the tail of the list from this element.

Examples

let list = list!(1,2,3,4);
assert_eq!(list.tail(), &list!(2,3,4))

Provide a forward iterator

Returns the last element of the list

Examples

let a_list = list!(1,2,3,4);
assert_eq!(a_list.last(), &4)

Optionally index the list.

Unlike list indexing syntax (list[i]), this returns None if the index is out of bound rather than panicking.

Lists are zero-indexed, so just as when using list indexing syntax, the head of the list is index 0 and the last element of the list is index (length - 1).

Arguments

  • idx - the index to attempt to access

Return Value

  • Some(&T) if the index exists within the list, None otherwise.

Examples

let a_list = list!(1,2,3,4);
assert_eq!(a_list.get(1), Some(&2));
assert_eq!(a_list.get(3), Some(&4));
assert_eq!(a_list.get(10), None);

Trait Implementations

impl<T> Stack<T> for List<T>
[src]

Stack implementation using a cons list

Push an item to the top of the stack, returning a new stack.

Examples:


let mut s: List<isize> = Stack::empty();
assert_eq!(s.peek(), None);
s = s.push(1);
assert_eq!(s.peek(), Some(&1));
s = s.push(6);
assert_eq!(s.peek(), Some(&6));

Pop the top element of the stack.

Pop the top element of the stack. Returns an Option<(T,List<T>)> containing the top element and a new List<T> with that item removed, or None if the stack is empty.

Examples:


let mut s: List<isize> = Stack::empty();
s = s.push(2);
s = s.push(1);
let pop_result = s.pop().unwrap();
s = pop_result.1;
assert_eq!(s.peek(), Some(&2));
assert_eq!(pop_result.0, 1);

Returns an empty stack.

Peek at the top element of the stack.

Peek at the top element of the stack. Returns an Option<&T> with a borrowed pointer to the top element, or None if the stack is empty.

Examples:


let mut s: List<isize> = Stack::empty();
s = s.push(2);
s = s.push(1);
let pop_result = s.pop().unwrap();
s = pop_result.1;
assert_eq!(s.peek(), Some(&2));
assert_eq!(pop_result.0, 1);

impl<T: PartialEq> PartialEq for List<T>
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl<T: Eq> Eq for List<T>
[src]

impl<T: Hash> Hash for List<T>
[src]

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

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

impl<T: Clone> Clone for List<T>
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl<'a, T> Display for List<T> where
    T: Display
[src]

Formats the value using the given formatter. Read more

impl<'a, T> Debug for List<T> where
    T: Debug
[src]

Formats the value using the given formatter.

impl<T> Default for List<T>
[src]

Returns the "default value" for a type. Read more

impl<T> FromIterator<T> for List<T>
[src]

Build a List<T> from a structure implementing IntoIterator<T>.

This takes advantage of the List.append_chain() method under the hood to provide roughly O(n) performance.

Examples

let mut a_vec = vec![1,2,3,4];
let another_vec = a_vec.clone();
let a_list = List::from_iter(a_vec);
for i in 0..a_list.length() {
    assert_eq!(a_list[i], another_vec[i])
}

impl<T> Index<usize> for List<T>
[src]

Implementation of indexing for List<T>.

Examples:

let list: List<isize> = list!(1,2,3,4,5,6);
assert_eq!(list[0usize], 1);

The returned type after indexing

The method for the indexing (container[index]) operation

impl<T> Index<u64> for List<T>
[src]

Implementation of indexing for List<T>.

Examples:

let list = list!(1,2,3,4,5,6);
assert_eq!(list[0usize], 1);

The returned type after indexing

The method for the indexing (container[index]) operation

impl<T> Index<i64> for List<T>
[src]

Implementation of indexing for List<T>.

Examples:

let list = list!(1,2,3,4,5,6);
assert_eq!(list[0isize], 1);

The returned type after indexing

The method for the indexing (container[index]) operation

impl<T> Index<isize> for List<T>
[src]

Implementation of indexing for List<T>.

Examples:

let list = list!(1,2,3,4,5,6);
assert_eq!(list[0isize], 1);

The returned type after indexing

The method for the indexing (container[index]) operation

impl<T> Encode for List<T> where
    T: Encode
[src]

Encodes this object to a list of bytes.