Refactored list::head() to be based on List<T>

This commit is contained in:
Bruno de Oliveira Abinader 2014-02-24 23:34:26 -04:00
parent 45fd63a8b7
commit fed034c402
2 changed files with 19 additions and 21 deletions

View file

@ -14,19 +14,19 @@
extern crate collections;
use collections::list::{List, Cons, Nil, head};
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!(!ls.is_empty());
return head(ls);
return ls.head().unwrap().clone();
}
pub fn main() {