Skip to content

Commit f0fcf7a

Browse files
GroupingMap: add doctests using BTreeMap
Copied from their non-suffixed variants and minimally updated.
1 parent 2208850 commit f0fcf7a

File tree

1 file changed

+243
-0
lines changed

1 file changed

+243
-0
lines changed

src/grouping_map.rs

+243
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,28 @@ where
569569
{
570570
/// Apply [`aggregate`](Self::aggregate) with a provided empty map
571571
/// (`BTreeMap` or `HashMap` with any hasher).
572+
///
573+
/// ```
574+
/// use std::collections::BTreeMap;
575+
/// use itertools::Itertools;
576+
///
577+
/// let data = vec![2, 8, 5, 7, 9, 0, 4, 10];
578+
/// let lookup = data.into_iter()
579+
/// .into_grouping_map_by(|&n| n % 4)
580+
/// .aggregate_in(|acc, _key, val| {
581+
/// if val == 0 || val == 10 {
582+
/// None
583+
/// } else {
584+
/// Some(acc.unwrap_or(0) + val)
585+
/// }
586+
/// }, BTreeMap::new());
587+
///
588+
/// assert_eq!(lookup[&0], 4); // 0 resets the accumulator so only 4 is summed
589+
/// assert_eq!(lookup[&1], 5 + 9);
590+
/// assert_eq!(lookup.get(&2), None); // 10 resets the accumulator and nothing is summed afterward
591+
/// assert_eq!(lookup[&3], 7);
592+
/// assert_eq!(lookup.len(), 3); // The final keys are only 0, 1 and 2
593+
/// ```
572594
pub fn aggregate_in<FO, R, M>(self, mut operation: FO, mut map: M) -> M
573595
where
574596
FO: FnMut(Option<R>, &K, V) -> Option<R>,
@@ -588,6 +610,28 @@ where
588610

589611
/// Apply [`fold_with`](Self::fold_with) with a provided empty map
590612
/// (`BTreeMap` or `HashMap` with any hasher).
613+
///
614+
/// ```
615+
/// use std::collections::BTreeMap;
616+
/// use itertools::Itertools;
617+
///
618+
/// #[derive(Debug, Default)]
619+
/// struct Accumulator {
620+
/// acc: usize,
621+
/// }
622+
///
623+
/// let lookup = (1..=7)
624+
/// .into_grouping_map_by(|&n| n % 3)
625+
/// .fold_with_in(|_key, _val| Default::default(), |Accumulator { acc }, _key, val| {
626+
/// let acc = acc + val;
627+
/// Accumulator { acc }
628+
/// }, BTreeMap::new());
629+
///
630+
/// assert_eq!(lookup[&0].acc, 3 + 6);
631+
/// assert_eq!(lookup[&1].acc, 1 + 4 + 7);
632+
/// assert_eq!(lookup[&2].acc, 2 + 5);
633+
/// assert_eq!(lookup.len(), 3);
634+
/// ```
591635
pub fn fold_with_in<FI, FO, R, M>(self, mut init: FI, mut operation: FO, map: M) -> M
592636
where
593637
FI: FnMut(&K, &V) -> R,
@@ -605,6 +649,20 @@ where
605649

606650
/// Apply [`fold`](Self::fold) with a provided empty map
607651
/// (`BTreeMap` or `HashMap` with any hasher).
652+
///
653+
/// ```
654+
/// use std::collections::BTreeMap;
655+
/// use itertools::Itertools;
656+
///
657+
/// let lookup = (1..=7)
658+
/// .into_grouping_map_by(|&n| n % 3)
659+
/// .fold_in(0, |acc, _key, val| acc + val, BTreeMap::new());
660+
///
661+
/// assert_eq!(lookup[&0], 3 + 6);
662+
/// assert_eq!(lookup[&1], 1 + 4 + 7);
663+
/// assert_eq!(lookup[&2], 2 + 5);
664+
/// assert_eq!(lookup.len(), 3);
665+
/// ```
608666
pub fn fold_in<FO, R, M>(self, init: R, operation: FO, map: M) -> M
609667
where
610668
R: Clone,
@@ -616,6 +674,20 @@ where
616674

617675
/// Apply [`reduce`](Self::reduce) with a provided empty map
618676
/// (`BTreeMap` or `HashMap` with any hasher).
677+
///
678+
/// ```
679+
/// use std::collections::BTreeMap;
680+
/// use itertools::Itertools;
681+
///
682+
/// let lookup = (1..=7)
683+
/// .into_grouping_map_by(|&n| n % 3)
684+
/// .reduce_in(|acc, _key, val| acc + val, BTreeMap::new());
685+
///
686+
/// assert_eq!(lookup[&0], 3 + 6);
687+
/// assert_eq!(lookup[&1], 1 + 4 + 7);
688+
/// assert_eq!(lookup[&2], 2 + 5);
689+
/// assert_eq!(lookup.len(), 3);
690+
/// ```
619691
pub fn reduce_in<FO, M>(self, mut operation: FO, map: M) -> M
620692
where
621693
FO: FnMut(V, &K, V) -> V,
@@ -634,6 +706,20 @@ where
634706

635707
/// Apply [`collect`](Self::collect) with a provided empty map
636708
/// (`BTreeMap` or `HashMap` with any hasher).
709+
///
710+
/// ```
711+
/// use std::collections::{BTreeMap, BTreeSet};
712+
/// use itertools::Itertools;
713+
///
714+
/// let lookup = vec![0, 1, 2, 3, 4, 5, 6, 2, 3, 6].into_iter()
715+
/// .into_grouping_map_by(|&n| n % 3)
716+
/// .collect_in::<BTreeSet<_>, _>(BTreeMap::new());
717+
///
718+
/// assert_eq!(lookup[&0], vec![0, 3, 6].into_iter().collect::<BTreeSet<_>>());
719+
/// assert_eq!(lookup[&1], vec![1, 4].into_iter().collect::<BTreeSet<_>>());
720+
/// assert_eq!(lookup[&2], vec![2, 5].into_iter().collect::<BTreeSet<_>>());
721+
/// assert_eq!(lookup.len(), 3);
722+
/// ```
637723
pub fn collect_in<C, M>(self, mut map: M) -> M
638724
where
639725
C: Default + Extend<V>,
@@ -650,6 +736,20 @@ where
650736

651737
/// Apply [`max`](Self::max) with a provided empty map
652738
/// (`BTreeMap` or `HashMap` with any hasher).
739+
///
740+
/// ```
741+
/// use std::collections::BTreeMap;
742+
/// use itertools::Itertools;
743+
///
744+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
745+
/// .into_grouping_map_by(|&n| n % 3)
746+
/// .max_in(BTreeMap::new());
747+
///
748+
/// assert_eq!(lookup[&0], 12);
749+
/// assert_eq!(lookup[&1], 7);
750+
/// assert_eq!(lookup[&2], 8);
751+
/// assert_eq!(lookup.len(), 3);
752+
/// ```
653753
pub fn max_in<M>(self, map: M) -> M
654754
where
655755
V: Ord,
@@ -660,6 +760,20 @@ where
660760

661761
/// Apply [`max_by`](Self::max_by) with a provided empty map
662762
/// (`BTreeMap` or `HashMap` with any hasher).
763+
///
764+
/// ```
765+
/// use std::collections::BTreeMap;
766+
/// use itertools::Itertools;
767+
///
768+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
769+
/// .into_grouping_map_by(|&n| n % 3)
770+
/// .max_by_in(|_key, x, y| y.cmp(x), BTreeMap::new());
771+
///
772+
/// assert_eq!(lookup[&0], 3);
773+
/// assert_eq!(lookup[&1], 1);
774+
/// assert_eq!(lookup[&2], 5);
775+
/// assert_eq!(lookup.len(), 3);
776+
/// ```
663777
pub fn max_by_in<F, M>(self, mut compare: F, map: M) -> M
664778
where
665779
F: FnMut(&K, &V, &V) -> Ordering,
@@ -676,6 +790,20 @@ where
676790

677791
/// Apply [`max_by_key`](Self::max_by_key) with a provided empty map
678792
/// (`BTreeMap` or `HashMap` with any hasher).
793+
///
794+
/// ```
795+
/// use std::collections::BTreeMap;
796+
/// use itertools::Itertools;
797+
///
798+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
799+
/// .into_grouping_map_by(|&n| n % 3)
800+
/// .max_by_key_in(|_key, &val| val % 4, BTreeMap::new());
801+
///
802+
/// assert_eq!(lookup[&0], 3);
803+
/// assert_eq!(lookup[&1], 7);
804+
/// assert_eq!(lookup[&2], 5);
805+
/// assert_eq!(lookup.len(), 3);
806+
/// ```
679807
pub fn max_by_key_in<F, CK, M>(self, mut f: F, map: M) -> M
680808
where
681809
F: FnMut(&K, &V) -> CK,
@@ -687,6 +815,20 @@ where
687815

688816
/// Apply [`min`](Self::min) with a provided empty map
689817
/// (`BTreeMap` or `HashMap` with any hasher).
818+
///
819+
/// ```
820+
/// use std::collections::BTreeMap;
821+
/// use itertools::Itertools;
822+
///
823+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
824+
/// .into_grouping_map_by(|&n| n % 3)
825+
/// .min_in(BTreeMap::new());
826+
///
827+
/// assert_eq!(lookup[&0], 3);
828+
/// assert_eq!(lookup[&1], 1);
829+
/// assert_eq!(lookup[&2], 5);
830+
/// assert_eq!(lookup.len(), 3);
831+
/// ```
690832
pub fn min_in<M>(self, map: M) -> M
691833
where
692834
V: Ord,
@@ -697,6 +839,20 @@ where
697839

698840
/// Apply [`min_by`](Self::min_by) with a provided empty map
699841
/// (`BTreeMap` or `HashMap` with any hasher).
842+
///
843+
/// ```
844+
/// use std::collections::BTreeMap;
845+
/// use itertools::Itertools;
846+
///
847+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
848+
/// .into_grouping_map_by(|&n| n % 3)
849+
/// .min_by_in(|_key, x, y| y.cmp(x), BTreeMap::new());
850+
///
851+
/// assert_eq!(lookup[&0], 12);
852+
/// assert_eq!(lookup[&1], 7);
853+
/// assert_eq!(lookup[&2], 8);
854+
/// assert_eq!(lookup.len(), 3);
855+
/// ```
700856
pub fn min_by_in<F, M>(self, mut compare: F, map: M) -> M
701857
where
702858
F: FnMut(&K, &V, &V) -> Ordering,
@@ -713,6 +869,20 @@ where
713869

714870
/// Apply [`min_by_key`](Self::min_by_key) with a provided empty map
715871
/// (`BTreeMap` or `HashMap` with any hasher).
872+
///
873+
/// ```
874+
/// use std::collections::BTreeMap;
875+
/// use itertools::Itertools;
876+
///
877+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
878+
/// .into_grouping_map_by(|&n| n % 3)
879+
/// .min_by_key_in(|_key, &val| val % 4, BTreeMap::new());
880+
///
881+
/// assert_eq!(lookup[&0], 12);
882+
/// assert_eq!(lookup[&1], 4);
883+
/// assert_eq!(lookup[&2], 8);
884+
/// assert_eq!(lookup.len(), 3);
885+
/// ```
716886
pub fn min_by_key_in<F, CK, M>(self, mut f: F, map: M) -> M
717887
where
718888
F: FnMut(&K, &V) -> CK,
@@ -724,6 +894,21 @@ where
724894

725895
/// Apply [`minmax`](Self::minmax) with a provided empty map
726896
/// (`BTreeMap` or `HashMap` with any hasher).
897+
///
898+
/// ```
899+
/// use std::collections::BTreeMap;
900+
/// use itertools::Itertools;
901+
/// use itertools::MinMaxResult::{OneElement, MinMax};
902+
///
903+
/// let lookup = vec![1, 3, 4, 5, 7, 9, 12].into_iter()
904+
/// .into_grouping_map_by(|&n| n % 3)
905+
/// .minmax_in(BTreeMap::new());
906+
///
907+
/// assert_eq!(lookup[&0], MinMax(3, 12));
908+
/// assert_eq!(lookup[&1], MinMax(1, 7));
909+
/// assert_eq!(lookup[&2], OneElement(5));
910+
/// assert_eq!(lookup.len(), 3);
911+
/// ```
727912
pub fn minmax_in<M>(self, map: M) -> M
728913
where
729914
V: Ord,
@@ -734,6 +919,21 @@ where
734919

735920
/// Apply [`minmax_by`](Self::minmax_by) with a provided empty map
736921
/// (`BTreeMap` or `HashMap` with any hasher).
922+
///
923+
/// ```
924+
/// use std::collections::BTreeMap;
925+
/// use itertools::Itertools;
926+
/// use itertools::MinMaxResult::{OneElement, MinMax};
927+
///
928+
/// let lookup = vec![1, 3, 4, 5, 7, 9, 12].into_iter()
929+
/// .into_grouping_map_by(|&n| n % 3)
930+
/// .minmax_by_in(|_key, x, y| y.cmp(x), BTreeMap::new());
931+
///
932+
/// assert_eq!(lookup[&0], MinMax(12, 3));
933+
/// assert_eq!(lookup[&1], MinMax(7, 1));
934+
/// assert_eq!(lookup[&2], OneElement(5));
935+
/// assert_eq!(lookup.len(), 3);
936+
/// ```
737937
pub fn minmax_by_in<F, M>(self, mut compare: F, map: M) -> M
738938
where
739939
F: FnMut(&K, &V, &V) -> Ordering,
@@ -768,6 +968,21 @@ where
768968

769969
/// Apply [`minmax_by_key`](Self::minmax_by_key) with a provided empty map
770970
/// (`BTreeMap` or `HashMap` with any hasher).
971+
///
972+
/// ```
973+
/// use std::collections::BTreeMap;
974+
/// use itertools::Itertools;
975+
/// use itertools::MinMaxResult::{OneElement, MinMax};
976+
///
977+
/// let lookup = vec![1, 3, 4, 5, 7, 9, 12].into_iter()
978+
/// .into_grouping_map_by(|&n| n % 3)
979+
/// .minmax_by_key_in(|_key, &val| val % 4, BTreeMap::new());
980+
///
981+
/// assert_eq!(lookup[&0], MinMax(12, 3));
982+
/// assert_eq!(lookup[&1], MinMax(4, 7));
983+
/// assert_eq!(lookup[&2], OneElement(5));
984+
/// assert_eq!(lookup.len(), 3);
985+
/// ```
771986
pub fn minmax_by_key_in<F, CK, M>(self, mut f: F, map: M) -> M
772987
where
773988
F: FnMut(&K, &V) -> CK,
@@ -779,6 +994,20 @@ where
779994

780995
/// Apply [`sum`](Self::sum) with a provided empty map
781996
/// (`BTreeMap` or `HashMap` with any hasher).
997+
///
998+
/// ```
999+
/// use std::collections::BTreeMap;
1000+
/// use itertools::Itertools;
1001+
///
1002+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
1003+
/// .into_grouping_map_by(|&n| n % 3)
1004+
/// .sum_in(BTreeMap::new());
1005+
///
1006+
/// assert_eq!(lookup[&0], 3 + 9 + 12);
1007+
/// assert_eq!(lookup[&1], 1 + 4 + 7);
1008+
/// assert_eq!(lookup[&2], 5 + 8);
1009+
/// assert_eq!(lookup.len(), 3);
1010+
/// ```
7821011
pub fn sum_in<M>(self, map: M) -> M
7831012
where
7841013
V: Add<V, Output = V>,
@@ -789,6 +1018,20 @@ where
7891018

7901019
/// Apply [`product`](Self::product) with a provided empty map
7911020
/// (`BTreeMap` or `HashMap` with any hasher).
1021+
///
1022+
/// ```
1023+
/// use std::collections::BTreeMap;
1024+
/// use itertools::Itertools;
1025+
///
1026+
/// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
1027+
/// .into_grouping_map_by(|&n| n % 3)
1028+
/// .product_in(BTreeMap::new());
1029+
///
1030+
/// assert_eq!(lookup[&0], 3 * 9 * 12);
1031+
/// assert_eq!(lookup[&1], 1 * 4 * 7);
1032+
/// assert_eq!(lookup[&2], 5 * 8);
1033+
/// assert_eq!(lookup.len(), 3);
1034+
/// ```
7921035
pub fn product_in<M>(self, map: M) -> M
7931036
where
7941037
V: Mul<V, Output = V>,

0 commit comments

Comments
 (0)