diff --git a/src/lib.rs b/src/lib.rs index 07465bb..8d9bfa1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -605,6 +605,31 @@ impl NonEmpty { } } + /// Similar functionality as [`NonEmpty::map`], but enumerated. + /// + /// # Examples + /// + /// ``` + /// use nonempty::NonEmpty; + /// + /// let non_empty = NonEmpty::from((1, vec![2, 3, 4, 5])); + /// + /// let indices = non_empty.enumerated_map(|index, i| index); + /// + /// let expected = NonEmpty::from((0, vec![1, 2, 3, 4])); + /// + /// assert_eq!(indices, expected); + /// ``` + pub fn enumerated_map(self, mut f: F) -> NonEmpty + where + F: FnMut((usize, T)) -> U, + { + NonEmpty { + head: f((0, self.head)), + tail: self.tail.into_iter().enumerate().map(f).collect(), + } + } + /// A structure preserving, fallible mapping function. pub fn try_map(self, mut f: F) -> Result, E> where