Skip to content

Commit 9d2cd03

Browse files
committed
Add PeekMap and adaptors::peek_map
1 parent cdf12df commit 9d2cd03

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

src/adaptors/map.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::iter::Peekable;
12
use std::iter::FromIterator;
23
use std::marker::PhantomData;
34

@@ -122,3 +123,41 @@ pub fn map_into<I, R>(iter: I) -> MapInto<I, R> {
122123
f: MapSpecialCaseFnInto(PhantomData),
123124
}
124125
}
126+
127+
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
128+
pub struct PeekMap<I, F, U>
129+
where
130+
I: Iterator,
131+
F: FnMut(I::Item, Option<&I::Item>) -> U,
132+
{
133+
iter: Peekable<I>,
134+
f: F,
135+
}
136+
137+
impl<I, F, U> Iterator for PeekMap<I, F, U>
138+
where
139+
I: Iterator,
140+
F: FnMut(I::Item, Option<&I::Item>) -> U,
141+
{
142+
type Item = U;
143+
144+
fn next(&mut self) -> Option<Self::Item> {
145+
Some((self.f)(self.iter.next()?, self.iter.peek()))
146+
}
147+
148+
fn size_hint(&self) -> (usize, Option<usize>) {
149+
self.iter.size_hint()
150+
}
151+
}
152+
153+
/// Create a new `PeekMap` iterator.
154+
pub fn peek_map<I, F, U>(iter: I, f: F) -> PeekMap<I, F, U>
155+
where
156+
I: Iterator,
157+
F: FnMut(I::Item, Option<&I::Item>) -> U,
158+
{
159+
PeekMap {
160+
iter: iter.peekable(),
161+
f,
162+
}
163+
}

src/adaptors/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod multi_product;
1010
pub use self::coalesce::*;
1111
#[allow(deprecated)]
1212
pub use self::map::MapResults;
13-
pub use self::map::{map_into, map_ok, MapInto, MapOk};
13+
pub use self::map::{map_into, map_ok, peek_map, MapInto, MapOk, PeekMap};
1414
#[cfg(feature = "use_alloc")]
1515
pub use self::multi_product::*;
1616

src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ pub mod structs {
8484
pub use crate::adaptors::MultiProduct;
8585
pub use crate::adaptors::{
8686
Batching, Coalesce, Dedup, DedupBy, DedupByWithCount, DedupWithCount, FilterMapOk,
87-
FilterOk, Interleave, InterleaveShortest, MapInto, MapOk, Positions, Product, PutBack,
88-
TakeWhileRef, TupleCombinations, Update, WhileSome,
87+
FilterOk, Interleave, InterleaveShortest, MapInto, MapOk, PeekMap, Positions, Product,
88+
PutBack, TakeWhileRef, TupleCombinations, Update, WhileSome,
8989
};
9090
#[allow(deprecated)]
9191
pub use crate::adaptors::{MapResults, Step};
@@ -3964,6 +3964,14 @@ pub trait Itertools: Iterator {
39643964
_ => Err(sh),
39653965
}
39663966
}
3967+
3968+
fn peek_map<B, F>(self, f: F) -> PeekMap<Self, F, B>
3969+
where
3970+
Self: Sized,
3971+
F: FnMut(Self::Item, Option<&Self::Item>) -> B,
3972+
{
3973+
adaptors::peek_map(self, f)
3974+
}
39673975
}
39683976

39693977
impl<T: ?Sized> Itertools for T where T: Iterator {}

0 commit comments

Comments
 (0)