Listt::of now only accepts iterables
authorwidmogrod <widmogrod@gmail.com>
Tue, 19 Dec 2017 21:20:13 +0000 (22:20 +0100)
committerwidmogrod <widmogrod@gmail.com>
Tue, 19 Dec 2017 21:20:13 +0000 (22:20 +0100)
example/Free2MonadTest.php
example/ListComprehensionWithMonadTest.php
example/MaybeMonadAndCollectionTest.php
example/MaybeMonoidTest.php
src/Functional/listt.php
src/Primitive/Listt.php
test/Monad/MaybeTest.php
test/Primitive/ListtTest.php
test/Primitive/ProductTest.php

index 18fac55..c6459ec 100644 (file)
@@ -127,7 +127,7 @@ function interpretState(TeletypeF $r)
             return State::of(function (Listt $state) use ($a) {
                 return [
                     $a->next,
-                    f\append($state, Listt::of('PutStrLn'))
+                    f\append($state, f\fromValue('PutStrLn'))
                 ];
             });
         },
@@ -135,7 +135,7 @@ function interpretState(TeletypeF $r)
             return State::of(function (Listt $state) use ($a) {
                 return [
                     ($a->processor)('demo'),
-                    f\append($state, Listt::of('GetLine'))
+                    f\append($state, f\fromValue('GetLine'))
                 ];
             });
         },
@@ -143,7 +143,7 @@ function interpretState(TeletypeF $r)
             return State::of(function (Listt $state) use ($a) {
                 return [
                     ff\Pure::of('exit'),
-                    f\append($state, Listt::of('ExitSuccess'))
+                    f\append($state, f\fromValue('ExitSuccess'))
                 ];
             });
         },
index c60e56b..0e2407d 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace example;
 
+use function Widmogrod\Functional\fromValue;
 use Widmogrod\Primitive\Listt;
 
 class ListComprehensionWithMonadTest extends \PHPUnit_Framework_TestCase
@@ -13,7 +14,7 @@ class ListComprehensionWithMonadTest extends \PHPUnit_Framework_TestCase
             ->bind(function ($n) {
                 return Listt::of(['a', 'b'])
                     ->bind(function ($x) use ($n) {
-                        return [[$n, $x]];
+                        return fromValue([$n, $x]);
                     });
             });
 
index 2bb85b8..14a39bd 100644 (file)
@@ -3,7 +3,9 @@
 namespace example;
 
 use Widmogrod\Monad\Maybe;
-use Widmogrod\Monad\Maybe as m;
+use function Widmogrod\Monad\Maybe\just;
+use const Widmogrod\Monad\Maybe\maybeNull;
+use function Widmogrod\Monad\Maybe\nothing;
 use Widmogrod\Primitive\Listt;
 use Widmogrod\Functional as f;
 
@@ -17,23 +19,22 @@ class MaybeMonadAndCollectionTest extends \PHPUnit_Framework_TestCase
         // $get :: String a -> [b] -> Maybe b
         $get = f\curryN(2, function ($key, $array) {
             return isset($array[$key])
-                ? m\just($array[$key])
-                : m\nothing();
+                ? just($array[$key])
+                : nothing();
         });
 
         $listOfFirstImages = f\pipeline(
             Listt::of,
-            f\map(m\maybeNull),
-            f\bind(f\bind($get('meta'))),
-            f\bind(f\bind($get('images'))),
-            f\bind(f\bind($get(0))),
-            f\join
+            f\map(maybeNull),
+            f\map(f\bind($get('meta'))),
+            f\map(f\bind($get('images'))),
+            f\map(f\bind($get(0)))
         );
 
         $result = $listOfFirstImages($data);
 
         $this->assertEquals(
-            Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]),
+            Listt::of([just('//first.jpg'), just('//third.jpg'), nothing()]),
             $result
         );
     }
@@ -47,19 +48,19 @@ class MaybeMonadAndCollectionTest extends \PHPUnit_Framework_TestCase
         $get = function ($key) {
             return f\bind(function ($array) use ($key) {
                 return isset($array[$key])
-                    ? m\just($array[$key])
-                    : m\nothing();
+                    ? just($array[$key])
+                    : nothing();
             });
         };
 
         $result = Listt::of($data)
             ->map(Maybe\maybeNull)
-            ->bind($get('meta'))
-            ->bind($get('images'))
-            ->bind($get(0));
+            ->map($get('meta'))
+            ->map($get('images'))
+            ->map($get(0));
 
         $this->assertEquals(
-            Listt::of([m\just('//first.jpg'), m\just('//third.jpg'), m\nothing()]),
+            Listt::of([just('//first.jpg'), just('//third.jpg'), nothing()]),
             $result
         );
     }
index 6ff26f3..a013756 100644 (file)
@@ -58,7 +58,7 @@ class MaybeMonoidTest extends \PHPUnit_Framework_TestCase
     {
         // $makeMaybeMonoid :: string -> Maybe Listt string
         $makeMaybeMonoid = function ($val): Maybe {
-            return maybeNull($val)->map(Listt::of);
+            return maybeNull($val)->map(f\fromValue);
         };
 
         // $names :: array Maybe Listt string
index 928231d..e0e235a 100644 (file)
@@ -5,11 +5,20 @@ namespace Widmogrod\Functional;
 use Widmogrod\FantasyLand\Foldable;
 use Widmogrod\Primitive\Listt;
 
+const fromIterable = 'Widmogrod\Functional\fromIterable';
+
 function fromIterable(iterable $i): Listt
 {
     return Listt::of(array_map(identity, $i));
 }
 
+const fromValue = 'Widmogrod\Functional\fromValue';
+
+function fromValue($value): Listt
+{
+    return Listt::of([$value]);
+}
+
 /**
  * @var callable
  */
index 3d17e29..521eb0a 100644 (file)
@@ -18,14 +18,8 @@ class Listt implements
 
     public const of = 'Widmogrod\Primitive\Listt::of';
 
-    /**
-     * @param array $value
-     */
-    public function __construct($value)
+    public function __construct(iterable $value)
     {
-        $givenType = is_object($value) ? get_class($value) : gettype($value);
-        assert(is_iterable($value), "Not iterable value given $givenType");
-
         $this->value = $value;
     }
 
index 21d438e..ca3d206 100644 (file)
@@ -134,9 +134,9 @@ class MaybeTest extends \PHPUnit_Framework_TestCase
     {
         return [
             'Just' => [
-                '$x' => Just::of(Listt::of(1)),
-                '$y' => Just::of(Listt::of(2)),
-                '$z' => Just::of(Listt::of(3))
+                '$x' => Just::of(Listt::of([1])),
+                '$y' => Just::of(Listt::of([2])),
+                '$z' => Just::of(Listt::of([3]))
             ],
             'Nothing' => [
                 '$x' => Nothing::mempty(),
index f23dc43..c2e708a 100644 (file)
@@ -7,6 +7,7 @@ use Widmogrod\FantasyLand\Applicative;
 use Widmogrod\FantasyLand\Functor;
 use Widmogrod\FantasyLand\Monoid;
 use Widmogrod\Functional as f;
+use const Widmogrod\Functional\fromValue;
 use Widmogrod\Helpful\ApplicativeLaws;
 use Widmogrod\Helpful\FunctorLaws;
 use Widmogrod\Helpful\MonadLaws;
@@ -26,7 +27,7 @@ class ListtTest extends \PHPUnit_Framework_TestCase
     {
         MonadLaws::test(
             f\curryN(3, [$this, 'assertEquals']),
-            f\curryN(1, Listt::of),
+            f\curryN(1, fromValue),
             $f,
             $g,
             $x
@@ -36,10 +37,10 @@ class ListtTest extends \PHPUnit_Framework_TestCase
     public function provideData()
     {
         $addOne = function ($x) {
-            return Listt::of($x + 1);
+            return Listt::of([$x + 1]);
         };
         $addTwo = function ($x) {
-            return Listt::of($x + 2);
+            return Listt::of([$x + 2]);
         };
 
         return [
@@ -77,16 +78,16 @@ class ListtTest extends \PHPUnit_Framework_TestCase
     {
         return [
             'Listt' => [
-                '$pure' => Listt::of,
-                '$u'    => Listt::of(function () {
+                '$pure' => fromValue,
+                '$u'    => Listt::of([function () {
                     return 1;
-                }),
-                '$v' => Listt::of(function () {
+                }]),
+                '$v' => Listt::of([function () {
                     return 5;
-                }),
-                '$w' => Listt::of(function () {
+                }]),
+                '$w' => Listt::of([function () {
                     return 7;
-                }),
+                }]),
                 '$f' => function ($x) {
                     return $x + 400;
                 },
index d5310a5..2655571 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace test\Primitive;
 
+use Eris\TestTrait;
 use Widmogrod\Functional as f;
 use Widmogrod\Helpful\MonoidLaws;
 use Widmogrod\Helpful\SetoidLaws;
@@ -9,6 +10,7 @@ use Widmogrod\Primitive\Product;
 
 class ProductTest extends \PHPUnit_Framework_TestCase
 {
+    use TestTrait;
     /**
      * @dataProvider provideSetoidLaws
      */
@@ -43,7 +45,9 @@ class ProductTest extends \PHPUnit_Framework_TestCase
 
     private function randomize()
     {
-        return Product::of(random_int(-100000000, 100000000));
+        usleep(10);
+
+        return Product::of(random_int(-1000, 1000));
     }
 
     public function provideSetoidLaws()