From 688a207125b45c6f9bfcbe7c5b0ee8a9e2879467 Mon Sep 17 00:00:00 2001 From: widmogrod Date: Tue, 19 Dec 2017 22:20:13 +0100 Subject: [PATCH] Listt::of now only accepts iterables --- example/Free2MonadTest.php | 6 +++--- example/ListComprehensionWithMonadTest.php | 3 ++- example/MaybeMonadAndCollectionTest.php | 31 +++++++++++++++--------------- example/MaybeMonoidTest.php | 2 +- src/Functional/listt.php | 9 +++++++++ src/Primitive/Listt.php | 8 +------- test/Monad/MaybeTest.php | 6 +++--- test/Primitive/ListtTest.php | 21 ++++++++++---------- test/Primitive/ProductTest.php | 6 +++++- 9 files changed, 51 insertions(+), 41 deletions(-) diff --git a/example/Free2MonadTest.php b/example/Free2MonadTest.php index 18fac55..c6459ec 100644 --- a/example/Free2MonadTest.php +++ b/example/Free2MonadTest.php @@ -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')) ]; }); }, diff --git a/example/ListComprehensionWithMonadTest.php b/example/ListComprehensionWithMonadTest.php index c60e56b..0e2407d 100644 --- a/example/ListComprehensionWithMonadTest.php +++ b/example/ListComprehensionWithMonadTest.php @@ -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]); }); }); diff --git a/example/MaybeMonadAndCollectionTest.php b/example/MaybeMonadAndCollectionTest.php index 2bb85b8..14a39bd 100644 --- a/example/MaybeMonadAndCollectionTest.php +++ b/example/MaybeMonadAndCollectionTest.php @@ -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 ); } diff --git a/example/MaybeMonoidTest.php b/example/MaybeMonoidTest.php index 6ff26f3..a013756 100644 --- a/example/MaybeMonoidTest.php +++ b/example/MaybeMonoidTest.php @@ -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 diff --git a/src/Functional/listt.php b/src/Functional/listt.php index 928231d..e0e235a 100644 --- a/src/Functional/listt.php +++ b/src/Functional/listt.php @@ -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 */ diff --git a/src/Primitive/Listt.php b/src/Primitive/Listt.php index 3d17e29..521eb0a 100644 --- a/src/Primitive/Listt.php +++ b/src/Primitive/Listt.php @@ -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; } diff --git a/test/Monad/MaybeTest.php b/test/Monad/MaybeTest.php index 21d438e..ca3d206 100644 --- a/test/Monad/MaybeTest.php +++ b/test/Monad/MaybeTest.php @@ -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(), diff --git a/test/Primitive/ListtTest.php b/test/Primitive/ListtTest.php index f23dc43..c2e708a 100644 --- a/test/Primitive/ListtTest.php +++ b/test/Primitive/ListtTest.php @@ -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; }, diff --git a/test/Primitive/ProductTest.php b/test/Primitive/ProductTest.php index d5310a5..2655571 100644 --- a/test/Primitive/ProductTest.php +++ b/test/Primitive/ProductTest.php @@ -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() -- 2.11.0