From 4c553af0f80c5693d832f43ce7021abba15c4535 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Sat, 15 Feb 2014 02:03:43 -0400 Subject: [PATCH 01/18] Replaced @List with ~List in Rust's recursive example --- src/doc/rust.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/rust.md b/src/doc/rust.md index 9bc394abf5e5c..bb299b107c7db 100644 --- a/src/doc/rust.md +++ b/src/doc/rust.md @@ -3256,10 +3256,10 @@ An example of a *recursive* type and its use: ~~~~ enum List { Nil, - Cons(T, @List) + Cons(T, ~List) } -let a: List = Cons(7, @Cons(13, @Nil)); +let a: List = Cons(7, ~Cons(13, ~Nil)); ~~~~ ### Pointer types From 1e6151a77067d3fcb9a101cb5ee66e7252571845 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Fri, 14 Feb 2014 14:59:13 -0400 Subject: [PATCH 02/18] Renamed variables --- src/libcollections/list.rs | 132 ++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 67 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 0dc13aa2b49e2..8174d12cce70b 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -10,8 +10,6 @@ //! A standard, garbage-collected linked list. - - #[deriving(Clone, Eq)] #[allow(missing_doc)] pub enum List { @@ -33,30 +31,30 @@ pub fn from_vec(v: &[T]) -> @List { * * # Arguments * - * * ls - The list to fold + * * list - The list to fold * * z - The initial value * * f - The function to apply */ -pub fn foldl(z: T, ls: @List, f: |&T, &U| -> T) -> T { +pub fn foldl(z: T, list: @List, f: |&T, &U| -> T) -> T { let mut accum: T = z; - iter(ls, |elt| accum = f(&accum, elt)); + iter(list, |element| accum = f(&accum, element)); accum } /** * Search for an element that matches a given predicate * - * Apply function `f` to each element of `ls`, starting from the first. + * Apply function `f` to each element of `list`, starting from the first. * When function `f` returns true then an option containing the element * is returned. If `f` matches no elements then none is returned. */ -pub fn find(ls: @List, f: |&T| -> bool) -> Option { - let mut ls = ls; +pub fn find(list: @List, f: |&T| -> bool) -> Option { + let mut list = list; loop { - ls = match *ls { - Cons(ref hd, tl) => { - if f(hd) { return Some((*hd).clone()); } - tl + list = match *list { + Cons(ref head, tail) => { + if f(head) { return Some((*head).clone()); } + tail } Nil => return None } @@ -66,17 +64,17 @@ pub fn find(ls: @List, f: |&T| -> bool) -> Option { /** * Returns true if a list contains an element that matches a given predicate * - * Apply function `f` to each element of `ls`, starting from the first. + * Apply function `f` to each element of `list`, starting from the first. * When function `f` returns true then it also returns true. If `f` matches no * elements then false is returned. */ -pub fn any(ls: @List, f: |&T| -> bool) -> bool { - let mut ls = ls; +pub fn any(list: @List, f: |&T| -> bool) -> bool { + let mut list = list; loop { - ls = match *ls { - Cons(ref hd, tl) => { - if f(hd) { return true; } - tl + list = match *list { + Cons(ref head, tail) => { + if f(head) { return true; } + tail } Nil => return false } @@ -84,53 +82,53 @@ pub fn any(ls: @List, f: |&T| -> bool) -> bool { } /// Returns true if a list contains an element with the given value -pub fn has(ls: @List, elt: T) -> bool { +pub fn has(list: @List, element: T) -> bool { let mut found = false; - each(ls, |e| { - if *e == elt { found = true; false } else { true } + each(list, |e| { + if *e == element { found = true; false } else { true } }); return found; } /// Returns true if the list is empty -pub fn is_empty(ls: @List) -> bool { - match *ls { +pub fn is_empty(list: @List) -> bool { + match *list { Nil => true, _ => false } } /// Returns the length of a list -pub fn len(ls: @List) -> uint { +pub fn len(list: @List) -> uint { let mut count = 0u; - iter(ls, |_e| count += 1u); + iter(list, |_e| count += 1u); count } /// Returns all but the first element of a list -pub fn tail(ls: @List) -> @List { - match *ls { - Cons(_, tl) => return tl, +pub fn tail(list: @List) -> @List { + match *list { + Cons(_, tail) => return tail, Nil => fail!("list empty") } } /// Returns the first element of a list -pub fn head(ls: @List) -> T { - match *ls { - Cons(ref hd, _) => (*hd).clone(), +pub fn head(list: @List) -> T { + match *list { + Cons(ref head, _) => (*head).clone(), // makes me sad _ => fail!("head invoked on empty list") } } /// Appends one list to another -pub fn append(l: @List, m: @List) -> @List { - match *l { - Nil => return m, - Cons(ref x, xs) => { - let rest = append(xs, m); - return @Cons((*x).clone(), rest); +pub fn append(list: @List, other: @List) -> @List { + match *list { + Nil => return other, + Cons(ref head, tail) => { + let rest = append(tail, other); + return @Cons((*head).clone(), rest); } } } @@ -144,13 +142,13 @@ fn push(ll: &mut @list, vv: T) { */ /// Iterate over a list -pub fn iter(l: @List, f: |&T|) { - let mut cur = l; +pub fn iter(list: @List, f: |&T|) { + let mut cur = list; loop { cur = match *cur { - Cons(ref hd, tl) => { - f(hd); - tl + Cons(ref head, tail) => { + f(head); + tail } Nil => break } @@ -158,13 +156,13 @@ pub fn iter(l: @List, f: |&T|) { } /// Iterate over a list -pub fn each(l: @List, f: |&T| -> bool) -> bool { - let mut cur = l; +pub fn each(list: @List, f: |&T| -> bool) -> bool { + let mut cur = list; loop { cur = match *cur { - Cons(ref hd, tl) => { - if !f(hd) { return false; } - tl + Cons(ref head, tail) => { + if !f(head) { return false; } + tail } Nil => { return true; } } @@ -191,11 +189,11 @@ mod tests { #[test] fn test_from_vec() { - let l = from_vec([0, 1, 2]); + let list = from_vec([0, 1, 2]); - assert_eq!(head(l), 0); + assert_eq!(head(list), 0); - let tail_l = tail(l); + let tail_l = tail(list); assert_eq!(head(tail_l), 1); let tail_tail_l = tail(tail_l); @@ -211,9 +209,9 @@ mod tests { #[test] fn test_foldl() { fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); } - let l = from_vec([0, 1, 2, 3, 4]); + let list = from_vec([0, 1, 2, 3, 4]); let empty = @list::Nil::; - assert_eq!(list::foldl(0u, l, add), 10u); + assert_eq!(list::foldl(0u, list, add), 10u); assert_eq!(list::foldl(0u, empty, add), 0u); } @@ -222,50 +220,50 @@ mod tests { fn sub(a: &int, b: &int) -> int { *a - *b } - let l = from_vec([1, 2, 3, 4]); - assert_eq!(list::foldl(0, l, sub), -10); + let list = from_vec([1, 2, 3, 4]); + assert_eq!(list::foldl(0, list, sub), -10); } #[test] fn test_find_success() { fn match_(i: &int) -> bool { return *i == 2; } - let l = from_vec([0, 1, 2]); - assert_eq!(list::find(l, match_), option::Some(2)); + let list = from_vec([0, 1, 2]); + assert_eq!(list::find(list, match_), option::Some(2)); } #[test] fn test_find_fail() { fn match_(_i: &int) -> bool { return false; } - let l = from_vec([0, 1, 2]); + let list = from_vec([0, 1, 2]); let empty = @list::Nil::; - assert_eq!(list::find(l, match_), option::None::); + assert_eq!(list::find(list, match_), option::None::); assert_eq!(list::find(empty, match_), option::None::); } #[test] fn test_any() { fn match_(i: &int) -> bool { return *i == 2; } - let l = from_vec([0, 1, 2]); + let list = from_vec([0, 1, 2]); let empty = @list::Nil::; - assert_eq!(list::any(l, match_), true); + assert_eq!(list::any(list, match_), true); assert_eq!(list::any(empty, match_), false); } #[test] fn test_has() { - let l = from_vec([5, 8, 6]); + let list = from_vec([5, 8, 6]); let empty = @list::Nil::; - assert!((list::has(l, 5))); - assert!((!list::has(l, 7))); - assert!((list::has(l, 8))); + assert!((list::has(list, 5))); + assert!((!list::has(list, 7))); + assert!((list::has(list, 8))); assert!((!list::has(empty, 5))); } #[test] fn test_len() { - let l = from_vec([0, 1, 2]); + let list = from_vec([0, 1, 2]); let empty = @list::Nil::; - assert_eq!(list::len(l), 3u); + assert_eq!(list::len(list), 3u); assert_eq!(list::len(empty), 0u); } From 8846970bba8d529f4361eec65d39254060483e03 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 21:38:40 -0400 Subject: [PATCH 03/18] Implement Eq for Cell --- src/libstd/cell.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/libstd/cell.rs b/src/libstd/cell.rs index 0a3c87f405893..bc28f2f445ee0 100644 --- a/src/libstd/cell.rs +++ b/src/libstd/cell.rs @@ -55,6 +55,12 @@ impl Clone for Cell { } } +impl Eq for Cell { + fn eq(&self, other: &Cell) -> bool { + self.get() == other.get() + } +} + /// A mutable memory location with dynamically checked borrow rules pub struct RefCell { priv value: T, @@ -273,11 +279,14 @@ mod test { #[test] fn smoketest_cell() { let x = Cell::new(10); + assert_eq!(x, Cell::new(10)); assert_eq!(x.get(), 10); x.set(20); + assert_eq!(x, Cell::new(20)); assert_eq!(x.get(), 20); let y = Cell::new((30, 40)); + assert_eq!(y, Cell::new((30, 40))); assert_eq!(y.get(), (30, 40)); } From 2b362768ff3cab2c966f1f18cf119b21fc96ea30 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 23:49:43 -0400 Subject: [PATCH 04/18] Modified list::from_vec() to return List --- src/libcollections/list.rs | 47 ++++++++++--------- .../log-knows-the-names-of-variants-in-std.rs | 6 +-- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 8174d12cce70b..8be06bc9afea0 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -17,11 +17,6 @@ pub enum List { Nil, } -/// Create a list from a vector -pub fn from_vec(v: &[T]) -> @List { - v.rev_iter().fold(@Nil::, |t, h| @Cons((*h).clone(), t)) -} - /** * Left fold * @@ -133,6 +128,16 @@ pub fn append(list: @List, other: @List) -> @List { } } +impl List { + /// Create a list from a vector + pub fn from_vec(v: &[T]) -> List { + match v.len() { + 0 => Nil, + _ => v.rev_iter().fold(Nil, |tail, value: &T| Cons(value.clone(), @tail)) + } + } +} + /* /// Push one element into the front of a list, returning a new list /// THIS VERSION DOESN'T ACTUALLY WORK @@ -171,16 +176,16 @@ pub fn each(list: @List, f: |&T| -> bool) -> bool { #[cfg(test)] mod tests { - use list::{List, Nil, from_vec, head, is_empty, tail}; + use list::{List, Nil, head, is_empty, tail}; use list; use std::option; #[test] fn test_is_empty() { - let empty : @list::List = from_vec([]); - let full1 = from_vec([1]); - let full2 = from_vec(['r', 'u']); + let empty : @list::List = @List::from_vec([]); + let full1 = @List::from_vec([1]); + let full2 = @List::from_vec(['r', 'u']); assert!(is_empty(empty)); assert!(!is_empty(full1)); @@ -189,7 +194,7 @@ mod tests { #[test] fn test_from_vec() { - let list = from_vec([0, 1, 2]); + let list = @List::from_vec([0, 1, 2]); assert_eq!(head(list), 0); @@ -202,14 +207,14 @@ mod tests { #[test] fn test_from_vec_empty() { - let empty : @list::List = from_vec([]); - assert_eq!(empty, @list::Nil::); + let empty : list::List = List::from_vec([]); + assert_eq!(empty, Nil::); } #[test] fn test_foldl() { fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); } - let list = from_vec([0, 1, 2, 3, 4]); + let list = @List::from_vec([0, 1, 2, 3, 4]); let empty = @list::Nil::; assert_eq!(list::foldl(0u, list, add), 10u); assert_eq!(list::foldl(0u, empty, add), 0u); @@ -220,21 +225,21 @@ mod tests { fn sub(a: &int, b: &int) -> int { *a - *b } - let list = from_vec([1, 2, 3, 4]); + let list = @List::from_vec([1, 2, 3, 4]); assert_eq!(list::foldl(0, list, sub), -10); } #[test] fn test_find_success() { fn match_(i: &int) -> bool { return *i == 2; } - let list = from_vec([0, 1, 2]); + let list = @List::from_vec([0, 1, 2]); assert_eq!(list::find(list, match_), option::Some(2)); } #[test] fn test_find_fail() { fn match_(_i: &int) -> bool { return false; } - let list = from_vec([0, 1, 2]); + let list = @List::from_vec([0, 1, 2]); let empty = @list::Nil::; assert_eq!(list::find(list, match_), option::None::); assert_eq!(list::find(empty, match_), option::None::); @@ -243,7 +248,7 @@ mod tests { #[test] fn test_any() { fn match_(i: &int) -> bool { return *i == 2; } - let list = from_vec([0, 1, 2]); + let list = @List::from_vec([0, 1, 2]); let empty = @list::Nil::; assert_eq!(list::any(list, match_), true); assert_eq!(list::any(empty, match_), false); @@ -251,7 +256,7 @@ mod tests { #[test] fn test_has() { - let list = from_vec([5, 8, 6]); + let list = @List::from_vec([5, 8, 6]); let empty = @list::Nil::; assert!((list::has(list, 5))); assert!((!list::has(list, 7))); @@ -261,7 +266,7 @@ mod tests { #[test] fn test_len() { - let list = from_vec([0, 1, 2]); + let list = @List::from_vec([0, 1, 2]); let empty = @list::Nil::; assert_eq!(list::len(list), 3u); assert_eq!(list::len(empty), 0u); @@ -269,7 +274,7 @@ mod tests { #[test] fn test_append() { - assert!(from_vec([1,2,3,4]) - == list::append(list::from_vec([1,2]), list::from_vec([3,4]))); + assert!(@List::from_vec([1,2,3,4]) + == list::append(@List::from_vec([1,2]), @List::from_vec([3,4]))); } } diff --git a/src/test/run-pass/log-knows-the-names-of-variants-in-std.rs b/src/test/run-pass/log-knows-the-names-of-variants-in-std.rs index 05c5a7a67f563..0129740252ca1 100644 --- a/src/test/run-pass/log-knows-the-names-of-variants-in-std.rs +++ b/src/test/run-pass/log-knows-the-names-of-variants-in-std.rs @@ -11,7 +11,7 @@ // except according to those terms. extern crate collections; -use collections::list; +use collections::list::List; #[deriving(Clone)] enum foo { @@ -24,8 +24,8 @@ fn check_log(exp: ~str, v: T) { } pub fn main() { - let x = list::from_vec([a(22u), b(~"hi")]); - let exp = ~"@Cons(a(22u), @Cons(b(~\"hi\"), @Nil))"; + let x = List::from_vec([a(22u), b(~"hi")]); + let exp = ~"Cons(a(22u), @Cons(b(~\"hi\"), @Nil))"; let act = format!("{:?}", x); assert!(act == exp); check_log(exp, x); From 52524bfe880f3722d8d70e4433429c1b4a3f31d3 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 21:59:14 -0400 Subject: [PATCH 05/18] Implemented Items<'a, T> for List --- src/libcollections/list.rs | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 8be06bc9afea0..d4685f37a39d4 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -17,6 +17,42 @@ pub enum List { Nil, } +pub struct Items<'a, T> { + priv head: &'a List, + priv next: Option<&'a @List> +} + +impl<'a, T> Iterator<&'a T> for Items<'a, T> { + fn next(&mut self) -> Option<&'a T> { + match self.next { + None => match *self.head { + Nil => None, + Cons(ref value, ref tail) => { + self.next = Some(tail); + Some(value) + } + }, + Some(next) => match **next { + Nil => None, + Cons(ref value, ref tail) => { + self.next = Some(tail); + Some(value) + } + } + } + } +} + +impl List { + /// Returns a forward iterator + pub fn iter<'a>(&'a self) -> Items<'a, T> { + Items { + head: self, + next: None + } + } +} + /** * Left fold * @@ -181,6 +217,16 @@ mod tests { use std::option; + #[test] + fn test_iter() { + let list = List::from_vec([0, 1, 2]); + let mut iter = list.iter(); + assert_eq!(&0, iter.next().unwrap()); + assert_eq!(&1, iter.next().unwrap()); + assert_eq!(&2, iter.next().unwrap()); + assert_eq!(None, iter.next()); + } + #[test] fn test_is_empty() { let empty : @list::List = @List::from_vec([]); From 0c8731e65ce535254114c6404d46b2074d96ab07 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:18:19 -0400 Subject: [PATCH 06/18] Replaced list::each with iter() in Arenas's Drop impl --- src/libarena/lib.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/libarena/lib.rs b/src/libarena/lib.rs index 717e5ec7b18b8..1fc88eda37b5f 100644 --- a/src/libarena/lib.rs +++ b/src/libarena/lib.rs @@ -25,7 +25,6 @@ extern crate collections; use collections::list::{List, Cons, Nil}; -use collections::list; use std::cast::{transmute, transmute_mut, transmute_mut_region}; use std::cast; @@ -44,7 +43,7 @@ use std::vec; // The way arena uses arrays is really deeply awful. The arrays are // allocated, and have capacities reserved, but the fill for the array // will always stay at 0. -#[deriving(Clone)] +#[deriving(Clone, Eq)] struct Chunk { data: Rc>, fill: Cell, @@ -119,13 +118,11 @@ impl Drop for Arena { fn drop(&mut self) { unsafe { destroy_chunk(&self.head); - - list::each(self.chunks.get(), |chunk| { + for chunk in self.chunks.get().iter() { if !chunk.is_pod.get() { destroy_chunk(chunk); } - true - }); + } } } } From 43bc6fcc62e3ad92c76ea8f160a6189ac86618f2 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:22:11 -0400 Subject: [PATCH 07/18] Replaced list::each with iter() in get_region --- src/librustc/middle/typeck/mod.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/librustc/middle/typeck/mod.rs b/src/librustc/middle/typeck/mod.rs index c04e4f24b0db0..dda45e839e12a 100644 --- a/src/librustc/middle/typeck/mod.rs +++ b/src/librustc/middle/typeck/mod.rs @@ -73,7 +73,6 @@ use std::cell::RefCell; use collections::HashMap; use std::rc::Rc; use collections::List; -use collections::list; use syntax::codemap::Span; use syntax::print::pprust::*; use syntax::{ast, ast_map, abi}; @@ -311,23 +310,18 @@ pub fn require_same_types(tcx: ty::ctxt, // corresponding ty::Region pub type isr_alist = @List<(ty::BoundRegion, ty::Region)>; -trait get_and_find_region { - fn get(&self, br: ty::BoundRegion) -> ty::Region; - fn find(&self, br: ty::BoundRegion) -> Option; +trait get_region<'a, T:'static> { + fn get(&'a self, br: ty::BoundRegion) -> ty::Region; } -impl get_and_find_region for isr_alist { - fn get(&self, br: ty::BoundRegion) -> ty::Region { - self.find(br).unwrap() - } - - fn find(&self, br: ty::BoundRegion) -> Option { - let mut ret = None; - list::each(*self, |isr| { +impl<'a, T:'static> get_region <'a, T> for isr_alist { + fn get(&'a self, br: ty::BoundRegion) -> ty::Region { + let mut region = None; + for isr in self.iter() { let (isr_br, isr_r) = *isr; - if isr_br == br { ret = Some(isr_r); false } else { true } - }); - ret + if isr_br == br { region = Some(isr_r); break; } + }; + region.unwrap() } } From a09a4b882d415ea764f58816b963de0203c4e9f0 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:34:59 -0400 Subject: [PATCH 08/18] Removed list::foldl() in favor of iter().fold() --- src/libcollections/list.rs | 43 +++++++++----------------------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index d4685f37a39d4..ce6084923f914 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -53,25 +53,6 @@ impl List { } } -/** - * Left fold - * - * Applies `f` to `u` and the first element in the list, then applies `f` to - * the result of the previous call and the second element, and so on, - * returning the accumulated result. - * - * # Arguments - * - * * list - The list to fold - * * z - The initial value - * * f - The function to apply - */ -pub fn foldl(z: T, list: @List, f: |&T, &U| -> T) -> T { - let mut accum: T = z; - iter(list, |element| accum = f(&accum, element)); - accum -} - /** * Search for an element that matches a given predicate * @@ -258,21 +239,17 @@ mod tests { } #[test] - fn test_foldl() { - fn add(a: &uint, b: &int) -> uint { return *a + (*b as uint); } - let list = @List::from_vec([0, 1, 2, 3, 4]); - let empty = @list::Nil::; - assert_eq!(list::foldl(0u, list, add), 10u); - assert_eq!(list::foldl(0u, empty, add), 0u); - } + fn test_fold() { + fn add_(a: uint, b: &uint) -> uint { a + *b } + fn subtract_(a: uint, b: &uint) -> uint { a - *b } - #[test] - fn test_foldl2() { - fn sub(a: &int, b: &int) -> int { - *a - *b - } - let list = @List::from_vec([1, 2, 3, 4]); - assert_eq!(list::foldl(0, list, sub), -10); + let empty = Nil::; + assert_eq!(empty.iter().fold(0u, add_), 0u); + assert_eq!(empty.iter().fold(10u, subtract_), 10u); + + let list = List::from_vec([0u, 1u, 2u, 3u, 4u]); + assert_eq!(list.iter().fold(0u, add_), 10u); + assert_eq!(list.iter().fold(10u, subtract_), 0u); } #[test] From d68190706448b5a1ca09be690f360c08a7a4f831 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:40:57 -0400 Subject: [PATCH 09/18] Removed list::find() in favor of iter().find() --- src/libcollections/list.rs | 41 ++++++++++---------------------------- 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index ce6084923f914..459b0a78a1eec 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -53,26 +53,6 @@ impl List { } } -/** - * Search for an element that matches a given predicate - * - * Apply function `f` to each element of `list`, starting from the first. - * When function `f` returns true then an option containing the element - * is returned. If `f` matches no elements then none is returned. - */ -pub fn find(list: @List, f: |&T| -> bool) -> Option { - let mut list = list; - loop { - list = match *list { - Cons(ref head, tail) => { - if f(head) { return Some((*head).clone()); } - tail - } - Nil => return None - } - }; -} - /** * Returns true if a list contains an element that matches a given predicate * @@ -196,8 +176,6 @@ mod tests { use list::{List, Nil, head, is_empty, tail}; use list; - use std::option; - #[test] fn test_iter() { let list = List::from_vec([0, 1, 2]); @@ -254,18 +232,21 @@ mod tests { #[test] fn test_find_success() { - fn match_(i: &int) -> bool { return *i == 2; } - let list = @List::from_vec([0, 1, 2]); - assert_eq!(list::find(list, match_), option::Some(2)); + fn match_(i: & &int) -> bool { **i == 2 } + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.iter().find(match_).unwrap(), &2); } #[test] fn test_find_fail() { - fn match_(_i: &int) -> bool { return false; } - let list = @List::from_vec([0, 1, 2]); - let empty = @list::Nil::; - assert_eq!(list::find(list, match_), option::None::); - assert_eq!(list::find(empty, match_), option::None::); + fn match_(_i: & &int) -> bool { false } + + let empty = Nil::; + assert_eq!(empty.iter().find(match_), None); + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.iter().find(match_), None); } #[test] From 197116d7ce7862b21eac6af498a2726d7a1bc3d6 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:45:27 -0400 Subject: [PATCH 10/18] Removed list::any() in favor of iter().any() --- src/libcollections/list.rs | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 459b0a78a1eec..ddac516d8bbdf 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -53,26 +53,6 @@ impl List { } } -/** - * Returns true if a list contains an element that matches a given predicate - * - * Apply function `f` to each element of `list`, starting from the first. - * When function `f` returns true then it also returns true. If `f` matches no - * elements then false is returned. - */ -pub fn any(list: @List, f: |&T| -> bool) -> bool { - let mut list = list; - loop { - list = match *list { - Cons(ref head, tail) => { - if f(head) { return true; } - tail - } - Nil => return false - } - }; -} - /// Returns true if a list contains an element with the given value pub fn has(list: @List, element: T) -> bool { let mut found = false; @@ -251,11 +231,13 @@ mod tests { #[test] fn test_any() { - fn match_(i: &int) -> bool { return *i == 2; } - let list = @List::from_vec([0, 1, 2]); - let empty = @list::Nil::; - assert_eq!(list::any(list, match_), true); - assert_eq!(list::any(empty, match_), false); + fn match_(i: &int) -> bool { *i == 2 } + + let empty = Nil::; + assert_eq!(empty.iter().any(match_), false); + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.iter().any(match_), true); } #[test] From e589fcffcc8da46b5949d15284a466d9ed27a003 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 22:54:34 -0400 Subject: [PATCH 11/18] Implemented list::len() based on Container trait --- src/libcollections/list.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index ddac516d8bbdf..9223669c78763 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -10,6 +10,8 @@ //! A standard, garbage-collected linked list. +use std::container::Container; + #[deriving(Clone, Eq)] #[allow(missing_doc)] pub enum List { @@ -53,6 +55,11 @@ impl List { } } +impl Container for List { + /// Returns the length of a list + fn len(&self) -> uint { self.iter().len() } +} + /// Returns true if a list contains an element with the given value pub fn has(list: @List, element: T) -> bool { let mut found = false; @@ -70,13 +77,6 @@ pub fn is_empty(list: @List) -> bool { } } -/// Returns the length of a list -pub fn len(list: @List) -> uint { - let mut count = 0u; - iter(list, |_e| count += 1u); - count -} - /// Returns all but the first element of a list pub fn tail(list: @List) -> @List { match *list { @@ -252,10 +252,11 @@ mod tests { #[test] fn test_len() { - let list = @List::from_vec([0, 1, 2]); - let empty = @list::Nil::; - assert_eq!(list::len(list), 3u); - assert_eq!(list::len(empty), 0u); + let empty = Nil::; + assert_eq!(empty.len(), 0u); + + let list = List::from_vec([0, 1, 2]); + assert_eq!(list.len(), 3u); } #[test] From a14d72d49e4d87b970f899a761dbdfce63239b79 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 25 Feb 2014 01:06:54 -0400 Subject: [PATCH 12/18] Implemented list::is_empty() based on Container trait --- src/libcollections/list.rs | 25 +++++++++-------------- src/test/run-pass/non-boolean-pure-fns.rs | 4 ++-- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 9223669c78763..1239ce702204c 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -58,6 +58,9 @@ impl List { impl Container for List { /// Returns the length of a list fn len(&self) -> uint { self.iter().len() } + + /// Returns true if the list is empty + fn is_empty(&self) -> bool { match *self { Nil => true, _ => false } } } /// Returns true if a list contains an element with the given value @@ -69,14 +72,6 @@ pub fn has(list: @List, element: T) -> bool { return found; } -/// Returns true if the list is empty -pub fn is_empty(list: @List) -> bool { - match *list { - Nil => true, - _ => false - } -} - /// Returns all but the first element of a list pub fn tail(list: @List) -> @List { match *list { @@ -153,7 +148,7 @@ pub fn each(list: @List, f: |&T| -> bool) -> bool { #[cfg(test)] mod tests { - use list::{List, Nil, head, is_empty, tail}; + use list::{List, Nil, head, tail}; use list; #[test] @@ -168,13 +163,13 @@ mod tests { #[test] fn test_is_empty() { - let empty : @list::List = @List::from_vec([]); - let full1 = @List::from_vec([1]); - let full2 = @List::from_vec(['r', 'u']); + let empty : list::List = List::from_vec([]); + let full1 = List::from_vec([1]); + let full2 = List::from_vec(['r', 'u']); - assert!(is_empty(empty)); - assert!(!is_empty(full1)); - assert!(!is_empty(full2)); + assert!(empty.is_empty()); + assert!(!full1.is_empty()); + assert!(!full2.is_empty()); } #[test] diff --git a/src/test/run-pass/non-boolean-pure-fns.rs b/src/test/run-pass/non-boolean-pure-fns.rs index cb08c81d9e0ed..9cbf80c310548 100644 --- a/src/test/run-pass/non-boolean-pure-fns.rs +++ b/src/test/run-pass/non-boolean-pure-fns.rs @@ -14,7 +14,7 @@ extern crate collections; -use collections::list::{List, Cons, Nil, head, is_empty}; +use collections::list::{List, Cons, Nil, head}; fn pure_length_go(ls: @List, acc: uint) -> uint { match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } } @@ -25,7 +25,7 @@ fn pure_length(ls: @List) -> uint { pure_length_go(ls, 0u) } fn nonempty_list(ls: @List) -> bool { pure_length(ls) > 0u } fn safe_head(ls: @List) -> T { - assert!(!is_empty(ls)); + assert!(!ls.is_empty()); return head(ls); } From 223f309fc3b0550803ec222bbb77f4ef129e915a Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 25 Feb 2014 00:54:06 -0400 Subject: [PATCH 13/18] Removed deprecated list::{iter,each}() functions --- src/libcollections/list.rs | 28 ---------------------------- 1 file changed, 28 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 1239ce702204c..035e17be90b06 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -118,34 +118,6 @@ fn push(ll: &mut @list, vv: T) { } */ -/// Iterate over a list -pub fn iter(list: @List, f: |&T|) { - let mut cur = list; - loop { - cur = match *cur { - Cons(ref head, tail) => { - f(head); - tail - } - Nil => break - } - } -} - -/// Iterate over a list -pub fn each(list: @List, f: |&T| -> bool) -> bool { - let mut cur = list; - loop { - cur = match *cur { - Cons(ref head, tail) => { - if !f(head) { return false; } - tail - } - Nil => { return true; } - } - } -} - #[cfg(test)] mod tests { use list::{List, Nil, head, tail}; From 45fd63a8b7b1fa0242d864f9592c05d06669b395 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 23:25:04 -0400 Subject: [PATCH 14/18] Replaced list::has() with list::contains() --- src/libcollections/list.rs | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 035e17be90b06..05e60d07df85e 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -63,13 +63,11 @@ impl Container for List { fn is_empty(&self) -> bool { match *self { Nil => true, _ => false } } } -/// Returns true if a list contains an element with the given value -pub fn has(list: @List, element: T) -> bool { - let mut found = false; - each(list, |e| { - if *e == element { found = true; false } else { true } - }); - return found; +impl List { + /// Returns true if a list contains an element with the given value + pub fn contains(&self, element: T) -> bool { + self.iter().any(|list_element| *list_element == element) + } } /// Returns all but the first element of a list @@ -208,13 +206,14 @@ mod tests { } #[test] - fn test_has() { - let list = @List::from_vec([5, 8, 6]); - let empty = @list::Nil::; - assert!((list::has(list, 5))); - assert!((!list::has(list, 7))); - assert!((list::has(list, 8))); - assert!((!list::has(empty, 5))); + fn test_contains() { + let empty = Nil::; + assert!((!empty.contains(5))); + + let list = List::from_vec([5, 8, 6]); + assert!((list.contains(5))); + assert!((!list.contains(7))); + assert!((list.contains(8))); } #[test] From fed034c402eb22b60fb9d7581e720bb0010dae65 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 23:34:26 -0400 Subject: [PATCH 15/18] Refactored list::head() to be based on List --- src/libcollections/list.rs | 30 +++++++++++------------ src/test/run-pass/non-boolean-pure-fns.rs | 10 ++++---- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 05e60d07df85e..a922c247b2f65 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -53,6 +53,14 @@ impl List { next: None } } + + /// Returns the first element of a list + pub fn head<'a>(&'a self) -> Option<&'a T> { + match *self { + Nil => None, + Cons(ref head, _) => Some(head) + } + } } impl Container for List { @@ -78,15 +86,6 @@ pub fn tail(list: @List) -> @List { } } -/// Returns the first element of a list -pub fn head(list: @List) -> T { - match *list { - Cons(ref head, _) => (*head).clone(), - // makes me sad - _ => fail!("head invoked on empty list") - } -} - /// Appends one list to another pub fn append(list: @List, other: @List) -> @List { match *list { @@ -118,7 +117,7 @@ fn push(ll: &mut @list, vv: T) { #[cfg(test)] mod tests { - use list::{List, Nil, head, tail}; + use list::{List, Nil, tail}; use list; #[test] @@ -145,14 +144,13 @@ mod tests { #[test] fn test_from_vec() { let list = @List::from_vec([0, 1, 2]); + assert_eq!(list.head().unwrap(), &0); - assert_eq!(head(list), 0); - - let tail_l = tail(list); - assert_eq!(head(tail_l), 1); + let mut tail = tail(list); + assert_eq!(tail.head().unwrap(), &1); - let tail_tail_l = tail(tail_l); - assert_eq!(head(tail_tail_l), 2); + tail = tail(tail); + assert_eq!(tail.head().unwrap(), &2); } #[test] diff --git a/src/test/run-pass/non-boolean-pure-fns.rs b/src/test/run-pass/non-boolean-pure-fns.rs index 9cbf80c310548..66bb2e702bea3 100644 --- a/src/test/run-pass/non-boolean-pure-fns.rs +++ b/src/test/run-pass/non-boolean-pure-fns.rs @@ -14,19 +14,19 @@ extern crate collections; -use collections::list::{List, Cons, Nil, head}; +use collections::list::{List, Cons, Nil}; -fn pure_length_go(ls: @List, acc: uint) -> uint { +fn pure_length_go(ls: @List, acc: uint) -> uint { match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } } } -fn pure_length(ls: @List) -> uint { pure_length_go(ls, 0u) } +fn pure_length(ls: @List) -> uint { pure_length_go(ls, 0u) } -fn nonempty_list(ls: @List) -> bool { pure_length(ls) > 0u } +fn nonempty_list(ls: @List) -> bool { pure_length(ls) > 0u } fn safe_head(ls: @List) -> T { assert!(!ls.is_empty()); - return head(ls); + return ls.head().unwrap().clone(); } pub fn main() { From 65f19932309f068e09c7472dc06b3e8856afb6fc Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Tue, 25 Feb 2014 01:23:53 -0400 Subject: [PATCH 16/18] Refactored list::tail() to be based on List --- src/libcollections/list.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index a922c247b2f65..4cadc27eea3b1 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -61,6 +61,14 @@ impl List { Cons(ref head, _) => Some(head) } } + + /// Returns all but the first element of a list + pub fn tail(&self) -> Option<@List> { + match *self { + Nil => None, + Cons(_, tail) => Some(tail) + } + } } impl Container for List { @@ -78,14 +86,6 @@ impl List { } } -/// Returns all but the first element of a list -pub fn tail(list: @List) -> @List { - match *list { - Cons(_, tail) => return tail, - Nil => fail!("list empty") - } -} - /// Appends one list to another pub fn append(list: @List, other: @List) -> @List { match *list { @@ -117,7 +117,7 @@ fn push(ll: &mut @list, vv: T) { #[cfg(test)] mod tests { - use list::{List, Nil, tail}; + use list::{List, Nil}; use list; #[test] @@ -143,13 +143,13 @@ mod tests { #[test] fn test_from_vec() { - let list = @List::from_vec([0, 1, 2]); + let list = List::from_vec([0, 1, 2]); assert_eq!(list.head().unwrap(), &0); - let mut tail = tail(list); + let mut tail = list.tail().unwrap(); assert_eq!(tail.head().unwrap(), &1); - tail = tail(tail); + tail = tail.tail().unwrap(); assert_eq!(tail.head().unwrap(), &2); } From 1c27c90119b7e55ce160f39543874b166db7eae6 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 23:48:47 -0400 Subject: [PATCH 17/18] Refactored list::append() to be based on List --- src/libcollections/list.rs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 4cadc27eea3b1..030d7f34e9f66 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -86,17 +86,6 @@ impl List { } } -/// Appends one list to another -pub fn append(list: @List, other: @List) -> @List { - match *list { - Nil => return other, - Cons(ref head, tail) => { - let rest = append(tail, other); - return @Cons((*head).clone(), rest); - } - } -} - impl List { /// Create a list from a vector pub fn from_vec(v: &[T]) -> List { @@ -105,6 +94,17 @@ impl List { _ => v.rev_iter().fold(Nil, |tail, value: &T| Cons(value.clone(), @tail)) } } + + /// Appends one list to another, returning a new list + pub fn append(&self, other: List) -> List { + match other { + Nil => return self.clone(), + _ => match *self { + Nil => return other, + Cons(ref value, tail) => Cons(value.clone(), @tail.append(other)) + } + } + } } /* @@ -225,7 +225,7 @@ mod tests { #[test] fn test_append() { - assert!(@List::from_vec([1,2,3,4]) - == list::append(@List::from_vec([1,2]), @List::from_vec([3,4]))); + assert_eq!(List::from_vec([1, 2, 3, 4]), + List::from_vec([1, 2]).append(List::from_vec([3, 4]))); } } From 4da6d041c268d58654d54b317288c940e5c623fd Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Mon, 24 Feb 2014 23:45:16 -0400 Subject: [PATCH 18/18] Replaced list::push() with unshift() based on List --- src/libcollections/list.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/libcollections/list.rs b/src/libcollections/list.rs index 030d7f34e9f66..2359b7ec7694f 100644 --- a/src/libcollections/list.rs +++ b/src/libcollections/list.rs @@ -105,15 +105,12 @@ impl List { } } } -} -/* -/// Push one element into the front of a list, returning a new list -/// THIS VERSION DOESN'T ACTUALLY WORK -fn push(ll: &mut @list, vv: T) { - ll = &mut @cons(vv, *ll) + /// Push one element into the front of a list, returning a new list + pub fn unshift(&self, element: T) -> List { + Cons(element, @(self.clone())) + } } -*/ #[cfg(test)] mod tests { @@ -228,4 +225,13 @@ mod tests { assert_eq!(List::from_vec([1, 2, 3, 4]), List::from_vec([1, 2]).append(List::from_vec([3, 4]))); } + + #[test] + fn test_unshift() { + let list = List::from_vec([1]); + let new_list = list.unshift(0); + assert_eq!(list.len(), 1u); + assert_eq!(new_list.len(), 2u); + assert_eq!(new_list, List::from_vec([0, 1])); + } }