auto merge of #12348 : brunoabinader/rust/libcollections-list-refactory, r=alexcrichton
This PR includes: - Create an iterator for ```List<T>``` called ```Items<T>```; - Move all list operations inside ```List<T>``` impl; - Removed functions that are already provided by ```Iterator``` trait; - Refactor on ```len()``` and ```is_empty``` using ```Container``` trait; - Bunch of minor fixes; A replacement for using @ is intended, but still in discussion. Closes #12344.
This commit is contained in:
commit
f203fc7daf
7 changed files with 191 additions and 231 deletions
|
|
@ -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<T>(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);
|
||||
|
|
|
|||
|
|
@ -14,19 +14,19 @@
|
|||
|
||||
extern crate collections;
|
||||
|
||||
use collections::list::{List, Cons, Nil, head, is_empty};
|
||||
use collections::list::{List, Cons, Nil};
|
||||
|
||||
fn pure_length_go<T:Clone>(ls: @List<T>, acc: uint) -> uint {
|
||||
fn pure_length_go<T>(ls: @List<T>, acc: uint) -> uint {
|
||||
match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } }
|
||||
}
|
||||
|
||||
fn pure_length<T:Clone>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
|
||||
fn pure_length<T>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
|
||||
|
||||
fn nonempty_list<T:Clone>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
|
||||
fn nonempty_list<T>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
|
||||
|
||||
fn safe_head<T:Clone>(ls: @List<T>) -> T {
|
||||
assert!(!is_empty(ls));
|
||||
return head(ls);
|
||||
assert!(!ls.is_empty());
|
||||
return ls.head().unwrap().clone();
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue