Skip to content

Commit

Permalink
Added body to free::zip
Browse files Browse the repository at this point in the history
Added body to the code example in the docs for free::zip.
The code body added uses two types to make it obvious how `zip`
functions.
  • Loading branch information
JoelMon committed Jul 8, 2022
1 parent 6b7063a commit e75a656
Showing 1 changed file with 60 additions and 46 deletions.
106 changes: 60 additions & 46 deletions src/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,24 @@ use std::iter::{self, Zip};
type VecIntoIter<T> = alloc::vec::IntoIter<T>;

#[cfg(feature = "use_alloc")]
use alloc::{
string::String,
};
use alloc::string::String;

use crate::Itertools;
use crate::intersperse::{Intersperse, IntersperseWith};
use crate::Itertools;

pub use crate::adaptors::{
interleave,
merge,
put_back,
};
pub use crate::adaptors::{interleave, merge, put_back};
#[cfg(feature = "use_alloc")]
pub use crate::put_back_n_impl::put_back_n;
pub use crate::kmerge_impl::kmerge;
pub use crate::merge_join::merge_join_by;
#[cfg(feature = "use_alloc")]
pub use crate::multipeek_impl::multipeek;
#[cfg(feature = "use_alloc")]
pub use crate::peek_nth::peek_nth;
#[cfg(feature = "use_alloc")]
pub use crate::kmerge_impl::kmerge;
pub use crate::zip_eq_impl::zip_eq;
pub use crate::merge_join::merge_join_by;
pub use crate::put_back_n_impl::put_back_n;
#[cfg(feature = "use_alloc")]
pub use crate::rciter_impl::rciter;
pub use crate::zip_eq_impl::zip_eq;

/// Iterate `iterable` with a particular value inserted between each element.
///
Expand All @@ -45,8 +39,9 @@ pub use crate::rciter_impl::rciter;
/// itertools::assert_equal(intersperse((0..3), 8), vec![0, 8, 1, 8, 2]);
/// ```
pub fn intersperse<I>(iterable: I, element: I::Item) -> Intersperse<I::IntoIter>
where I: IntoIterator,
<I as IntoIterator>::Item: Clone
where
I: IntoIterator,
<I as IntoIterator>::Item: Clone,
{
Itertools::intersperse(iterable.into_iter(), element)
}
Expand All @@ -64,8 +59,9 @@ pub fn intersperse<I>(iterable: I, element: I::Item) -> Intersperse<I::IntoIter>
/// assert_eq!(i, 8);
/// ```
pub fn intersperse_with<I, F>(iterable: I, element: F) -> IntersperseWith<I::IntoIter, F>
where I: IntoIterator,
F: FnMut() -> I::Item
where
I: IntoIterator,
F: FnMut() -> I::Item,
{
Itertools::intersperse_with(iterable.into_iter(), element)
}
Expand All @@ -82,7 +78,8 @@ pub fn intersperse_with<I, F>(iterable: I, element: F) -> IntersperseWith<I::Int
/// }
/// ```
pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
where I: IntoIterator
where
I: IntoIterator,
{
iterable.into_iter().enumerate()
}
Expand All @@ -99,8 +96,9 @@ pub fn enumerate<I>(iterable: I) -> iter::Enumerate<I::IntoIter>
/// }
/// ```
pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
where I: IntoIterator,
I::IntoIter: DoubleEndedIterator
where
I: IntoIterator,
I::IntoIter: DoubleEndedIterator,
{
iterable.into_iter().rev()
}
Expand All @@ -112,14 +110,20 @@ pub fn rev<I>(iterable: I) -> iter::Rev<I::IntoIter>
/// ```
/// use itertools::zip;
///
/// let data = [1, 2, 3, 4, 5];
/// for (a, b) in zip(&data, &data[1..]) {
/// /* loop body */
/// let data_1 = [1, 2, 3, 4, 5];
/// let data_2 = ['a', 'b', 'c'];
/// let mut result: Vec<(i32, char)> = Vec::new();
///
/// for (a, b) in zip(&data_1, &data_2) {
/// result.push((*a, *b));
/// }
/// assert_eq!(result, vec![(1, 'a'),(2, 'b'),(3, 'c')]);
/// ```
///
pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
where I: IntoIterator,
J: IntoIterator
where
I: IntoIterator,
J: IntoIterator,
{
i.into_iter().zip(j)
}
Expand All @@ -135,9 +139,13 @@ pub fn zip<I, J>(i: I, j: J) -> Zip<I::IntoIter, J::IntoIter>
/// /* loop body */
/// }
/// ```
pub fn chain<I, J>(i: I, j: J) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
where I: IntoIterator,
J: IntoIterator<Item = I::Item>
pub fn chain<I, J>(
i: I,
j: J,
) -> iter::Chain<<I as IntoIterator>::IntoIter, <J as IntoIterator>::IntoIter>
where
I: IntoIterator,
J: IntoIterator<Item = I::Item>,
{
i.into_iter().chain(j)
}
Expand All @@ -152,8 +160,9 @@ pub fn chain<I, J>(i: I, j: J) -> iter::Chain<<I as IntoIterator>::IntoIter, <J
/// assert_eq!(cloned(b"abc").next(), Some(b'a'));
/// ```
pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned<I::IntoIter>
where I: IntoIterator<Item=&'a T>,
T: Clone,
where
I: IntoIterator<Item = &'a T>,
T: Clone,
{
iterable.into_iter().cloned()
}
Expand All @@ -168,8 +177,9 @@ pub fn cloned<'a, I, T: 'a>(iterable: I) -> iter::Cloned<I::IntoIter>
/// assert_eq!(fold(&[1., 2., 3.], 0., |a, &b| f32::max(a, b)), 3.);
/// ```
pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
where I: IntoIterator,
F: FnMut(B, I::Item) -> B
where
I: IntoIterator,
F: FnMut(B, I::Item) -> B,
{
iterable.into_iter().fold(init, f)
}
Expand All @@ -184,8 +194,9 @@ pub fn fold<I, B, F>(iterable: I, init: B, f: F) -> B
/// assert!(all(&[1, 2, 3], |elt| *elt > 0));
/// ```
pub fn all<I, F>(iterable: I, f: F) -> bool
where I: IntoIterator,
F: FnMut(I::Item) -> bool
where
I: IntoIterator,
F: FnMut(I::Item) -> bool,
{
iterable.into_iter().all(f)
}
Expand All @@ -200,8 +211,9 @@ pub fn all<I, F>(iterable: I, f: F) -> bool
/// assert!(any(&[0, -1, 2], |elt| *elt > 0));
/// ```
pub fn any<I, F>(iterable: I, f: F) -> bool
where I: IntoIterator,
F: FnMut(I::Item) -> bool
where
I: IntoIterator,
F: FnMut(I::Item) -> bool,
{
iterable.into_iter().any(f)
}
Expand All @@ -216,8 +228,9 @@ pub fn any<I, F>(iterable: I, f: F) -> bool
/// assert_eq!(max(0..10), Some(9));
/// ```
pub fn max<I>(iterable: I) -> Option<I::Item>
where I: IntoIterator,
I::Item: Ord
where
I: IntoIterator,
I::Item: Ord,
{
iterable.into_iter().max()
}
Expand All @@ -232,13 +245,13 @@ pub fn max<I>(iterable: I) -> Option<I::Item>
/// assert_eq!(min(0..10), Some(0));
/// ```
pub fn min<I>(iterable: I) -> Option<I::Item>
where I: IntoIterator,
I::Item: Ord
where
I: IntoIterator,
I::Item: Ord,
{
iterable.into_iter().min()
}


/// Combine all iterator elements into one String, separated by `sep`.
///
/// [`IntoIterator`] enabled version of [`Itertools::join`].
Expand All @@ -250,8 +263,9 @@ pub fn min<I>(iterable: I) -> Option<I::Item>
/// ```
#[cfg(feature = "use_alloc")]
pub fn join<I>(iterable: I, sep: &str) -> String
where I: IntoIterator,
I::Item: Display
where
I: IntoIterator,
I::Item: Display,
{
iterable.into_iter().join(sep)
}
Expand All @@ -268,9 +282,9 @@ pub fn join<I>(iterable: I, sep: &str) -> String
/// ```
#[cfg(feature = "use_alloc")]
pub fn sorted<I>(iterable: I) -> VecIntoIter<I::Item>
where I: IntoIterator,
I::Item: Ord
where
I: IntoIterator,
I::Item: Ord,
{
iterable.into_iter().sorted()
}

0 comments on commit e75a656

Please sign in to comment.