@@ -570,6 +570,28 @@ where
570570 K : Eq ,
571571{
572572 /// Apply [`aggregate`](Self::aggregate) with a provided map.
573+ ///
574+ /// ```
575+ /// use std::collections::BTreeMap;
576+ /// use itertools::Itertools;
577+ ///
578+ /// let data = vec![2, 8, 5, 7, 9, 0, 4, 10];
579+ /// let lookup = data.into_iter()
580+ /// .into_grouping_map_by(|&n| n % 4)
581+ /// .aggregate_in(|acc, _key, val| {
582+ /// if val == 0 || val == 10 {
583+ /// None
584+ /// } else {
585+ /// Some(acc.unwrap_or(0) + val)
586+ /// }
587+ /// }, BTreeMap::new());
588+ ///
589+ /// assert_eq!(lookup[&0], 4); // 0 resets the accumulator so only 4 is summed
590+ /// assert_eq!(lookup[&1], 5 + 9);
591+ /// assert_eq!(lookup.get(&2), None); // 10 resets the accumulator and nothing is summed afterward
592+ /// assert_eq!(lookup[&3], 7);
593+ /// assert_eq!(lookup.len(), 3); // The final keys are only 0, 1 and 2
594+ /// ```
573595 pub fn aggregate_in < FO , R , M > ( self , mut operation : FO , mut map : M ) -> M
574596 where
575597 FO : FnMut ( Option < R > , & K , V ) -> Option < R > ,
@@ -586,6 +608,28 @@ where
586608 }
587609
588610 /// Apply [`fold_with`](Self::fold_with) with a provided map.
611+ ///
612+ /// ```
613+ /// use std::collections::BTreeMap;
614+ /// use itertools::Itertools;
615+ ///
616+ /// #[derive(Debug, Default)]
617+ /// struct Accumulator {
618+ /// acc: usize,
619+ /// }
620+ ///
621+ /// let lookup = (1..=7)
622+ /// .into_grouping_map_by(|&n| n % 3)
623+ /// .fold_with_in(|_key, _val| Default::default(), |Accumulator { acc }, _key, val| {
624+ /// let acc = acc + val;
625+ /// Accumulator { acc }
626+ /// }, BTreeMap::new());
627+ ///
628+ /// assert_eq!(lookup[&0].acc, 3 + 6);
629+ /// assert_eq!(lookup[&1].acc, 1 + 4 + 7);
630+ /// assert_eq!(lookup[&2].acc, 2 + 5);
631+ /// assert_eq!(lookup.len(), 3);
632+ /// ```
589633 pub fn fold_with_in < FI , FO , R , M > ( self , mut init : FI , mut operation : FO , map : M ) -> M
590634 where
591635 FI : FnMut ( & K , & V ) -> R ,
@@ -602,6 +646,20 @@ where
602646 }
603647
604648 /// Apply [`fold`](Self::fold) with a provided map.
649+ ///
650+ /// ```
651+ /// use std::collections::BTreeMap;
652+ /// use itertools::Itertools;
653+ ///
654+ /// let lookup = (1..=7)
655+ /// .into_grouping_map_by(|&n| n % 3)
656+ /// .fold_in(0, |acc, _key, val| acc + val, BTreeMap::new());
657+ ///
658+ /// assert_eq!(lookup[&0], 3 + 6);
659+ /// assert_eq!(lookup[&1], 1 + 4 + 7);
660+ /// assert_eq!(lookup[&2], 2 + 5);
661+ /// assert_eq!(lookup.len(), 3);
662+ /// ```
605663 pub fn fold_in < FO , R , M > ( self , init : R , operation : FO , map : M ) -> M
606664 where
607665 R : Clone ,
@@ -612,6 +670,20 @@ where
612670 }
613671
614672 /// Apply [`reduce`](Self::reduce) with a provided map.
673+ ///
674+ /// ```
675+ /// use std::collections::BTreeMap;
676+ /// use itertools::Itertools;
677+ ///
678+ /// let lookup = (1..=7)
679+ /// .into_grouping_map_by(|&n| n % 3)
680+ /// .reduce_in(|acc, _key, val| acc + val, BTreeMap::new());
681+ ///
682+ /// assert_eq!(lookup[&0], 3 + 6);
683+ /// assert_eq!(lookup[&1], 1 + 4 + 7);
684+ /// assert_eq!(lookup[&2], 2 + 5);
685+ /// assert_eq!(lookup.len(), 3);
686+ /// ```
615687 pub fn reduce_in < FO , M > ( self , mut operation : FO , map : M ) -> M
616688 where
617689 FO : FnMut ( V , & K , V ) -> V ,
@@ -629,6 +701,20 @@ where
629701 }
630702
631703 /// Apply [`collect`](Self::collect) with a provided map.
704+ ///
705+ /// ```
706+ /// use std::collections::{BTreeMap, BTreeSet};
707+ /// use itertools::Itertools;
708+ ///
709+ /// let lookup = vec![0, 1, 2, 3, 4, 5, 6, 2, 3, 6].into_iter()
710+ /// .into_grouping_map_by(|&n| n % 3)
711+ /// .collect_in::<BTreeSet<_>, _>(BTreeMap::new());
712+ ///
713+ /// assert_eq!(lookup[&0], vec![0, 3, 6].into_iter().collect::<BTreeSet<_>>());
714+ /// assert_eq!(lookup[&1], vec![1, 4].into_iter().collect::<BTreeSet<_>>());
715+ /// assert_eq!(lookup[&2], vec![2, 5].into_iter().collect::<BTreeSet<_>>());
716+ /// assert_eq!(lookup.len(), 3);
717+ /// ```
632718 pub fn collect_in < C , M > ( self , mut map : M ) -> M
633719 where
634720 C : Default + Extend < V > ,
@@ -642,6 +728,20 @@ where
642728 }
643729
644730 /// Apply [`max`](Self::max) with a provided map.
731+ ///
732+ /// ```
733+ /// use std::collections::BTreeMap;
734+ /// use itertools::Itertools;
735+ ///
736+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
737+ /// .into_grouping_map_by(|&n| n % 3)
738+ /// .max_in(BTreeMap::new());
739+ ///
740+ /// assert_eq!(lookup[&0], 12);
741+ /// assert_eq!(lookup[&1], 7);
742+ /// assert_eq!(lookup[&2], 8);
743+ /// assert_eq!(lookup.len(), 3);
744+ /// ```
645745 pub fn max_in < M > ( self , map : M ) -> M
646746 where
647747 V : Ord ,
@@ -651,6 +751,20 @@ where
651751 }
652752
653753 /// Apply [`max_by`](Self::max_by) with a provided map.
754+ ///
755+ /// ```
756+ /// use std::collections::BTreeMap;
757+ /// use itertools::Itertools;
758+ ///
759+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
760+ /// .into_grouping_map_by(|&n| n % 3)
761+ /// .max_by_in(|_key, x, y| y.cmp(x), BTreeMap::new());
762+ ///
763+ /// assert_eq!(lookup[&0], 3);
764+ /// assert_eq!(lookup[&1], 1);
765+ /// assert_eq!(lookup[&2], 5);
766+ /// assert_eq!(lookup.len(), 3);
767+ /// ```
654768 pub fn max_by_in < F , M > ( self , mut compare : F , map : M ) -> M
655769 where
656770 F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -666,6 +780,20 @@ where
666780 }
667781
668782 /// Apply [`max_by_key`](Self::max_by_key) with a provided map.
783+ ///
784+ /// ```
785+ /// use std::collections::BTreeMap;
786+ /// use itertools::Itertools;
787+ ///
788+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
789+ /// .into_grouping_map_by(|&n| n % 3)
790+ /// .max_by_key_in(|_key, &val| val % 4, BTreeMap::new());
791+ ///
792+ /// assert_eq!(lookup[&0], 3);
793+ /// assert_eq!(lookup[&1], 7);
794+ /// assert_eq!(lookup[&2], 5);
795+ /// assert_eq!(lookup.len(), 3);
796+ /// ```
669797 pub fn max_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
670798 where
671799 F : FnMut ( & K , & V ) -> CK ,
@@ -676,6 +804,20 @@ where
676804 }
677805
678806 /// Apply [`min`](Self::min) with a provided map.
807+ ///
808+ /// ```
809+ /// use std::collections::BTreeMap;
810+ /// use itertools::Itertools;
811+ ///
812+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
813+ /// .into_grouping_map_by(|&n| n % 3)
814+ /// .min_in(BTreeMap::new());
815+ ///
816+ /// assert_eq!(lookup[&0], 3);
817+ /// assert_eq!(lookup[&1], 1);
818+ /// assert_eq!(lookup[&2], 5);
819+ /// assert_eq!(lookup.len(), 3);
820+ /// ```
679821 pub fn min_in < M > ( self , map : M ) -> M
680822 where
681823 V : Ord ,
@@ -685,6 +827,20 @@ where
685827 }
686828
687829 /// Apply [`min_by`](Self::min_by) with a provided map.
830+ ///
831+ /// ```
832+ /// use std::collections::BTreeMap;
833+ /// use itertools::Itertools;
834+ ///
835+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
836+ /// .into_grouping_map_by(|&n| n % 3)
837+ /// .min_by_in(|_key, x, y| y.cmp(x), BTreeMap::new());
838+ ///
839+ /// assert_eq!(lookup[&0], 12);
840+ /// assert_eq!(lookup[&1], 7);
841+ /// assert_eq!(lookup[&2], 8);
842+ /// assert_eq!(lookup.len(), 3);
843+ /// ```
688844 pub fn min_by_in < F , M > ( self , mut compare : F , map : M ) -> M
689845 where
690846 F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -700,6 +856,20 @@ where
700856 }
701857
702858 /// Apply [`min_by_key`](Self::min_by_key) with a provided map.
859+ ///
860+ /// ```
861+ /// use std::collections::BTreeMap;
862+ /// use itertools::Itertools;
863+ ///
864+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
865+ /// .into_grouping_map_by(|&n| n % 3)
866+ /// .min_by_key_in(|_key, &val| val % 4, BTreeMap::new());
867+ ///
868+ /// assert_eq!(lookup[&0], 12);
869+ /// assert_eq!(lookup[&1], 4);
870+ /// assert_eq!(lookup[&2], 8);
871+ /// assert_eq!(lookup.len(), 3);
872+ /// ```
703873 pub fn min_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
704874 where
705875 F : FnMut ( & K , & V ) -> CK ,
@@ -710,6 +880,21 @@ where
710880 }
711881
712882 /// Apply [`minmax`](Self::minmax) with a provided map.
883+ ///
884+ /// ```
885+ /// use std::collections::BTreeMap;
886+ /// use itertools::Itertools;
887+ /// use itertools::MinMaxResult::{OneElement, MinMax};
888+ ///
889+ /// let lookup = vec![1, 3, 4, 5, 7, 9, 12].into_iter()
890+ /// .into_grouping_map_by(|&n| n % 3)
891+ /// .minmax_in(BTreeMap::new());
892+ ///
893+ /// assert_eq!(lookup[&0], MinMax(3, 12));
894+ /// assert_eq!(lookup[&1], MinMax(1, 7));
895+ /// assert_eq!(lookup[&2], OneElement(5));
896+ /// assert_eq!(lookup.len(), 3);
897+ /// ```
713898 pub fn minmax_in < M > ( self , map : M ) -> M
714899 where
715900 V : Ord ,
@@ -719,6 +904,21 @@ where
719904 }
720905
721906 /// Apply [`minmax_by`](Self::minmax_by) with a provided map.
907+ ///
908+ /// ```
909+ /// use std::collections::BTreeMap;
910+ /// use itertools::Itertools;
911+ /// use itertools::MinMaxResult::{OneElement, MinMax};
912+ ///
913+ /// let lookup = vec![1, 3, 4, 5, 7, 9, 12].into_iter()
914+ /// .into_grouping_map_by(|&n| n % 3)
915+ /// .minmax_by_in(|_key, x, y| y.cmp(x), BTreeMap::new());
916+ ///
917+ /// assert_eq!(lookup[&0], MinMax(12, 3));
918+ /// assert_eq!(lookup[&1], MinMax(7, 1));
919+ /// assert_eq!(lookup[&2], OneElement(5));
920+ /// assert_eq!(lookup.len(), 3);
921+ /// ```
722922 pub fn minmax_by_in < F , M > ( self , mut compare : F , map : M ) -> M
723923 where
724924 F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -752,6 +952,21 @@ where
752952 }
753953
754954 /// Apply [`minmax_by_key`](Self::minmax_by_key) with a provided map.
955+ ///
956+ /// ```
957+ /// use std::collections::BTreeMap;
958+ /// use itertools::Itertools;
959+ /// use itertools::MinMaxResult::{OneElement, MinMax};
960+ ///
961+ /// let lookup = vec![1, 3, 4, 5, 7, 9, 12].into_iter()
962+ /// .into_grouping_map_by(|&n| n % 3)
963+ /// .minmax_by_key_in(|_key, &val| val % 4, BTreeMap::new());
964+ ///
965+ /// assert_eq!(lookup[&0], MinMax(12, 3));
966+ /// assert_eq!(lookup[&1], MinMax(4, 7));
967+ /// assert_eq!(lookup[&2], OneElement(5));
968+ /// assert_eq!(lookup.len(), 3);
969+ /// ```
755970 pub fn minmax_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
756971 where
757972 F : FnMut ( & K , & V ) -> CK ,
@@ -762,6 +977,20 @@ where
762977 }
763978
764979 /// Apply [`sum`](Self::sum) with a provided map.
980+ ///
981+ /// ```
982+ /// use std::collections::BTreeMap;
983+ /// use itertools::Itertools;
984+ ///
985+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
986+ /// .into_grouping_map_by(|&n| n % 3)
987+ /// .sum_in(BTreeMap::new());
988+ ///
989+ /// assert_eq!(lookup[&0], 3 + 9 + 12);
990+ /// assert_eq!(lookup[&1], 1 + 4 + 7);
991+ /// assert_eq!(lookup[&2], 5 + 8);
992+ /// assert_eq!(lookup.len(), 3);
993+ /// ```
765994 pub fn sum_in < M > ( self , map : M ) -> M
766995 where
767996 V : Add < V , Output = V > ,
@@ -771,6 +1000,20 @@ where
7711000 }
7721001
7731002 /// Apply [`product`](Self::product) with a provided map.
1003+ ///
1004+ /// ```
1005+ /// use std::collections::BTreeMap;
1006+ /// use itertools::Itertools;
1007+ ///
1008+ /// let lookup = vec![1, 3, 4, 5, 7, 8, 9, 12].into_iter()
1009+ /// .into_grouping_map_by(|&n| n % 3)
1010+ /// .product_in(BTreeMap::new());
1011+ ///
1012+ /// assert_eq!(lookup[&0], 3 * 9 * 12);
1013+ /// assert_eq!(lookup[&1], 1 * 4 * 7);
1014+ /// assert_eq!(lookup[&2], 5 * 8);
1015+ /// assert_eq!(lookup.len(), 3);
1016+ /// ```
7741017 pub fn product_in < M > ( self , map : M ) -> M
7751018 where
7761019 V : Mul < V , Output = V > ,
0 commit comments