diff options
author | Jakub Kotur <qtr@google.com> | 2020-12-21 17:28:15 +0100 |
---|---|---|
committer | Jakub Kotur <qtr@google.com> | 2021-03-05 16:28:26 +0100 |
commit | a425e55a65d84cc4bdded363daa365cd4c1b84e6 (patch) | |
tree | b63f2f1e94a9289b403c84c2e51079b79ba74c00 /src/concat_impl.rs | |
parent | 54eae68ce52d631abac923a2bd0d5ebe49b8eea5 (diff) | |
download | itertools-a425e55a65d84cc4bdded363daa365cd4c1b84e6.tar.gz |
Initial import of itertools-0.9.0.
Bug: 155309706
Change-Id: Id790c146e836f0eadfb0d8a103cbe2d226a598c3
Diffstat (limited to 'src/concat_impl.rs')
-rw-r--r-- | src/concat_impl.rs | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/src/concat_impl.rs b/src/concat_impl.rs new file mode 100644 index 0000000..6048d18 --- /dev/null +++ b/src/concat_impl.rs @@ -0,0 +1,22 @@ +use crate::Itertools; + +/// Combine all an iterator's elements into one element by using `Extend`. +/// +/// `IntoIterator`-enabled version of `.concat()` +/// +/// This combinator will extend the first item with each of the rest of the +/// items of the iterator. If the iterator is empty, the default value of +/// `I::Item` is returned. +/// +/// ```rust +/// use itertools::concat; +/// +/// let input = vec![vec![1], vec![2, 3], vec![4, 5, 6]]; +/// assert_eq!(concat(input), vec![1, 2, 3, 4, 5, 6]); +/// ``` +pub fn concat<I>(iterable: I) -> I::Item + where I: IntoIterator, + I::Item: Extend<<<I as IntoIterator>::Item as IntoIterator>::Item> + IntoIterator + Default +{ + iterable.into_iter().fold1(|mut a, b| { a.extend(b); a }).unwrap_or_else(|| <_>::default()) +} |