提交 e56ba156 编写于 作者: B Ben Blum

Add position() to iter/iter-trait

上级 9f7e62ea
......@@ -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> {
......
......@@ -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 {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册