Skip to content

Remove .flatten() and flatten() #316

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

Merged
merged 2 commits into from
Nov 24, 2018
Merged
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
76 changes: 0 additions & 76 deletions src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use std::mem::replace;
use std::iter::{Fuse, Peekable, FromIterator};
use std::marker::PhantomData;
use size_hint;
use fold;

macro_rules! clone_fields {
($name:ident, $base:expr, $($field:ident),+) => (
Expand Down Expand Up @@ -1035,81 +1034,6 @@ impl_tuple_combination!(Tuple3Combination Tuple2Combination ; A, A, A, A ; a b);
impl_tuple_combination!(Tuple4Combination Tuple3Combination ; A, A, A, A, A; a b c);


/// An iterator adapter to simply flatten a structure.
///
/// See [`.flatten()`](../trait.Itertools.html#method.flatten) for more information.
#[derive(Clone, Debug)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct Flatten<I, J> {
iter: I,
front: Option<J>,
}

/// Flatten an iterable of iterables into a single combined sequence of all
/// the elements in the iterables.
///
/// This is more or less equivalent to `.flat_map` with an identity
/// function.
///
/// This is an `IntoIterator`-enabled version of the [`.flatten()`][1] adaptor.
///
/// [1]: trait.Itertools.html#method.flatten
///
/// ```
/// use itertools::flatten;
///
/// let data = vec![vec![1, 2, 3], vec![4, 5, 6]];
///
/// itertools::assert_equal(flatten(&data),
/// &[1, 2, 3, 4, 5, 6]);
/// ```
pub fn flatten<I, J>(iterable: I) -> Flatten<I::IntoIter, J>
where I: IntoIterator,
I::Item: IntoIterator<IntoIter=J, Item=J::Item>,
J: Iterator,
{
Flatten {
iter: iterable.into_iter(),
front: None,
}
}

impl<I, J> Iterator for Flatten<I, J>
where I: Iterator,
I::Item: IntoIterator<IntoIter=J, Item=J::Item>,
J: Iterator,
{
type Item = J::Item;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(ref mut f) = self.front {
match f.next() {
elt @ Some(_) => return elt,
None => { }
}
}
if let Some(next_front) = self.iter.next() {
self.front = Some(next_front.into_iter());
} else {
break;
}
}
None
}

// special case to convert segmented iterator into consecutive loops
fn fold<Acc, G>(self, init: Acc, mut f: G) -> Acc
where G: FnMut(Acc, Self::Item) -> Acc,
{
let mut accum = init;
if let Some(iter) = self.front {
accum = fold(iter, accum, &mut f);
}
self.iter.fold(accum, move |accum, iter| fold(iter, accum, &mut f))
}

}

/// An iterator adapter to apply a transformation within a nested `Result`.
///
/// See [`.map_results()`](../trait.Itertools.html#method.map_results) for more information.
Expand Down
25 changes: 0 additions & 25 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ pub mod structs {
WhileSome,
Coalesce,
TupleCombinations,
Flatten,
Positions,
Update,
};
Expand Down Expand Up @@ -107,7 +106,6 @@ pub mod structs {
pub use ziptuple::Zip;
}
pub use structs::*;
pub use adaptors::flatten;
pub use concat_impl::concat;
pub use cons_tuples_impl::cons_tuples;
pub use diff::diff_with;
Expand Down Expand Up @@ -1108,29 +1106,6 @@ pub trait Itertools : Iterator {
pad_tail::pad_using(self, min, f)
}

/// Flatten an iterator of iterables into a single combined sequence of all
/// the elements in the iterables.
///
/// This is more or less equivalent to `.flat_map` with an identity
/// function.
///
/// See also the [`flatten`](fn.flatten.html) function.
///
/// ```ignore
/// use itertools::Itertools;
///
/// let data = vec![vec![1, 2, 3], vec![4, 5, 6]];
/// let flattened = data.iter().flatten();
///
/// itertools::assert_equal(flattened, &[1, 2, 3, 4, 5, 6]);
/// ```
fn flatten(self) -> Flatten<Self, <Self::Item as IntoIterator>::IntoIter>
where Self: Sized,
Self::Item: IntoIterator
{
adaptors::flatten(self)
}

/// Return an iterator adaptor that wraps each element in a `Position` to
/// ease special-case handling of the first or last elements.
///
Expand Down
11 changes: 0 additions & 11 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use itertools::{
multizip,
EitherOrBoth,
};
use itertools::flatten;
use itertools::free::{
cloned,
enumerate,
Expand Down Expand Up @@ -604,16 +603,6 @@ quickcheck! {
true
}

fn equal_flatten(a: Vec<Option<i32>>) -> bool {
itertools::equal(flatten(&a),
a.iter().filter_map(|x| x.as_ref()))
}

fn equal_flatten_vec(a: Vec<Vec<u8>>) -> bool {
itertools::equal(flatten(&a),
a.iter().flat_map(|x| x))
}

fn equal_combinations_2(a: Vec<u8>) -> bool {
let mut v = Vec::new();
for (i, x) in enumerate(&a) {
Expand Down
14 changes: 0 additions & 14 deletions tests/test_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use core::iter;

use it::flatten;
use it::Itertools;
use it::interleave;
use it::multizip;
Expand Down Expand Up @@ -236,19 +235,6 @@ fn part() {
assert_eq!(data, [9, 6, 3, 4, 5, 2, 7, 8, 1]);
}

#[test]
fn flatten_clone() {
let data = &[
&[1,2,3],
&[4,5,6]
];
let flattened1 = flatten(data.into_iter().cloned());
let flattened2 = flattened1.clone();

it::assert_equal(flattened1, &[1,2,3,4,5,6]);
it::assert_equal(flattened2, &[1,2,3,4,5,6]);
}

#[test]
fn tree_fold1() {
for i in 0..100 {
Expand Down
18 changes: 0 additions & 18 deletions tests/test_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#[macro_use] extern crate itertools as it;
extern crate permutohedron;

use it::flatten;
use it::Itertools;
use it::multizip;
use it::multipeek;
Expand Down Expand Up @@ -538,23 +537,6 @@ fn concat_non_empty() {
assert_eq!(data.into_iter().concat(), vec![1,2,3,4,5,6,7,8,9])
}

#[test]
fn flatten_iter() {
let data = vec![vec![1,2,3], vec![4,5], vec![], vec![6]];
it::assert_equal(flatten(data), vec![1,2,3,4,5,6]);
}

#[test]
fn flatten_fold() {
let xs = [0, 1, 1, 1, 2, 1, 3, 3];
let ch = xs.iter().chunks(3);
let mut iter = flatten(&ch);
iter.next();
let mut xs_d = Vec::new();
iter.fold((), |(), &elt| xs_d.push(elt));
assert_eq!(&xs_d[..], &xs[1..]);
}

#[test]
fn combinations() {
assert!((1..3).combinations(5).next().is_none());
Expand Down