File tree Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Expand file tree Collapse file tree 3 files changed +29
-1
lines changed Original file line number Diff line number Diff line change 1
1
---
2
- refs/heads/master: 139aaa1616fd341a71f0f11adb4e8850639e228c
2
+ refs/heads/master: 81acf69f9708a236ef78faa180575ae7b618fba9
Original file line number Diff line number Diff line change @@ -87,6 +87,16 @@ pred is_not_empty[T](&T[mutable?] v) -> bool {
87
87
88
88
// Accessors
89
89
90
+ /// Returns the first element of a vector
91
+ fn head[ T ] ( & T [ mutable?] v) : is_not_empty ( v) -> T {
92
+ ret v. ( 0 ) ;
93
+ }
94
+
95
+ /// Returns all but the first element of a vector
96
+ fn tail[ T ] ( & T [ mutable?] v) : is_not_empty ( v) -> T [ mutable?] {
97
+ ret slice ( v, 1 u, len ( v) ) ;
98
+ }
99
+
90
100
/// Returns the last element of `v`.
91
101
fn last[ T ] ( & T [ mutable?] v) -> option:: t[ T ] {
92
102
if ( len ( v) == 0 u) { ret none; }
Original file line number Diff line number Diff line change @@ -94,6 +94,22 @@ fn test_is_not_empty() {
94
94
assert !ivec:: is_not_empty[ int] ( ~[ ] ) ;
95
95
}
96
96
97
+ fn test_head ( ) {
98
+ auto a = ~[ 11 , 12 ] ;
99
+ check ivec:: is_not_empty ( a) ;
100
+ assert ivec:: head ( a) == 11 ;
101
+ }
102
+
103
+ fn test_tail ( ) {
104
+ auto a = ~[ 11 ] ;
105
+ check ivec:: is_not_empty ( a) ;
106
+ assert ivec:: tail ( a) == ~[ ] ;
107
+
108
+ a = ~[ 11 , 12 ] ;
109
+ check ivec:: is_not_empty ( a) ;
110
+ assert ivec:: tail ( a) == ~[ 12 ] ;
111
+ }
112
+
97
113
fn test_last ( ) {
98
114
auto n = ivec:: last ( ~[ ] ) ;
99
115
assert ( n == none) ;
@@ -257,6 +273,8 @@ fn main() {
257
273
// Accessors
258
274
test_init_fn ( ) ;
259
275
test_init_elt ( ) ;
276
+ test_head ( ) ;
277
+ test_tail ( ) ;
260
278
test_last ( ) ;
261
279
test_slice ( ) ;
262
280
You can’t perform that action at this time.
0 commit comments