Skip to content

Commit 66e9809

Browse files
committed
---
yaml --- r: 3574 b: refs/heads/master c: 001397d h: refs/heads/master v: v3
1 parent 5a99a73 commit 66e9809

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: f71c8dd91873d3983a21b85f5c86651b1a5cefed
2+
refs/heads/master: 001397da3ced1065b254a0e87ae071e17d0b16ba

trunk/src/lib/ivec.rs

Lines changed: 10 additions & 0 deletions
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) {

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

Lines changed: 19 additions & 2 deletions
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)