@@ -569,6 +569,28 @@ where
569
569
{
570
570
/// Apply [`aggregate`](Self::aggregate) with a provided empty map
571
571
/// (`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
+ /// ```
572
594
pub fn aggregate_in < FO , R , M > ( self , mut operation : FO , mut map : M ) -> M
573
595
where
574
596
FO : FnMut ( Option < R > , & K , V ) -> Option < R > ,
@@ -588,6 +610,28 @@ where
588
610
589
611
/// Apply [`fold_with`](Self::fold_with) with a provided empty map
590
612
/// (`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
+ /// ```
591
635
pub fn fold_with_in < FI , FO , R , M > ( self , mut init : FI , mut operation : FO , map : M ) -> M
592
636
where
593
637
FI : FnMut ( & K , & V ) -> R ,
@@ -605,6 +649,20 @@ where
605
649
606
650
/// Apply [`fold`](Self::fold) with a provided empty map
607
651
/// (`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
+ /// ```
608
666
pub fn fold_in < FO , R , M > ( self , init : R , operation : FO , map : M ) -> M
609
667
where
610
668
R : Clone ,
@@ -616,6 +674,20 @@ where
616
674
617
675
/// Apply [`reduce`](Self::reduce) with a provided empty map
618
676
/// (`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
+ /// ```
619
691
pub fn reduce_in < FO , M > ( self , mut operation : FO , map : M ) -> M
620
692
where
621
693
FO : FnMut ( V , & K , V ) -> V ,
@@ -634,6 +706,20 @@ where
634
706
635
707
/// Apply [`collect`](Self::collect) with a provided empty map
636
708
/// (`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
+ /// ```
637
723
pub fn collect_in < C , M > ( self , mut map : M ) -> M
638
724
where
639
725
C : Default + Extend < V > ,
@@ -650,6 +736,20 @@ where
650
736
651
737
/// Apply [`max`](Self::max) with a provided empty map
652
738
/// (`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
+ /// ```
653
753
pub fn max_in < M > ( self , map : M ) -> M
654
754
where
655
755
V : Ord ,
@@ -660,6 +760,20 @@ where
660
760
661
761
/// Apply [`max_by`](Self::max_by) with a provided empty map
662
762
/// (`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
+ /// ```
663
777
pub fn max_by_in < F , M > ( self , mut compare : F , map : M ) -> M
664
778
where
665
779
F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -676,6 +790,20 @@ where
676
790
677
791
/// Apply [`max_by_key`](Self::max_by_key) with a provided empty map
678
792
/// (`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
+ /// ```
679
807
pub fn max_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
680
808
where
681
809
F : FnMut ( & K , & V ) -> CK ,
@@ -687,6 +815,20 @@ where
687
815
688
816
/// Apply [`min`](Self::min) with a provided empty map
689
817
/// (`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
+ /// ```
690
832
pub fn min_in < M > ( self , map : M ) -> M
691
833
where
692
834
V : Ord ,
@@ -697,6 +839,20 @@ where
697
839
698
840
/// Apply [`min_by`](Self::min_by) with a provided empty map
699
841
/// (`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
+ /// ```
700
856
pub fn min_by_in < F , M > ( self , mut compare : F , map : M ) -> M
701
857
where
702
858
F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -713,6 +869,20 @@ where
713
869
714
870
/// Apply [`min_by_key`](Self::min_by_key) with a provided empty map
715
871
/// (`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
+ /// ```
716
886
pub fn min_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
717
887
where
718
888
F : FnMut ( & K , & V ) -> CK ,
@@ -724,6 +894,21 @@ where
724
894
725
895
/// Apply [`minmax`](Self::minmax) with a provided empty map
726
896
/// (`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
+ /// ```
727
912
pub fn minmax_in < M > ( self , map : M ) -> M
728
913
where
729
914
V : Ord ,
@@ -734,6 +919,21 @@ where
734
919
735
920
/// Apply [`minmax_by`](Self::minmax_by) with a provided empty map
736
921
/// (`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
+ /// ```
737
937
pub fn minmax_by_in < F , M > ( self , mut compare : F , map : M ) -> M
738
938
where
739
939
F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -768,6 +968,21 @@ where
768
968
769
969
/// Apply [`minmax_by_key`](Self::minmax_by_key) with a provided empty map
770
970
/// (`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
+ /// ```
771
986
pub fn minmax_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
772
987
where
773
988
F : FnMut ( & K , & V ) -> CK ,
@@ -779,6 +994,20 @@ where
779
994
780
995
/// Apply [`sum`](Self::sum) with a provided empty map
781
996
/// (`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
+ /// ```
782
1011
pub fn sum_in < M > ( self , map : M ) -> M
783
1012
where
784
1013
V : Add < V , Output = V > ,
@@ -789,6 +1018,20 @@ where
789
1018
790
1019
/// Apply [`product`](Self::product) with a provided empty map
791
1020
/// (`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
+ /// ```
792
1035
pub fn product_in < M > ( self , map : M ) -> M
793
1036
where
794
1037
V : Mul < V , Output = V > ,
0 commit comments