Skip to content

Commit 3edad35

Browse files
committed
core: Add iter::count
1 parent 85175d6 commit 3edad35

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/libcore/iter.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ fn reverse<A:copy,IA:iterable<A>>(self: IA, blk: fn(A)) {
8282
vec::riter(to_list(self), blk)
8383
}
8484

85+
fn count<A,IA:iterable<A>>(self: IA, x: A) -> uint {
86+
foldl(self, 0u) {|count, value|
87+
if value == x {
88+
count + 1u
89+
} else {
90+
count
91+
}
92+
}
93+
}
94+
8595
fn repeat(times: uint, blk: fn()) {
8696
let i = 0u;
8797
while i < times {
@@ -223,4 +233,9 @@ fn test_max_empty() {
223233
#[test]
224234
fn test_reverse() {
225235
assert to_list(bind reverse([1, 2, 3], _)) == [3, 2, 1];
236+
}
237+
238+
#[test]
239+
fn test_count() {
240+
assert count([1, 2, 1, 2, 1], 1) == 3u;
226241
}

0 commit comments

Comments
 (0)