Skip to content

Commit 001397d

Browse files
committed
stdlib: Add ivec::any() and ivec::all(); put out burning tinderbox
1 parent f71c8dd commit 001397d

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

src/lib/ivec.rs

+10
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@ fn map[T,U](fn(&T)->U f, &mutable T[mutable?] v) -> U[] {
171171
ret result;
172172
}
173173

174+
fn any[T](fn(&T)->bool f, &T[] v) -> bool {
175+
for (T elem in v) { if (f(elem)) { ret true; } }
176+
ret false;
177+
}
178+
179+
fn all[T](fn(&T)->bool f, &T[] v) -> bool {
180+
for (T elem in v) { if (!f(elem)) { ret false; } }
181+
ret true;
182+
}
183+
174184

175185
mod unsafe {
176186
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {

src/test/run-pass/lib-ivec.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import std::option::some;
77

88
fn square(uint n) -> uint { ret n * n; }
99

10+
fn square_alias(&uint n) -> uint { ret n * n; }
11+
12+
pred is_three(&uint n) -> bool { ret n == 3u; }
13+
1014
fn test_reserve_and_on_heap() {
1115
let int[] v = ~[ 1, 2 ];
1216
assert (!ivec::on_heap(v));
@@ -167,15 +171,15 @@ fn test_grow_set() {
167171
fn test_map() {
168172
// Test on-stack map.
169173
auto v = ~[ 1u, 2u, 3u ];
170-
auto w = ivec::map(square, v);
174+
auto w = ivec::map(square_alias, v);
171175
assert (ivec::len(w) == 3u);
172176
assert (w.(0) == 1u);
173177
assert (w.(1) == 4u);
174178
assert (w.(2) == 9u);
175179

176180
// Test on-heap map.
177181
v = ~[ 1u, 2u, 3u, 4u, 5u ];
178-
w = ivec::map(square, v);
182+
w = ivec::map(square_alias, v);
179183
assert (ivec::len(w) == 5u);
180184
assert (w.(0) == 1u);
181185
assert (w.(1) == 4u);
@@ -184,6 +188,18 @@ fn test_map() {
184188
assert (w.(4) == 25u);
185189
}
186190

191+
fn test_any_and_all() {
192+
assert (ivec::any(is_three, ~[ 1u, 2u, 3u ]));
193+
assert (!ivec::any(is_three, ~[ 0u, 1u, 2u ]));
194+
assert (ivec::any(is_three, ~[ 1u, 2u, 3u, 4u, 5u ]));
195+
assert (!ivec::any(is_three, ~[ 1u, 2u, 4u, 5u, 6u ]));
196+
197+
assert (ivec::all(is_three, ~[ 3u, 3u, 3u ]));
198+
assert (!ivec::all(is_three, ~[ 3u, 3u, 2u ]));
199+
assert (ivec::all(is_three, ~[ 3u, 3u, 3u, 3u, 3u ]));
200+
assert (!ivec::all(is_three, ~[ 3u, 3u, 0u, 1u, 2u ]));
201+
}
202+
187203
fn main() {
188204
test_reserve_and_on_heap();
189205
test_unsafe_ptrs();
@@ -204,5 +220,6 @@ fn main() {
204220

205221
// Functional utilities
206222
test_map();
223+
test_any_and_all();
207224
}
208225

0 commit comments

Comments
 (0)