3 * Copyright (c) 2004-present, Facebook, Inc.
6 * This source code is licensed under the MIT license found in the
7 * LICENSE file in the root directory of this source tree.
11 namespace HH\Lib\Math
;
12 use namespace HH\Lib\
{C
, Math
, Vec
};
15 * Returns the largest element of the given Traversable, or null if the
16 * Traversable is empty.
18 * - For a known number of inputs, see `Math\maxva()`.
19 * - To find the smallest number, see `Math\min()`.
21 function max
<T
as num
>(
22 Traversable
<T
> $numbers,
25 foreach ($numbers as $number) {
26 if ($max === null ||
$number > $max) {
34 * Returns the largest element of the given Traversable, or null if the
35 * Traversable is empty.
37 * The value for comparison is determined by the given function. In the case of
38 * duplicate numeric keys, later values overwrite previous ones.
40 * For numeric elements, see `Math\max()`.
43 Traversable
<T
> $traversable,
44 (function(T
)[_
]: num
) $num_func,
45 )[ctx
$num_func]: ?T
{
48 foreach ($traversable as $value) {
49 $value_num = $num_func($value);
50 if ($max_num === null ||
$value_num >= $max_num) {
52 $max_num = $value_num;
59 * Returns the arithmetic mean of the numbers in the given container.
61 * - To find the sum, see `Math\sum()`.
62 * - To find the maximum, see `Math\max()`.
63 * - To find the minimum, see `Math\min()`.
65 function mean(Container
<num
> $numbers)[]: ?
float {
66 $count = (float)C\
count($numbers);
71 foreach ($numbers as $number) {
72 $mean +
= $number / $count;
78 * Returns the median of the given numbers.
80 * To find the mean, see `Math\mean()`.
82 function median(Container
<num
> $numbers)[]: ?
float {
83 $numbers = Vec\
sort($numbers);
84 $count = C\
count($numbers);
88 $middle_index = Math\
int_div($count, 2);
89 if ($count %
2 === 0) {
91 vec
[$numbers[$middle_index], $numbers[$middle_index - 1]]
94 return (float)$numbers[$middle_index];
98 * Returns the smallest element of the given Traversable, or null if the
99 * Traversable is empty.
101 * - For a known number of inputs, see `Math\minva()`.
102 * - To find the largest number, see `Math\max()`.
104 function min
<T
as num
>(
105 Traversable
<T
> $numbers,
108 foreach ($numbers as $number) {
109 if ($min === null ||
$number < $min) {
117 * Returns the smallest element of the given Traversable, or null if the
118 * Traversable is empty.
120 * The value for comparison is determined by the given function. In the case of
121 * duplicate numeric keys, later values overwrite previous ones.
123 * For numeric elements, see `Math\min()`.
126 Traversable
<T
> $traversable,
127 (function(T
)[_
]: num
) $num_func,
128 )[ctx
$num_func]: ?T
{
131 foreach ($traversable as $value) {
132 $value_num = $num_func($value);
133 if ($min_num === null ||
$value_num <= $min_num) {
135 $min_num = $value_num;
142 * Returns the integer sum of the values of the given Traversable.
144 * For a float sum, see `Math\sum_float()`.
147 Traversable
<int> $traversable,
150 foreach ($traversable as $value) {
151 $result +
= (int)$value;
157 * Returns the float sum of the values of the given Traversable.
159 * For an integer sum, see `Math\sum()`.
161 function sum_float
<T
as num
>(
162 Traversable
<T
> $traversable,
165 foreach ($traversable as $value) {
166 $result +
= (float)$value;