Class in collections::list coil-stdlib

List

class List<T>
Import
use collections::list::{List};
Source: src/collections/list.hy

A singly-linked list with constant-time push and pop at the front.

push_back and pop_back walk the whole list, so use collections::deque::VecDeque when you need both ends to be fast. for x in list visits the values front to back.

Associated functions#

NameDescription
newCreates an empty list.

Methods#

NameDescription
sizeReturns the number of values.
is_emptyReturns true when the list has no values.
push_frontAdds v at the front.
push_backAdds v at the back.
peek_frontReturns the front value without removing it, or None when the list is empty.
pop_frontRemoves and returns the front value, or None when the list is empty.
pop_backRemoves and returns the back value, or None when the list is empty.
clearRemoves every value.
to_vecReturns the values front to back as a new Vec.
iterReturns an iterator over the values, front to back.

Trait implementations#

Invariants#

Checked according to --contracts (see Contracts).

invariant self.len >= 0

Examples#

use collections::list::{List};
use tests::assert;
let xs: List<int> = List::new();
xs.push_front(2);
xs.push_front(1);
xs.push_back(3);
assert!(xs.size() == 3);
let sum = 0;
for x in xs {
    sum = sum * 10 + x;
}
assert!(sum == 123);
assert!((xs.pop_front() ?? 0) == 1);

See also#

↑ ↓ navigate ↵ open