Skip to content

Commit e56ba15

Browse files
committed
Add position() to iter/iter-trait
1 parent 9f7e62e commit e56ba15

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/libcore/iter-trait.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ impl extensions<A> of iter::base_iter<A> for IMPL_T<A> {
1616
}
1717
fn contains(x: A) -> bool { iter::contains(self, x) }
1818
fn count(x: A) -> uint { iter::count(self, x) }
19+
fn position(f: fn(A) -> bool) -> option<uint> {
20+
iter::position(self, f)
21+
}
1922
}
2023

2124
impl extensions<A:copy> for IMPL_T<A> {

src/libcore/iter.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ fn count<A,IA:base_iter<A>>(self: IA, x: A) -> uint {
8585
}
8686
}
8787

88+
fn position<A,IA:base_iter<A>>(self: IA, f: fn(A) -> bool)
89+
-> option<uint> {
90+
let mut i = 0;
91+
for self.each {|a|
92+
if f(a) { ret some(i); }
93+
i += 1;
94+
}
95+
ret none;
96+
}
97+
98+
// note: 'rposition' would only make sense to provide with a bidirectional
99+
// iter interface, such as would provide "reach" in addition to "each". as is,
100+
// it would have to be implemented with foldr, which is too inefficient.
101+
88102
fn repeat(times: uint, blk: fn()) {
89103
let mut i = 0u;
90104
while i < times {

0 commit comments

Comments
 (0)