File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -193,10 +193,26 @@ fn tail<T>(v: [mutable? T]) : is_not_empty(v) -> [T] {
193
193
ret slice ( v, 1 u, len ( v) ) ;
194
194
}
195
195
196
+ // FIXME: This name is sort of confusing next to init_fn, etc
197
+ // but this is the name haskell uses for this function,
198
+ // along with head/tail/last.
199
+ /*
200
+ Function: init
201
+
202
+ Returns all but the last elemnt of a vector
203
+
204
+ Preconditions:
205
+ `v` is not empty
206
+ */
207
+ fn init < T > ( v : [ mutable? T ] ) -> [ T ] {
208
+ assert len( v) != 0 u;
209
+ slice ( v, 0 u, len ( v) - 1 u)
210
+ }
211
+
196
212
/*
197
213
Function: last
198
214
199
- Returns the last element of `v`
215
+ Returns the last element of a vector
200
216
201
217
Returns:
202
218
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import std::vec::*;
5
5
import std:: option;
6
6
import std:: option:: none;
7
7
import std:: option:: some;
8
+ import std:: task;
8
9
9
10
fn square ( n : uint ) -> uint { ret n * n; }
10
11
@@ -443,6 +444,31 @@ fn reversed_mut() {
443
444
assert ( v2[ 1 ] == 10 ) ;
444
445
}
445
446
447
+ #[ test]
448
+ fn init ( ) {
449
+ let v = vec:: init ( [ 1 , 2 , 3 ] ) ;
450
+ assert v == [ 1 , 2 ] ;
451
+ }
452
+
453
+ #[ cfg( target_os = "linux" ) ]
454
+ #[ cfg( target_os = "mac" ) ]
455
+ #[ test]
456
+ fn init_empty ( ) {
457
+
458
+ let r = task:: join (
459
+ task:: spawn_joinable ( ( ) , fn ( & & _i : ( ) ) {
460
+ task:: unsupervise ( ) ;
461
+ vec:: init :: < int > ( [ ] ) ;
462
+ } ) ) ;
463
+ assert r == task:: tr_failure
464
+ }
465
+
466
+ // FIXME: Windows can't undwind
467
+ #[ cfg( target_os = "win32" ) ]
468
+ #[ test]
469
+ #[ ignore]
470
+ fn init_empty ( ) { }
471
+
446
472
// Local Variables:
447
473
// mode: rust;
448
474
// fill-column: 78;
You can’t perform that action at this time.
0 commit comments