1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! A standard linked list
21 MutCons(T, @mut MutList<T>),
25 /// Create a list from a vector
26 pub fn from_vec<T:Copy>(v: &[T]) -> @List<T> {
27 vec::foldr(v, @Nil::<T>, |h, t| @Cons(*h, t))
33 * Applies `f` to `u` and the first element in the list, then applies `f` to
34 * the result of the previous call and the second element, and so on,
35 * returning the accumulated result.
39 * * ls - The list to fold
40 * * z - The initial value
41 * * f - The function to apply
43 pub fn foldl<T:Copy,U>(z: T, ls: @List<U>, f: &fn(&T, &U) -> T) -> T {
45 do iter(ls) |elt| { accum = f(&accum, elt);}
50 * Search for an element that matches a given predicate
52 * Apply function `f` to each element of `v`, starting from the first.
53 * When function `f` returns true then an option containing the element
54 * is returned. If `f` matches no elements then none is returned.
56 pub fn find<T:Copy>(ls: @List<T>, f: &fn(&T) -> bool) -> Option<T> {
61 if f(hd) { return Some(*hd); }
69 /// Returns true if a list contains an element with the given value
70 pub fn has<T:Copy + Eq>(ls: @List<T>, elt: T) -> bool {
72 if *e == elt { return true; }
77 /// Returns true if the list is empty
78 pub fn is_empty<T:Copy>(ls: @List<T>) -> bool {
85 /// Returns the length of a list
86 pub fn len<T>(ls: @List<T>) -> uint {
88 iter(ls, |_e| count += 1u);
92 /// Returns all but the first element of a list
93 pub fn tail<T:Copy>(ls: @List<T>) -> @List<T> {
95 Cons(_, tl) => return tl,
96 Nil => fail!(~"list empty")
100 /// Returns the first element of a list
101 pub fn head<T:Copy>(ls: @List<T>) -> T {
103 Cons(copy hd, _) => hd,
105 _ => fail!(~"head invoked on empty list")
109 /// Appends one list to another
110 pub fn append<T:Copy>(l: @List<T>, m: @List<T>) -> @List<T> {
113 Cons(copy x, xs) => {
114 let rest = append(xs, m);
115 return @Cons(x, rest);
121 /// Push one element into the front of a list, returning a new list
122 /// THIS VERSION DOESN'T ACTUALLY WORK
123 fn push<T:Copy>(ll: &mut @list<T>, vv: T) {
124 ll = &mut @cons(vv, *ll)
128 /// Iterate over a list
129 pub fn iter<T>(l: @List<T>, f: &fn(&T)) {
133 Cons(ref hd, tl) => {
142 /// Iterate over a list
143 pub fn each<T>(l: @List<T>, f: &fn(&T) -> bool) {
147 Cons(ref hd, tl) => {
148 if !f(hd) { return; }
157 /// Iterate over a mutable list
158 pub fn each(@mut self, f: &fn(&mut T) -> bool) {
161 let borrowed = &mut *cur;
162 cur = match *borrowed {
163 MutCons(ref mut hd, tl) => {
184 let empty : @list::List<int> = from_vec(~[]);
185 let full1 = from_vec(~[1]);
186 let full2 = from_vec(~['r', 'u']);
188 assert!(is_empty(empty));
189 assert!(!is_empty(full1));
190 assert!(!is_empty(full2));
195 let l = from_vec(~[0, 1, 2]);
197 assert!((head(l) == 0));
199 let tail_l = tail(l);
200 assert!((head(tail_l) == 1));
202 let tail_tail_l = tail(tail_l);
203 assert!((head(tail_tail_l) == 2));
207 fn test_from_vec_empty() {
208 let empty : @list::List<int> = from_vec(~[]);
209 assert!((empty == @list::Nil::<int>));
214 fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); }
215 let l = from_vec(~[0, 1, 2, 3, 4]);
216 let empty = @list::Nil::<int>;
217 assert!((list::foldl(0u, l, add) == 10u));
218 assert!((list::foldl(0u, empty, add) == 0u));
223 fn sub(a: &int, b: &int) -> int {
226 let l = from_vec(~[1, 2, 3, 4]);
227 assert!((list::foldl(0, l, sub) == -10));
231 fn test_find_success() {
232 fn match_(i: &int) -> bool { return *i == 2; }
233 let l = from_vec(~[0, 1, 2]);
234 assert!((list::find(l, match_) == option::Some(2)));
238 fn test_find_fail() {
239 fn match_(_i: &int) -> bool { return false; }
240 let l = from_vec(~[0, 1, 2]);
241 let empty = @list::Nil::<int>;
242 assert!((list::find(l, match_) == option::None::<int>));
243 assert!((list::find(empty, match_) == option::None::<int>));
248 let l = from_vec(~[5, 8, 6]);
249 let empty = @list::Nil::<int>;
250 assert!((list::has(l, 5)));
251 assert!((!list::has(l, 7)));
252 assert!((list::has(l, 8)));
253 assert!((!list::has(empty, 5)));
258 let l = from_vec(~[0, 1, 2]);
259 let empty = @list::Nil::<int>;
260 assert!((list::len(l) == 3u));
261 assert!((list::len(empty) == 0u));
266 assert!(from_vec(~[1,2,3,4])
267 == list::append(list::from_vec(~[1,2]), list::from_vec(~[3,4])));