Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Itertools::enumerate_ok #598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,50 @@ impl_tuple_combination!(Tuple10Combination Tuple9Combination; a b c d e f g h i)
impl_tuple_combination!(Tuple11Combination Tuple10Combination; a b c d e f g h i j);
impl_tuple_combination!(Tuple12Combination Tuple11Combination; a b c d e f g h i j k);

/// An iterator adapter to enumerate values within a nested `Result::Ok`.
///
/// See [`.enumerate_ok()`](crate::Itertools::enumerate_ok) for more information.
#[derive(Clone)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct EnumerateOk<I> {
iter: I,
index: usize
}

/// Create a new `EnumerateOk` iterator.
pub fn enumerate_ok<I, T, E>(iter: I) -> EnumerateOk<I>
where
I: Iterator<Item = Result<T, E>>,
{
EnumerateOk {
iter,
index: 0
}
}

impl<I> fmt::Debug for EnumerateOk<I>
where
I: fmt::Debug,
{
debug_fmt_fields!(EnumerateOk, iter);
}

impl<I,T,E> Iterator for EnumerateOk<I>
where I: Iterator<Item = Result<T,E>>,
{
type Item = Result<(usize,T),E>;

fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|item| {
item.map(|v| {
let index = self.index;
self.index +=1;
(index,v)
})
})
}
}

/// An iterator adapter to filter values within a nested `Result::Ok`.
///
/// See [`.filter_ok()`](crate::Itertools::filter_ok) for more information.
Expand Down
18 changes: 18 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub mod structs {
Batching,
MapInto,
MapOk,
EnumerateOk,
Merge,
MergeBy,
TakeWhileRef,
Expand Down Expand Up @@ -846,6 +847,23 @@ pub trait Itertools : Iterator {
self.map_ok(f)
}

/// Return an iterator adaptor that adds an index to `Result::Ok`
/// values. `Result::Err` values are unchanged. The index is incremented
/// only for `Result::Ok` values.
///
/// ```
/// use itertools::Itertools;
///
/// let input = vec![Ok(41), Err(false), Ok(11)];
/// let it = input.into_iter().enumerate_ok();
/// itertools::assert_equal(it, vec![Ok((0, 41)), Err(false), Ok((1, 11))]);
/// ```
fn enumerate_ok<T, E>(self) -> EnumerateOk<Self>
where Self: Iterator<Item = Result<T, E>> + Sized,
{
adaptors::enumerate_ok(self)
}

/// Return an iterator adaptor that applies the provided closure
/// to every `Result::Ok` value. `Result::Err` values are
/// unchanged.
Expand Down