Add position() to iter/iter-trait

This commit is contained in:
Ben Blum 2012-06-25 18:04:00 -04:00
parent 9f7e62ea20
commit e56ba156e2
2 changed files with 17 additions and 0 deletions

View File

@ -16,6 +16,9 @@ fn foldl<B>(+b0: B, blk: fn(B, A) -> B) -> B {
}
fn contains(x: A) -> bool { iter::contains(self, x) }
fn count(x: A) -> uint { iter::count(self, x) }
fn position(f: fn(A) -> bool) -> option<uint> {
iter::position(self, f)
}
}
impl extensions<A:copy> for IMPL_T<A> {

View File

@ -85,6 +85,20 @@ fn count<A,IA:base_iter<A>>(self: IA, x: A) -> uint {
}
}
fn position<A,IA:base_iter<A>>(self: IA, f: fn(A) -> bool)
-> option<uint> {
let mut i = 0;
for self.each {|a|
if f(a) { ret some(i); }
i += 1;
}
ret none;
}
// note: 'rposition' would only make sense to provide with a bidirectional
// iter interface, such as would provide "reach" in addition to "each". as is,
// it would have to be implemented with foldr, which is too inefficient.
fn repeat(times: uint, blk: fn()) {
let mut i = 0u;
while i < times {