From: widmogrod Date: Thu, 21 Dec 2017 18:37:01 +0000 (+0100) Subject: Functional\constt test + implementation X-Git-Tag: 4.0.0~3^2~2 X-Git-Url: http://cameron1729.xyz/?a=commitdiff_plain;h=bf5ae6aa0b0f379b5fd6dbc6ee20e83bdc2410e2;p=php-functional.git Functional\constt test + implementation --- diff --git a/src/Functional/miscellaneous.php b/src/Functional/miscellaneous.php index 0787ebe..9379d9f 100644 --- a/src/Functional/miscellaneous.php +++ b/src/Functional/miscellaneous.php @@ -22,21 +22,30 @@ function identity($x) } /** + * @var callable + */ +const constt = 'Widmogrod\Functional\constt'; + +/** * const :: a -> b -> a * * const x is a unary function which evaluates to x for all inputs. * * For instance, - * + * ```haskell * >>> map (const 42) [0..3] * [42,42,42,42] + * ``` * * @param $a * @param $b + * @return callable */ -function constt($a, $b) +function constt($a, $b = null) { - // TODO + return curryN(2, function ($a) { + return $a; + }); } diff --git a/test/Functional/ConsttTest.php b/test/Functional/ConsttTest.php new file mode 100644 index 0000000..ba72215 --- /dev/null +++ b/test/Functional/ConsttTest.php @@ -0,0 +1,23 @@ +forAll( + Generator\int(), + Generator\int() + )->then(function ($a, $b) { + return constt($a, $b) === $a + && constt($a)($b) === $a; + }); + } +}