Skip to content

Commit

Permalink
Add position() to iter/iter-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
bblum committed Jun 28, 2012
1 parent 9f7e62e commit e56ba15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/libcore/iter-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ impl extensions<A> of iter::base_iter<A> for IMPL_T<A> {
}
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> {
Expand Down
14 changes: 14 additions & 0 deletions src/libcore/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e56ba15

Please sign in to comment.