@@ -570,6 +570,28 @@ where
570
570
K : Eq ,
571
571
{
572
572
/// 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
+ /// ```
573
595
pub fn aggregate_in < FO , R , M > ( self , mut operation : FO , mut map : M ) -> M
574
596
where
575
597
FO : FnMut ( Option < R > , & K , V ) -> Option < R > ,
@@ -586,6 +608,28 @@ where
586
608
}
587
609
588
610
/// 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
+ /// ```
589
633
pub fn fold_with_in < FI , FO , R , M > ( self , mut init : FI , mut operation : FO , map : M ) -> M
590
634
where
591
635
FI : FnMut ( & K , & V ) -> R ,
@@ -602,6 +646,20 @@ where
602
646
}
603
647
604
648
/// 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
+ /// ```
605
663
pub fn fold_in < FO , R , M > ( self , init : R , operation : FO , map : M ) -> M
606
664
where
607
665
R : Clone ,
@@ -612,6 +670,20 @@ where
612
670
}
613
671
614
672
/// 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
+ /// ```
615
687
pub fn reduce_in < FO , M > ( self , mut operation : FO , map : M ) -> M
616
688
where
617
689
FO : FnMut ( V , & K , V ) -> V ,
@@ -629,6 +701,20 @@ where
629
701
}
630
702
631
703
/// 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
+ /// ```
632
718
pub fn collect_in < C , M > ( self , mut map : M ) -> M
633
719
where
634
720
C : Default + Extend < V > ,
@@ -642,6 +728,20 @@ where
642
728
}
643
729
644
730
/// 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
+ /// ```
645
745
pub fn max_in < M > ( self , map : M ) -> M
646
746
where
647
747
V : Ord ,
@@ -651,6 +751,20 @@ where
651
751
}
652
752
653
753
/// 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
+ /// ```
654
768
pub fn max_by_in < F , M > ( self , mut compare : F , map : M ) -> M
655
769
where
656
770
F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -666,6 +780,20 @@ where
666
780
}
667
781
668
782
/// 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
+ /// ```
669
797
pub fn max_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
670
798
where
671
799
F : FnMut ( & K , & V ) -> CK ,
@@ -676,6 +804,20 @@ where
676
804
}
677
805
678
806
/// 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
+ /// ```
679
821
pub fn min_in < M > ( self , map : M ) -> M
680
822
where
681
823
V : Ord ,
@@ -685,6 +827,20 @@ where
685
827
}
686
828
687
829
/// 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
+ /// ```
688
844
pub fn min_by_in < F , M > ( self , mut compare : F , map : M ) -> M
689
845
where
690
846
F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -700,6 +856,20 @@ where
700
856
}
701
857
702
858
/// 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
+ /// ```
703
873
pub fn min_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
704
874
where
705
875
F : FnMut ( & K , & V ) -> CK ,
@@ -710,6 +880,21 @@ where
710
880
}
711
881
712
882
/// 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
+ /// ```
713
898
pub fn minmax_in < M > ( self , map : M ) -> M
714
899
where
715
900
V : Ord ,
@@ -719,6 +904,21 @@ where
719
904
}
720
905
721
906
/// 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
+ /// ```
722
922
pub fn minmax_by_in < F , M > ( self , mut compare : F , map : M ) -> M
723
923
where
724
924
F : FnMut ( & K , & V , & V ) -> Ordering ,
@@ -752,6 +952,21 @@ where
752
952
}
753
953
754
954
/// 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
+ /// ```
755
970
pub fn minmax_by_key_in < F , CK , M > ( self , mut f : F , map : M ) -> M
756
971
where
757
972
F : FnMut ( & K , & V ) -> CK ,
@@ -762,6 +977,20 @@ where
762
977
}
763
978
764
979
/// 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
+ /// ```
765
994
pub fn sum_in < M > ( self , map : M ) -> M
766
995
where
767
996
V : Add < V , Output = V > ,
@@ -771,6 +1000,20 @@ where
771
1000
}
772
1001
773
1002
/// 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
+ /// ```
774
1017
pub fn product_in < M > ( self , map : M ) -> M
775
1018
where
776
1019
V : Mul < V , Output = V > ,
0 commit comments