From bf5ae6aa0b0f379b5fd6dbc6ee20e83bdc2410e2 Mon Sep 17 00:00:00 2001 From: widmogrod Date: Thu, 21 Dec 2017 19:37:01 +0100 Subject: [PATCH] Functional\constt test + implementation --- src/Functional/miscellaneous.php | 15 ++++++++++++--- test/Functional/ConsttTest.php | 23 +++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 test/Functional/ConsttTest.php 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; + }); + } +} -- 2.11.0