Skip to content

Commit

Permalink
Add Itertools::enumerate_ok
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 146fb0e
Author: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com>
Date:   Sun Jan 16 12:44:41 2022 +0400

    mob next [ci-skip] [ci skip] [skip ci]

commit aa2db90
Author: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com>
Date:   Sun Jan 16 12:19:55 2022 +0400

    mob next [ci-skip] [ci skip] [skip ci]

commit f473fee
Author: Saatvik Shah <6854735+saatvikshah@users.noreply.github.com>
Date:   Sun Jan 16 09:09:32 2022 +0100

    mob next [ci-skip] [ci skip] [skip ci]

commit ff4628c
Author: Preet Dua <615318+prabhpreet@users.noreply.github.com>
Date:   Sat Jan 15 23:58:18 2022 -0800

    mob next [ci-skip] [ci skip] [skip ci]

commit 90bdeb6
Author: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com>
Date:   Sun Jan 16 11:46:53 2022 +0400

    mob next [ci-skip] [ci skip] [skip ci]

commit 7044ed3
Author: Shahar Dawn Or <mightyiampresence@gmail.com>
Date:   Sat Jan 15 16:58:27 2022 +0700

    mob next [ci-skip] [ci skip] [skip ci]

commit 659639e
Author: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com>
Date:   Sat Jan 15 13:54:21 2022 +0400

    mob next [ci-skip] [ci skip] [skip ci]

commit a305d97
Author: Preet Dua <615318+prabhpreet@users.noreply.github.com>
Date:   Sat Jan 15 01:43:33 2022 -0800

    mob next [ci-skip] [ci skip] [skip ci]

commit f08c2af
Author: Roland Fredenhagen <dev@modprog.de>
Date:   Sat Jan 15 10:33:59 2022 +0100

    mob next [ci-skip] [ci skip] [skip ci]

commit 48bd032
Author: Shahar Dawn Or <mightyiampresence@gmail.com>
Date:   Sat Jan 15 16:22:06 2022 +0700

    mob next [ci-skip] [ci skip] [skip ci]

commit db02884
Author: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com>
Date:   Sat Jan 15 13:10:44 2022 +0400

    mob next [ci-skip] [ci skip] [skip ci]

Co-authored-by: Saatvik Shah <6854735+saatvikshah@users.noreply.github.com>
Co-authored-by: Roland Fredenhagen <dev@modprog.de>
Co-authored-by: Shahar Dawn Or <mightyiampresence@gmail.com>
Co-authored-by: Preet Dua <615318+prabhpreet@users.noreply.github.com>
  • Loading branch information
5 people committed Jan 16, 2022
1 parent 6c4fc2f commit 27f1fb1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
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

0 comments on commit 27f1fb1

Please sign in to comment.