From 479b4c4862972b9f7e796927b71674fd88cdb407 Mon Sep 17 00:00:00 2001 From: NoneTheWisr <12655815+NoneTheWisr@users.noreply.github.com> Date: Sun, 16 Jan 2022 12:47:28 +0400 Subject: [PATCH] Add Itertools::enumerate_ok Squashed commit of the following: commit 146fb0e30de632787daffcf3208f30878490225b 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 aa2db90da74f3b7be72f0456f281c20785a38e2f 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 f473fee490788b468f08ca911b0fb96e27b82129 Author: sshah1 Date: Sun Jan 16 09:09:32 2022 +0100 mob next [ci-skip] [ci skip] [skip ci] commit ff4628c27ffdcdbc8ab324dc5b4702d650cc7a45 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 90bdeb6c313ca22c59af26fbf61a22f41374b959 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 7044ed31f7f0303976214efa000e0a6b36daa341 Author: Shahar Dawn Or Date: Sat Jan 15 16:58:27 2022 +0700 mob next [ci-skip] [ci skip] [skip ci] commit 659639e64a21407f0f7556355abbc29e07bcfa9d 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 a305d9745428bb9d56d21d3e9f69dadc45ac6356 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 f08c2afa2b9a177056befa72b0ce1ab9032e62e8 Author: Roland Fredenhagen Date: Sat Jan 15 10:33:59 2022 +0100 mob next [ci-skip] [ci skip] [skip ci] commit 48bd0321e205e8dfaa8d1bbbdd370a6583f07eeb Author: Shahar Dawn Or Date: Sat Jan 15 16:22:06 2022 +0700 mob next [ci-skip] [ci skip] [skip ci] commit db02884e85df53995b767b80d6f658ed19c43062 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: sshah1 Co-authored-by: Roland Fredenhagen Co-authored-by: Shahar Dawn Or Co-authored-by: Preet Dua <615318+prabhpreet@users.noreply.github.com> --- src/adaptors/mod.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 18 ++++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/src/adaptors/mod.rs b/src/adaptors/mod.rs index 2010f535b..6a57ce566 100644 --- a/src/adaptors/mod.rs +++ b/src/adaptors/mod.rs @@ -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 { + iter: I, + index: usize +} + +/// Create a new `EnumerateOk` iterator. +pub fn enumerate_ok(iter: I) -> EnumerateOk +where + I: Iterator>, +{ + EnumerateOk { + iter, + index: 0 + } +} + +impl fmt::Debug for EnumerateOk +where + I: fmt::Debug, +{ + debug_fmt_fields!(EnumerateOk, iter); +} + +impl Iterator for EnumerateOk + where I: Iterator>, +{ + type Item = Result<(usize,T),E>; + + fn next(&mut self) -> Option { + 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. diff --git a/src/lib.rs b/src/lib.rs index df95e19ba..ce7ea77ab 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -99,6 +99,7 @@ pub mod structs { Batching, MapInto, MapOk, + EnumerateOk, Merge, MergeBy, TakeWhileRef, @@ -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(self) -> EnumerateOk + where Self: Iterator> + Sized, + { + adaptors::enumerate_ok(self) + } + /// Return an iterator adaptor that applies the provided closure /// to every `Result::Ok` value. `Result::Err` values are /// unchanged.