Functional\constt test + implementation
authorwidmogrod <widmogrod@gmail.com>
Thu, 21 Dec 2017 18:37:01 +0000 (19:37 +0100)
committerwidmogrod <widmogrod@gmail.com>
Thu, 21 Dec 2017 18:37:01 +0000 (19:37 +0100)
src/Functional/miscellaneous.php
test/Functional/ConsttTest.php [new file with mode: 0644]

index 0787ebe..9379d9f 100644 (file)
@@ -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 (file)
index 0000000..ba72215
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+
+namespace test\Functional;
+
+use Eris\Generator;
+use Eris\TestTrait;
+use function Widmogrod\Functional\constt;
+
+class ConsttTest extends \PHPUnit_Framework_TestCase
+{
+    use TestTrait;
+
+    public function test_it_should_generate_infinite_list()
+    {
+        $this->forAll(
+            Generator\int(),
+            Generator\int()
+        )->then(function ($a, $b) {
+            return constt($a, $b) === $a
+                && constt($a)($b) === $a;
+        });
+    }
+}