Skip to content

Commit

Permalink
Convert Math\min to take a Traversable
Browse files Browse the repository at this point in the history
Summary:
The function formerly known as `Math\min` was renamed to `Math\minv`. This function will replace `C\min`.

See #25.

Reviewed By: fredemmott

Differential Revision: D6096198

fbshipit-source-id: 9f3067b1ba358a8ca6520fe8dbc8e13f09488eda
  • Loading branch information
kmeht authored and facebook-github-bot committed Oct 20, 2017
1 parent 5fd0e01 commit 94d870a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/math/containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,22 @@ function median(Container<num> $numbers): ?float {
}

/**
* Returns the smallest of all input numbers.
* Returns the smallest element of the given Traversable, or null if the
* Traversable is empty.
*
* For finding the largest number, see `Math\max`.
* For Traversables, see `C\min`.
* For a known number of inputs, see `Math\minv`.
* To find the largest number, see `Math\max`.
*/
<<__Deprecated('renamed to Math\\minv')>>
function min<T as num>(T $first_number, T ...$numbers): T {
return namespace\minv($first_number, ...$numbers);
function min<T as num>(
Traversable<T> $numbers,
): ?T {
$min = null;
foreach ($numbers as $number) {
if ($min === null || $number < $min) {
$min = $number;
}
}
return $min;
}

/**
Expand Down
31 changes: 31 additions & 0 deletions tests/math/MathContainersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,37 @@ public function testMedian(
}
}

public static function provideTestMin(): array<mixed> {
return array(
tuple(
array(),
null,
),
tuple(
Set {8, 6, 7, 5, 3, 0, 9},
0,
),
tuple(
HackLibTestTraversables::getIterator(
array(8, 6, 7, 5, 3, 0, 9),
),
0,
),
tuple(
Vector {8, 6, 7, -5, -3, 0, 9},
-5,
),
);
}

/** @dataProvider provideTestMin */
public function testMin<T as num>(
Traversable<T> $traversable,
?T $expected,
): void {
expect(Math\min($traversable))->toBeSame($expected);
}

public static function provideTestMinBy(): array<mixed> {
return array(
tuple(
Expand Down

0 comments on commit 94d870a

Please sign in to comment.