Skip to content

Commit 54e7f4c

Browse files
committed
---
yaml --- r: 149623 b: refs/heads/try2 c: fed034c h: refs/heads/master i: 149621: e0fd4d6 149619: a6480f5 149615: 3c3326b v: v3
1 parent 9a62baf commit 54e7f4c

File tree

3 files changed

+20
-22
lines changed

3 files changed

+20
-22
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: 45fd63a8b7b1fa0242d864f9592c05d06669b395
8+
refs/heads/try2: fed034c402eb22b60fb9d7581e720bb0010dae65
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libcollections/list.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ impl<T> List<T> {
5353
next: None
5454
}
5555
}
56+
57+
/// Returns the first element of a list
58+
pub fn head<'a>(&'a self) -> Option<&'a T> {
59+
match *self {
60+
Nil => None,
61+
Cons(ref head, _) => Some(head)
62+
}
63+
}
5664
}
5765

5866
impl<T> Container for List<T> {
@@ -78,15 +86,6 @@ pub fn tail<T>(list: @List<T>) -> @List<T> {
7886
}
7987
}
8088

81-
/// Returns the first element of a list
82-
pub fn head<T:Clone>(list: @List<T>) -> T {
83-
match *list {
84-
Cons(ref head, _) => (*head).clone(),
85-
// makes me sad
86-
_ => fail!("head invoked on empty list")
87-
}
88-
}
89-
9089
/// Appends one list to another
9190
pub fn append<T:Clone + 'static>(list: @List<T>, other: @List<T>) -> @List<T> {
9291
match *list {
@@ -118,7 +117,7 @@ fn push<T:Clone>(ll: &mut @list<T>, vv: T) {
118117

119118
#[cfg(test)]
120119
mod tests {
121-
use list::{List, Nil, head, tail};
120+
use list::{List, Nil, tail};
122121
use list;
123122

124123
#[test]
@@ -145,14 +144,13 @@ mod tests {
145144
#[test]
146145
fn test_from_vec() {
147146
let list = @List::from_vec([0, 1, 2]);
147+
assert_eq!(list.head().unwrap(), &0);
148148

149-
assert_eq!(head(list), 0);
150-
151-
let tail_l = tail(list);
152-
assert_eq!(head(tail_l), 1);
149+
let mut tail = tail(list);
150+
assert_eq!(tail.head().unwrap(), &1);
153151

154-
let tail_tail_l = tail(tail_l);
155-
assert_eq!(head(tail_tail_l), 2);
152+
tail = tail(tail);
153+
assert_eq!(tail.head().unwrap(), &2);
156154
}
157155

158156
#[test]

branches/try2/src/test/run-pass/non-boolean-pure-fns.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@
1414

1515
extern crate collections;
1616

17-
use collections::list::{List, Cons, Nil, head};
17+
use collections::list::{List, Cons, Nil};
1818

19-
fn pure_length_go<T:Clone>(ls: @List<T>, acc: uint) -> uint {
19+
fn pure_length_go<T>(ls: @List<T>, acc: uint) -> uint {
2020
match *ls { Nil => { acc } Cons(_, tl) => { pure_length_go(tl, acc + 1u) } }
2121
}
2222

23-
fn pure_length<T:Clone>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
23+
fn pure_length<T>(ls: @List<T>) -> uint { pure_length_go(ls, 0u) }
2424

25-
fn nonempty_list<T:Clone>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
25+
fn nonempty_list<T>(ls: @List<T>) -> bool { pure_length(ls) > 0u }
2626

2727
fn safe_head<T:Clone>(ls: @List<T>) -> T {
2828
assert!(!ls.is_empty());
29-
return head(ls);
29+
return ls.head().unwrap().clone();
3030
}
3131

3232
pub fn main() {

0 commit comments

Comments
 (0)