Skip to content

Commit a0c2949

Browse files
committed
libcore: add a ScanIterator, a generalisation of MapIterator to have internal state.
1 parent 4ff701b commit a0c2949

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

src/libcore/iterator.rs

+43-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ pub trait IteratorUtil<A> {
3939
fn take_while<'r>(self, predicate: &'r fn(&A) -> bool) -> TakeWhileIterator<'r, A, Self>;
4040
fn skip(self, n: uint) -> SkipIterator<Self>;
4141
fn take(self, n: uint) -> TakeIterator<Self>;
42+
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
43+
-> ScanIterator<'r, A, B, Self, St>;
4244
fn advance(&mut self, f: &fn(A) -> bool);
4345
}
4446

@@ -93,6 +95,12 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
9395
TakeIterator{iter: self, n: n}
9496
}
9597

98+
#[inline(always)]
99+
fn scan<'r, St, B>(self, initial_state: St, f: &'r fn(&mut St, A) -> Option<B>)
100+
-> ScanIterator<'r, A, B, T, St> {
101+
ScanIterator{iter: self, f: f, state: initial_state}
102+
}
103+
96104
/// A shim implementing the `for` loop iteration protocol for iterator objects
97105
#[inline]
98106
fn advance(&mut self, f: &fn(A) -> bool) {
@@ -306,12 +314,13 @@ impl<A, T: Iterator<A>> Iterator<A> for TakeIterator<T> {
306314

307315
pub struct UnfoldrIterator<'self, A, St> {
308316
priv f: &'self fn(&mut St) -> Option<A>,
309-
priv state: St
317+
state: St
310318
}
311319

312320
pub impl<'self, A, St> UnfoldrIterator<'self, A, St> {
313321
#[inline]
314-
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St) -> UnfoldrIterator<'self, A, St> {
322+
fn new(f: &'self fn(&mut St) -> Option<A>, initial_state: St)
323+
-> UnfoldrIterator<'self, A, St> {
315324
UnfoldrIterator {
316325
f: f,
317326
state: initial_state
@@ -326,6 +335,19 @@ impl<'self, A, St> Iterator<A> for UnfoldrIterator<'self, A, St> {
326335
}
327336
}
328337

338+
pub struct ScanIterator<'self, A, B, T, St> {
339+
priv iter: T,
340+
priv f: &'self fn(&mut St, A) -> Option<B>,
341+
state: St
342+
}
343+
344+
impl<'self, A, B, T: Iterator<A>, St> Iterator<B> for ScanIterator<'self, A, B, T, St> {
345+
#[inline]
346+
fn next(&mut self) -> Option<B> {
347+
self.iter.next().chain(|a| (self.f)(&mut self.state, a))
348+
}
349+
}
350+
329351
#[cfg(test)]
330352
mod tests {
331353
use super::*;
@@ -406,6 +428,25 @@ mod tests {
406428
assert_eq!(i, ys.len());
407429
}
408430

431+
#[test]
432+
fn test_iterator_scan() {
433+
// test the type inference
434+
fn add(old: &mut int, new: &uint) -> Option<float> {
435+
*old += *new as int;
436+
Some(*old as float)
437+
}
438+
let xs = [0u, 1, 2, 3, 4];
439+
let ys = [0f, 1f, 3f, 6f, 10f];
440+
441+
let mut it = xs.iter().scan(0, add);
442+
let mut i = 0;
443+
for it.advance |x| {
444+
assert_eq!(x, ys[i]);
445+
i += 1;
446+
}
447+
assert_eq!(i, ys.len());
448+
}
449+
409450
#[test]
410451
fn test_unfoldr() {
411452
fn count(st: &mut uint) -> Option<uint> {

0 commit comments

Comments
 (0)