blob: 450f7fce15269c2bd442f3f5bda064ed45be1526 [file] [log] [blame]
Jakub Kotura425e552020-12-21 17:28:15 +01001use crate::Itertools;
2
Joel Galensonb593e252021-06-21 13:15:57 -07003/// Combine all an iterator's elements into one element by using [`Extend`].
Jakub Kotura425e552020-12-21 17:28:15 +01004///
Joel Galensonb593e252021-06-21 13:15:57 -07005/// [`IntoIterator`]-enabled version of [`Itertools::concat`].
Jakub Kotura425e552020-12-21 17:28:15 +01006///
7/// This combinator will extend the first item with each of the rest of the
8/// items of the iterator. If the iterator is empty, the default value of
9/// `I::Item` is returned.
10///
11/// ```rust
12/// use itertools::concat;
13///
14/// let input = vec![vec![1], vec![2, 3], vec![4, 5, 6]];
15/// assert_eq!(concat(input), vec![1, 2, 3, 4, 5, 6]);
16/// ```
17pub fn concat<I>(iterable: I) -> I::Item
18 where I: IntoIterator,
19 I::Item: Extend<<<I as IntoIterator>::Item as IntoIterator>::Item> + IntoIterator + Default
20{
Joel Galenson6f798712021-04-01 17:03:06 -070021 iterable.into_iter().fold1(|mut a, b| { a.extend(b); a }).unwrap_or_else(<_>::default)
Jakub Kotura425e552020-12-21 17:28:15 +010022}