@@ -615,6 +615,18 @@ export function* reverse<T>(source: Iterable<T>): Iterable<T> {
615
615
}
616
616
}
617
617
618
+ /**
619
+ * Returns the sum of the values in the collection.
620
+ * @param source The input collection.
621
+ */
622
+ export function sum ( source : Iterable < number > ) : number {
623
+ let sum = 0
624
+ for ( const item of source ) {
625
+ sum += item
626
+ }
627
+ return sum
628
+ }
629
+
618
630
/**
619
631
* Returns the sum of the values returned by the selector for each element in the collection.
620
632
* @param selector A function to transform each element into a summable value.
@@ -640,6 +652,24 @@ export function sumBy<T>(a: any, b?: any): any {
640
652
return partial ? exec : exec ( a )
641
653
}
642
654
655
+ /**
656
+ * Returns the maximum of the values in the collection.
657
+ * @param source The input collection.
658
+ * @throws If the collection is empty.
659
+ */
660
+ export function max ( source : Iterable < number > ) : number {
661
+ let max : number | null = null
662
+ for ( const item of source ) {
663
+ if ( max === null || item > max ) {
664
+ max = item
665
+ }
666
+ }
667
+ if ( max === null ) {
668
+ throw new Error ( `Can't find max of an empty collection` )
669
+ }
670
+ return max
671
+ }
672
+
643
673
/**
644
674
* Returns the maximum of the values returned by the selector for each element in the collection.
645
675
* @param selector A function to transform each element into a comparable value.
@@ -673,6 +703,24 @@ export function maxBy<T>(a: any, b?: any): any {
673
703
return partial ? exec : exec ( a )
674
704
}
675
705
706
+ /**
707
+ * Returns the minimum of the values in the collection.
708
+ * @param source The input collection.
709
+ * @throws If the collection is empty.
710
+ */
711
+ export function min ( source : Iterable < number > ) : number {
712
+ let min : number | null = null
713
+ for ( const item of source ) {
714
+ if ( min === null || item < min ) {
715
+ min = item
716
+ }
717
+ }
718
+ if ( min === null ) {
719
+ throw new Error ( `Can't find min of an empty collection` )
720
+ }
721
+ return min
722
+ }
723
+
676
724
/**
677
725
* Returns the minimum of the values returned by the selector for each element in the collection.
678
726
* @param selector A function to transform each element into a comparable value.
@@ -706,6 +754,24 @@ export function minBy<T>(a: any, b?: any): any {
706
754
return partial ? exec : exec ( a )
707
755
}
708
756
757
+ /**
758
+ * Returns the mean (average) of the values in the collection.
759
+ * @param source The input collection.
760
+ * @throws If the collection is empty.
761
+ */
762
+ export function mean ( source : Iterable < number > ) : number {
763
+ let sum = 0
764
+ let count = 0
765
+ for ( const item of source ) {
766
+ sum += item
767
+ count ++
768
+ }
769
+ if ( count === 0 ) {
770
+ throw new Error ( `Can't find mean of an empty collection` )
771
+ }
772
+ return sum / count
773
+ }
774
+
709
775
/**
710
776
* Returns the mean (average) of the values returned by the selector for each element in the collection.
711
777
* @param selector A function to transform each element into a summable value.
0 commit comments