Skip to content

Commit e0c8e34

Browse files
committed
---
yaml --- r: 5507 b: refs/heads/master c: 3a7a294 h: refs/heads/master i: 5505: 84df55f 5503: 530c233 v: v3
1 parent c419277 commit e0c8e34

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
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: 7979bbbf5132104ca3359919f7c515e3a9d8dcd6
2+
refs/heads/master: 3a7a2943ddd1e7e91f625d6459c3e0636ca129cb

trunk/src/lib/vec.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,17 @@ fn filter_map<@T, @U>(f: block(T) -> option::t<U>, v: [mutable? T]) -> [U] {
213213
ret result;
214214
}
215215

216+
fn filter<@T>(f: block(T) -> bool, v: [mutable? T]) -> [T] {
217+
let result = [];
218+
for elem: T in v {
219+
let elem2 = elem; // satisfies alias checker
220+
if f(elem2) {
221+
result += [elem2];
222+
}
223+
}
224+
ret result;
225+
}
226+
216227
fn foldl<@T, @U>(p: block(U, T) -> U, z: U, v: [mutable? T]) -> U {
217228
let sz = len(v);
218229
if sz == 0u { ret z; }

trunk/src/test/stdtest/vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ fn square_alias(n: uint) -> uint { ret n * n; }
1212

1313
pure fn is_three(n: uint) -> bool { ret n == 3u; }
1414

15+
pure fn is_odd(n: uint) -> bool { ret n % 2u == 1u; }
16+
1517
fn square_if_odd(n: uint) -> option::t<uint> {
1618
ret if n % 2u == 1u { some(n * n) } else { none };
1719
}
@@ -269,7 +271,12 @@ fn test_filter_map() {
269271
assert (filter_map(halve, all_odd1) == []);
270272
assert (filter_map(halve, all_odd2) == []);
271273
assert (filter_map(halve, mix) == mix_dest);
274+
}
272275

276+
#[test]
277+
fn test_filter() {
278+
assert filter(is_odd, [1u, 2u, 3u]) == [1u, 3u];
279+
assert filter(is_three, [1u, 2u, 4u, 8u, 16u]) == [];
273280
}
274281

275282
#[test]

0 commit comments

Comments
 (0)