Skip to content

Commit

Permalink
Math\{maxv, minv}
Browse files Browse the repository at this point in the history
Summary:
This introduces `Math\{maxv, minv}` and aliases `Math\{max, min}` to them for migration purposes. The intended end state is that `C\{max, min}` are moved into the Math namespaces to become `Math\{max, min}`.

See #25.

Reviewed By: fredemmott

Differential Revision: D6095022

fbshipit-source-id: d1199d2d4f4a2d0cdbfe552679b833c50b7ac13b
  • Loading branch information
kmeht authored and facebook-github-bot committed Oct 19, 2017
1 parent 70925a4 commit 8f4ab2e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 15 deletions.
44 changes: 44 additions & 0 deletions src/math/compare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

namespace HH\Lib\Math;

/**
* Returns the largest of all input numbers.
*
* To find the smallest number, see `Math\min`.
* For Traversables, see `C\max`.
*/
function maxv<T as num>(T $first, T ...$rest): T {
$max = $first;
foreach ($rest as $number) {
if ($number > $max) {
$max = $number;
}
}
return $max;
}

/**
* Returns the smallest of all input numbers.
*
* To find the largest number, see `Math\max`.
* For Traversables, see `C\min`.
*/
function minv<T as num>(T $first, T ...$rest): T {
$min = $first;
foreach ($rest as $number) {
if ($number < $min) {
$min = $number;
}
}
return $min;
}
17 changes: 2 additions & 15 deletions src/math/containers.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,7 @@
* For Traversables, see `C\max`.
*/
function max<T as num>(T $first_number, T ...$numbers): T {
$max = $first_number;
foreach ($numbers as $number) {
if ($number > $max) {
$max = $number;
}
}
return $max;
return namespace\maxv($first_number, ...$numbers);
}

/**
Expand Down Expand Up @@ -55,7 +49,6 @@ function mean(Container<num> $numbers): ?float {
*/
function median(Container<num> $numbers): ?float {
$numbers = Vec\sort($numbers);

$count = C\count($numbers);
if ($count === 0) {
return null;
Expand All @@ -76,13 +69,7 @@ function median(Container<num> $numbers): ?float {
* For Traversables, see `C\min`.
*/
function min<T as num>(T $first_number, T ...$numbers): T {
$min = $first_number;
foreach ($numbers as $number) {
if ($number < $min) {
$min = $number;
}
}
return $min;
return namespace\minv($first_number, ...$numbers);
}

/**
Expand Down
67 changes: 67 additions & 0 deletions tests/math/MathCompareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?hh // strict
/*
* Copyright (c) 2004-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*/

use namespace HH\Lib\Math;
use function Facebook\FBExpect\expect;

/**
* @emails oncall+hack_prod_infra
*/
final class MathCompareTest extends PHPUnit_Framework_TestCase {

public static function provideTestMaxv(): array<mixed> {
return array(
tuple(1, vec[2], 2),
tuple(2, vec[1], 2),
tuple(1.0, vec[2.0], 2.0),
tuple(2.0, vec[1.0], 2.0),
tuple(1, vec[1], 1),
tuple(-2, vec[-1], -1),
tuple(1.0, vec[2], 2),
tuple(1, vec[2.0], 2.0),
tuple(-1, vec[1, 2, 3, 4, 5], 5),
tuple(-1, vec[5, 4, 3, 2, 1], 5),
);
}

/** @dataProvider provideTestMaxv */
public function testMaxv<T as num>(
T $first,
Container<T> $rest,
T $expected,
): void {
expect(Math\maxv($first, ...$rest))->toBeSame($expected);
}

public static function provideTestMinv(): array<mixed> {
return array(
tuple(1, vec[2], 1),
tuple(2, vec[1], 1),
tuple(1.0, vec[2.0], 1.0),
tuple(2.0, vec[1.0], 1.0),
tuple(1, vec[1], 1),
tuple(-2, vec[-1], -2),
tuple(1.0, vec[2], 1.0),
tuple(1, vec[2.0], 1),
tuple(1, vec[-1, -2, -3, -4, -5], -5),
tuple(1, vec[-5, -4, -3, -2, -1], -5),
);
}

/** @dataProvider provideTestMinv */
public function testMinv<T as num>(
T $first,
Container<T> $rest,
T $expected,
): void {
expect(Math\minv($first, ...$rest))->toBeSame($expected);
}
}

0 comments on commit 8f4ab2e

Please sign in to comment.