15
15
//! #[macro_use] extern crate itertools;
16
16
//! ```
17
17
//!
18
- //! ## License
19
- //! Dual-licensed to be compatible with the Rust project.
20
- //!
21
- //! Licensed under the Apache License, Version 2.0
22
- //! http://www.apache.org/licenses/LICENSE-2.0 or the MIT license
23
- //! http://opensource.org/licenses/MIT, at your
24
- //! option. This file may not be copied, modified, or distributed
25
- //! except according to those terms.
18
+ //! ## Rust Version
26
19
//!
20
+ //! This version of itertools requires Rust 1.12 or later.
27
21
//!
28
22
#![ doc( html_root_url="https://docs.rs/itertools/" ) ]
29
23
@@ -367,15 +361,6 @@ pub trait Itertools : Iterator {
367
361
groupbylazy:: new ( self , key)
368
362
}
369
363
370
- ///
371
- #[ deprecated( note = "renamed to .group_by()" ) ]
372
- fn group_by_lazy < K , F > ( self , key : F ) -> GroupBy < K , Self , F >
373
- where Self : Sized ,
374
- F : FnMut ( & Self :: Item ) -> K ,
375
- {
376
- self . group_by ( key)
377
- }
378
-
379
364
/// Return an *iterable* that can chunk the iterator.
380
365
///
381
366
/// Yield subiterators (chunks) that each yield a fixed number elements,
@@ -410,14 +395,6 @@ pub trait Itertools : Iterator {
410
395
groupbylazy:: new_chunks ( self , size)
411
396
}
412
397
413
- ///
414
- #[ deprecated( note = "renamed to .chunks()" ) ]
415
- fn chunks_lazy ( self , size : usize ) -> IntoChunks < Self >
416
- where Self : Sized ,
417
- {
418
- self . chunks ( size)
419
- }
420
-
421
398
/// Return an iterator over all contiguous windows producing tuples of
422
399
/// a specific size (up to 4).
423
400
///
@@ -621,10 +598,10 @@ pub trait Itertools : Iterator {
621
598
/// let it = vec![a, b, c].into_iter().kmerge();
622
599
/// itertools::assert_equal(it, vec![0, 1, 2, 3, 4, 5]);
623
600
/// ```
624
- fn kmerge ( self ) -> KMerge < << Self as Iterator > :: Item as IntoIterator >:: IntoIter >
601
+ fn kmerge ( self ) -> KMerge < <Self :: Item as IntoIterator >:: IntoIter >
625
602
where Self : Sized ,
626
603
Self :: Item : IntoIterator ,
627
- << Self as Iterator > :: Item as IntoIterator >:: Item : PartialOrd ,
604
+ <Self :: Item as IntoIterator >:: Item : PartialOrd ,
628
605
{
629
606
kmerge ( self )
630
607
}
@@ -650,11 +627,11 @@ pub trait Itertools : Iterator {
650
627
/// assert_eq!(it.last(), Some(-7.));
651
628
/// ```
652
629
fn kmerge_by < F > ( self , first : F )
653
- -> KMergeBy < << Self as Iterator > :: Item as IntoIterator >:: IntoIter , F >
630
+ -> KMergeBy < <Self :: Item as IntoIterator >:: IntoIter , F >
654
631
where Self : Sized ,
655
632
Self :: Item : IntoIterator ,
656
- F : FnMut ( & << Self as Iterator > :: Item as IntoIterator >:: Item ,
657
- & << Self as Iterator > :: Item as IntoIterator >:: Item ) -> bool
633
+ F : FnMut ( & <Self :: Item as IntoIterator >:: Item ,
634
+ & <Self :: Item as IntoIterator >:: Item ) -> bool
658
635
{
659
636
kmerge_by ( self , first)
660
637
}
@@ -888,8 +865,6 @@ pub trait Itertools : Iterator {
888
865
/// Iterator element type is `Vec<Self::Item>`. The iterator produces a new Vec per iteration,
889
866
/// and clones the iterator elements.
890
867
///
891
- /// **Panics** if `n` is zero.
892
- ///
893
868
/// ```
894
869
/// use itertools::Itertools;
895
870
///
@@ -934,17 +909,18 @@ pub trait Itertools : Iterator {
934
909
935
910
/// Unravel a nested iterator.
936
911
///
937
- /// This is a shortcut for `it.flat_map(|x| x)`.
912
+ /// This is more or less equivalent to `.flat_map` with an identity
913
+ /// function.
938
914
///
939
915
/// ```
940
916
/// use itertools::Itertools;
941
917
///
942
918
/// let data = vec![vec![1, 2, 3], vec![4, 5, 6]];
943
- /// let flattened = data.into_iter ().flatten();
919
+ /// let flattened = data.iter ().flatten();
944
920
///
945
- /// itertools::assert_equal(flattened, vec! [1, 2, 3, 4, 5, 6]);
921
+ /// itertools::assert_equal(flattened, & [1, 2, 3, 4, 5, 6]);
946
922
/// ```
947
- fn flatten ( self ) -> Flatten < Self , << Self as Iterator > :: Item as IntoIterator >:: IntoIter >
923
+ fn flatten ( self ) -> Flatten < Self , <Self :: Item as IntoIterator >:: IntoIter >
948
924
where Self : Sized ,
949
925
Self :: Item : IntoIterator
950
926
{
@@ -1087,11 +1063,10 @@ pub trait Itertools : Iterator {
1087
1063
///
1088
1064
/// itertools::assert_equal(rx.iter(), vec![1, 3, 5, 7, 9]);
1089
1065
/// ```
1090
- fn foreach < F > ( & mut self , mut f : F )
1091
- where F : FnMut ( Self :: Item )
1066
+ fn foreach < F > ( self , mut f : F )
1067
+ where F : FnMut ( Self :: Item ) ,
1068
+ Self : Sized ,
1092
1069
{
1093
- // FIXME: This use of fold doesn't actually do any iterator
1094
- // specific traversal (fold requries `self`)
1095
1070
self . fold ( ( ) , move |( ) , element| f ( element) )
1096
1071
}
1097
1072
@@ -1184,14 +1159,6 @@ pub trait Itertools : Iterator {
1184
1159
format:: new_format_default ( self , sep)
1185
1160
}
1186
1161
1187
- ///
1188
- #[ deprecated( note = "renamed to .format()" ) ]
1189
- fn format_default ( self , sep : & str ) -> Format < Self >
1190
- where Self : Sized ,
1191
- {
1192
- format:: new_format_default ( self , sep)
1193
- }
1194
-
1195
1162
/// Format all iterator elements, separated by `sep`.
1196
1163
///
1197
1164
/// This is a customizable version of `.format()`.
@@ -1330,18 +1297,11 @@ pub trait Itertools : Iterator {
1330
1297
/// assert_eq!((0..10).fold1(|x, y| x + y).unwrap_or(0), 45);
1331
1298
/// assert_eq!((0..0).fold1(|x, y| x * y), None);
1332
1299
/// ```
1333
- fn fold1 < F > ( & mut self , mut f : F ) -> Option < Self :: Item >
1334
- where F : FnMut ( Self :: Item , Self :: Item ) -> Self :: Item
1300
+ fn fold1 < F > ( mut self , f : F ) -> Option < Self :: Item >
1301
+ where F : FnMut ( Self :: Item , Self :: Item ) -> Self :: Item ,
1302
+ Self : Sized ,
1335
1303
{
1336
- match self . next ( ) {
1337
- None => None ,
1338
- Some ( mut x) => {
1339
- for y in self {
1340
- x = f ( x, y) ;
1341
- }
1342
- Some ( x)
1343
- }
1344
- }
1304
+ self . next ( ) . map ( move |x| self . fold ( x, f) )
1345
1305
}
1346
1306
1347
1307
/// An iterator method that applies a function, producing a single, final value.
@@ -1373,7 +1333,7 @@ pub trait Itertools : Iterator {
1373
1333
/// // fold_while:
1374
1334
/// let result3 = numbers.iter().fold_while(0, |acc, x| {
1375
1335
/// if *x > 5 { Done(acc) } else { Continue(acc + x) }
1376
- /// });
1336
+ /// }).into_inner() ;
1377
1337
///
1378
1338
/// // they're the same
1379
1339
/// assert_eq!(result, result2);
@@ -1383,23 +1343,18 @@ pub trait Itertools : Iterator {
1383
1343
/// The big difference between the computations of `result2` and `result3` is that while
1384
1344
/// `fold()` called the provided closure for every item of the callee iterator,
1385
1345
/// `fold_while()` actually stopped iterating as soon as it encountered `Fold::Done(_)`.
1386
- fn fold_while < B , F > ( self , init : B , mut f : F ) -> B
1346
+ fn fold_while < B , F > ( & mut self , init : B , mut f : F ) -> FoldWhile < B >
1387
1347
where Self : Sized ,
1388
1348
F : FnMut ( B , Self :: Item ) -> FoldWhile < B >
1389
1349
{
1390
- let mut accum = init;
1391
- for item in self {
1392
- match f ( accum, item) {
1393
- FoldWhile :: Continue ( res) => {
1394
- accum = res;
1395
- }
1396
- FoldWhile :: Done ( res) => {
1397
- accum = res;
1398
- break ;
1399
- }
1350
+ let mut acc = init;
1351
+ while let Some ( item) = self . next ( ) {
1352
+ match f ( acc, item) {
1353
+ FoldWhile :: Continue ( res) => acc = res,
1354
+ res @ FoldWhile :: Done ( _) => return res,
1400
1355
}
1401
1356
}
1402
- accum
1357
+ FoldWhile :: Continue ( acc )
1403
1358
}
1404
1359
1405
1360
/// Collect all iterator elements into a sorted vector in ascending order.
@@ -1679,10 +1634,28 @@ pub fn partition<'a, A: 'a, I, F>(iter: I, mut pred: F) -> usize
1679
1634
/// An enum used for controlling the execution of `.fold_while()`.
1680
1635
///
1681
1636
/// See [`.fold_while()`](trait.Itertools.html#method.fold_while) for more information.
1637
+ #[ derive( Copy , Clone , Debug ) ]
1682
1638
pub enum FoldWhile < T > {
1683
1639
/// Continue folding with this value
1684
1640
Continue ( T ) ,
1685
1641
/// Fold is complete and will return this value
1686
1642
Done ( T ) ,
1687
1643
}
1688
1644
1645
+ impl < T > FoldWhile < T > {
1646
+ /// Return the value in the continue or done.
1647
+ pub fn into_inner ( self ) -> T {
1648
+ match self {
1649
+ FoldWhile :: Continue ( x) | FoldWhile :: Done ( x) => x,
1650
+ }
1651
+ }
1652
+
1653
+ /// Return true if `self` is `Done`, false if it is `Continue`.
1654
+ pub fn is_done ( & self ) -> bool {
1655
+ match * self {
1656
+ FoldWhile :: Continue ( _) => false ,
1657
+ FoldWhile :: Done ( _) => true ,
1658
+ }
1659
+ }
1660
+ }
1661
+
0 commit comments