From c8ff22aa26d814120cacbf4481612f6ea46094ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Peru?= Date: Thu, 19 Jan 2017 18:01:31 +0100 Subject: [PATCH] Add reject and not functions --- README.md | 3 +++ composer.json | 3 +++ src/Utils/reflexify.php | 20 ++++++++++++++++++++ src/curry.php | 11 ++--------- src/not.php | 14 ++++++++++++++ src/reject.php | 26 ++++++++++++++++++++++++++ tests/Utils/reflexify.php | 34 ++++++++++++++++++++++++++++++++++ tests/not.php | 17 +++++++++++++++++ tests/reject.php | 22 ++++++++++++++++++++++ 9 files changed, 141 insertions(+), 9 deletions(-) create mode 100755 src/Utils/reflexify.php create mode 100755 src/not.php create mode 100755 src/reject.php create mode 100755 tests/Utils/reflexify.php create mode 100755 tests/not.php create mode 100755 tests/reject.php diff --git a/README.md b/README.md index 229ef82..1629f8f 100755 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ To see coverage do ```composer run test:coverage```. - Collections - concat - filter + - reject - map - reduce - flatten @@ -40,6 +41,8 @@ To see coverage do ```composer run test:coverage```. - Currying - curry - curryN +- Logic + - not - Extras - first - last diff --git a/composer.json b/composer.json index 6fa4050..d0c282d 100755 --- a/composer.json +++ b/composer.json @@ -18,6 +18,7 @@ "autoload": { "psr-4": {"fphp\\": "src/"}, "files": [ + "src/Utils/reflexify.php", "src/concat.php", "src/curry.php", "src/curryN.php", @@ -33,7 +34,9 @@ "src/last.php", "src/map.php", "src/none.php", + "src/not.php", "src/reduce.php", + "src/reject.php", "src/zip.php" ] }, diff --git a/src/Utils/reflexify.php b/src/Utils/reflexify.php new file mode 100755 index 0000000..99f742a --- /dev/null +++ b/src/Utils/reflexify.php @@ -0,0 +1,20 @@ +getNumberOfRequiredParameters(); diff --git a/src/not.php b/src/not.php new file mode 100755 index 0000000..5b9c8f6 --- /dev/null +++ b/src/not.php @@ -0,0 +1,14 @@ + + */ +function not($value) { + return !$value; +} diff --git a/src/reject.php b/src/reject.php new file mode 100755 index 0000000..ca1d33a --- /dev/null +++ b/src/reject.php @@ -0,0 +1,26 @@ + + */ +function reject(...$args) { + $reject = function (callable $f, $collection) { + $result = []; + foreach ($collection as $element) { + if (!$f($element)) { + $result[] = $element; + } + } + + return $result; + }; + + return curry($reject)(...$args); +} diff --git a/tests/Utils/reflexify.php b/tests/Utils/reflexify.php new file mode 100755 index 0000000..dfea009 --- /dev/null +++ b/tests/Utils/reflexify.php @@ -0,0 +1,34 @@ +toBeAnInstanceOf('ReflectionMethod'); + + $closure = reflexify(['DateTime', 'createFromFormat']); + expect($closure)->toBeAnInstanceOf('ReflectionMethod'); + + $invoke = new TestClass(); + $closure = reflexify($invoke); + expect($closure)->toBeAnInstanceOf('ReflectionMethod'); + + $closure = reflexify('explode'); + expect($closure)->toBeAnInstanceOf('ReflectionFunction'); + + $f = function($a, $b) { return $a + $b; }; + $closure = reflexify($f); + expect($closure)->toBeAnInstanceOf('ReflectionMethod'); + }); +}); diff --git a/tests/not.php b/tests/not.php new file mode 100755 index 0000000..f1f6304 --- /dev/null +++ b/tests/not.php @@ -0,0 +1,17 @@ +toBe(true); + }); + + it('should return false', function () { + $res = not(true); + + expect($res)->toBe(false); + }); +}); diff --git a/tests/reject.php b/tests/reject.php new file mode 100755 index 0000000..1863852 --- /dev/null +++ b/tests/reject.php @@ -0,0 +1,22 @@ +toBe(['dog', 'dog']); + }); + + it('should be curried', function () { + $f = function($x) { return $x !== 'dog'; }; + + $res = reject($f); + $res = $res(['cat', 'cat', 'dog', 'cat', 'dog']); + + expect($res)->toBe(['dog', 'dog']); + }); +});