Skip to content

Commit 76fc3ad

Browse files
committed
---
yaml --- r: 41632 b: refs/heads/master c: c766924 h: refs/heads/master v: v3
1 parent 62bd2c4 commit 76fc3ad

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 2891a49f0d9b3c6cdd4fa8b0bd1f2a5d06cdac58
2+
refs/heads/master: c766924f44038d47108346bf237f510a3c869bf2
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 2f46b763da2c098913884f101b6d71d69af41b49
55
refs/heads/try: 3d5418789064fdb463e872a4e651af1c628a3650

trunk/src/libcore/vec.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,21 @@ pub pure fn filter_map<T, U: Copy>(v: &[T], f: fn(t: &T) -> Option<U>)
846846
result
847847
}
848848

849+
/**
850+
* Construct a new vector from the elements of a vector for which some
851+
* predicate holds.
852+
*
853+
* Apply function `f` to each element of `v` and return a vector containing
854+
* only those elements for which `f` returned true.
855+
*/
856+
pub fn filter<T>(v: ~[T], f: fn(t: &T) -> bool) -> ~[T] {
857+
let mut result = ~[];
858+
do v.consume |_, elem| {
859+
if f(&elem) { result.push(elem); }
860+
}
861+
result
862+
}
863+
849864
/**
850865
* Construct a new vector from the elements of a vector for which some
851866
* predicate holds.
@@ -1805,6 +1820,7 @@ pub trait OwnedVector<T> {
18051820
fn truncate(&mut self, newlen: uint);
18061821
fn retain(&mut self, f: pure fn(t: &T) -> bool);
18071822
fn consume(self, f: fn(uint, v: T));
1823+
fn filter(self, f: fn(t: &T) -> bool) -> ~[T];
18081824
fn partition(self, f: pure fn(&T) -> bool) -> (~[T], ~[T]);
18091825
}
18101826

@@ -1864,6 +1880,11 @@ impl<T> ~[T]: OwnedVector<T> {
18641880
consume(self, f)
18651881
}
18661882

1883+
#[inline]
1884+
fn filter(self, f: fn(&T) -> bool) -> ~[T] {
1885+
filter(self, f)
1886+
}
1887+
18671888
/**
18681889
* Partitions the vector into those that satisfies the predicate, and
18691890
* those that do not.

0 commit comments

Comments
 (0)