Skip to content

Commit 3263324

Browse files
committed
---
yaml --- r: 142514 b: refs/heads/try2 c: f7b293b h: refs/heads/master v: v3
1 parent 529a8b8 commit 3263324

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ refs/heads/snap-stage3: 78a7676898d9f80ab540c6df5d4c9ce35bb50463
55
refs/heads/try: 519addf6277dbafccbb4159db4b710c37eaa2ec5
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
8-
refs/heads/try2: b0a9d8193f121db8115a1010659ee46f7186b2d5
8+
refs/heads/try2: f7b293bc75684a368e20567b5987c482dc5f8550
99
refs/heads/dist-snap: ba4081a5a8573875fed17545846f6f6902c8ba8d
1010
refs/tags/release-0.2: c870d2dffb391e14efb05aa27898f1f6333a9596
1111
refs/tags/release-0.3: b5f0d0f648d9a6153664837026ba1be43d3e2503

branches/try2/src/libstd/iterator.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,25 @@ pub trait IteratorUtil<A> {
245245
fn flat_map_<'r, B, U: Iterator<B>>(self, f: &'r fn(A) -> U)
246246
-> FlatMapIterator<'r, A, B, Self, U>;
247247

248+
/// Creates an iterator that calls a function with a reference to each
249+
/// element before yielding it. This is often useful for debugging an
250+
/// iterator pipeline.
251+
///
252+
/// # Example
253+
///
254+
/// ~~~ {.rust}
255+
///let xs = [1u, 4, 2, 3, 8, 9, 6];
256+
///let sum = xs.iter()
257+
/// .transform(|&x| x)
258+
/// .peek_(|&x| debug!("filtering %u", x))
259+
/// .filter(|&x| x % 2 == 0)
260+
/// .peek_(|&x| debug!("%u made it through", x))
261+
/// .sum();
262+
///println(sum.to_str());
263+
/// ~~~
264+
// FIXME: #5898: should be called `peek`
265+
fn peek_<'r>(self, f: &'r fn(&A)) -> PeekIterator<'r, A, Self>;
266+
248267
/// An adaptation of an external iterator to the for-loop protocol of rust.
249268
///
250269
/// # Example
@@ -442,6 +461,12 @@ impl<A, T: Iterator<A>> IteratorUtil<A> for T {
442461
FlatMapIterator{iter: self, f: f, subiter: None }
443462
}
444463

464+
// FIXME: #5898: should be called `peek`
465+
#[inline]
466+
fn peek_<'r>(self, f: &'r fn(&A)) -> PeekIterator<'r, A, T> {
467+
PeekIterator{iter: self, f: f}
468+
}
469+
445470
/// A shim implementing the `for` loop iteration protocol for iterator objects
446471
#[inline]
447472
fn advance(&mut self, f: &fn(A) -> bool) -> bool {
@@ -1041,6 +1066,32 @@ impl<'self, A, T: Iterator<A>, B, U: Iterator<B>> Iterator<B> for
10411066
}
10421067
}
10431068

1069+
/// An iterator that calls a function with a reference to each
1070+
/// element before yielding it.
1071+
pub struct PeekIterator<'self, A, T> {
1072+
priv iter: T,
1073+
priv f: &'self fn(&A)
1074+
}
1075+
1076+
impl<'self, A, T: Iterator<A>> Iterator<A> for PeekIterator<'self, A, T> {
1077+
#[inline]
1078+
fn next(&mut self) -> Option<A> {
1079+
let next = self.iter.next();
1080+
1081+
match next {
1082+
Some(ref a) => (self.f)(a),
1083+
None => ()
1084+
}
1085+
1086+
next
1087+
}
1088+
1089+
#[inline]
1090+
fn size_hint(&self) -> (uint, Option<uint>) {
1091+
self.iter.size_hint()
1092+
}
1093+
}
1094+
10441095
/// An iterator which just modifies the contained state throughout iteration.
10451096
pub struct UnfoldrIterator<'self, A, St> {
10461097
priv f: &'self fn(&mut St) -> Option<A>,
@@ -1236,6 +1287,20 @@ mod tests {
12361287
assert_eq!(i, ys.len());
12371288
}
12381289

1290+
#[test]
1291+
fn test_peek() {
1292+
let xs = [1u, 2, 3, 4];
1293+
let mut n = 0;
1294+
1295+
let ys = xs.iter()
1296+
.transform(|&x| x)
1297+
.peek_(|_| n += 1)
1298+
.collect::<~[uint]>();
1299+
1300+
assert_eq!(n, xs.len());
1301+
assert_eq!(xs, ys.as_slice());
1302+
}
1303+
12391304
#[test]
12401305
fn test_unfoldr() {
12411306
fn count(st: &mut uint) -> Option<uint> {

0 commit comments

Comments
 (0)