Skip to content

Commit

Permalink
Modified list::from_vec() to return List<T>
Browse files Browse the repository at this point in the history
  • Loading branch information
brunoabinader committed Feb 27, 2014
1 parent 8846970 commit 2b36276
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
47 changes: 26 additions & 21 deletions src/libcollections/list.rs
Expand Up @@ -17,11 +17,6 @@ pub enum List<T> {
Nil,
}

/// Create a list from a vector
pub fn from_vec<T:Clone + 'static>(v: &[T]) -> @List<T> {
v.rev_iter().fold(@Nil::<T>, |t, h| @Cons((*h).clone(), t))
}

/**
* Left fold
*
Expand Down Expand Up @@ -133,6 +128,16 @@ pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
}
}

impl<T:'static + Clone> List<T> {
/// Create a list from a vector
pub fn from_vec(v: &[T]) -> List<T> {
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
Expand Down Expand Up @@ -171,16 +176,16 @@ pub fn each<T>(list: @List<T>, 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<int> = from_vec([]);
let full1 = from_vec([1]);
let full2 = from_vec(['r', 'u']);
let empty : @list::List<int> = @List::from_vec([]);
let full1 = @List::from_vec([1]);
let full2 = @List::from_vec(['r', 'u']);

assert!(is_empty(empty));
assert!(!is_empty(full1));
Expand All @@ -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);

Expand All @@ -202,14 +207,14 @@ mod tests {

#[test]
fn test_from_vec_empty() {
let empty : @list::List<int> = from_vec([]);
assert_eq!(empty, @list::Nil::<int>);
let empty : list::List<int> = List::from_vec([]);
assert_eq!(empty, Nil::<int>);
}

#[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::<int>;
assert_eq!(list::foldl(0u, list, add), 10u);
assert_eq!(list::foldl(0u, empty, add), 0u);
Expand All @@ -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::<int>;
assert_eq!(list::find(list, match_), option::None::<int>);
assert_eq!(list::find(empty, match_), option::None::<int>);
Expand All @@ -243,15 +248,15 @@ 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::<int>;
assert_eq!(list::any(list, match_), true);
assert_eq!(list::any(empty, match_), false);
}

#[test]
fn test_has() {
let list = from_vec([5, 8, 6]);
let list = @List::from_vec([5, 8, 6]);
let empty = @list::Nil::<int>;
assert!((list::has(list, 5)));
assert!((!list::has(list, 7)));
Expand All @@ -261,15 +266,15 @@ 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::<int>;
assert_eq!(list::len(list), 3u);
assert_eq!(list::len(empty), 0u);
}

#[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])));
}
}
6 changes: 3 additions & 3 deletions src/test/run-pass/log-knows-the-names-of-variants-in-std.rs
Expand Up @@ -11,7 +11,7 @@
// except according to those terms.

extern crate collections;
use collections::list;
use collections::list::List;

#[deriving(Clone)]
enum foo {
Expand All @@ -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);
Expand Down

0 comments on commit 2b36276

Please sign in to comment.