Skip to content

Commit

Permalink
Add reject and not functions
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbiller committed Jan 19, 2017
1 parent 0796f38 commit c8ff22a
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ To see coverage do ```composer run test:coverage```.
- Collections
- concat
- filter
- reject
- map
- reduce
- flatten
Expand All @@ -40,6 +41,8 @@ To see coverage do ```composer run test:coverage```.
- Currying
- curry
- curryN
- Logic
- not
- Extras
- first
- last
Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"autoload": {
"psr-4": {"fphp\\": "src/"},
"files": [
"src/Utils/reflexify.php",
"src/concat.php",
"src/curry.php",
"src/curryN.php",
Expand All @@ -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"
]
},
Expand Down
20 changes: 20 additions & 0 deletions src/Utils/reflexify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace fphp\Utils;

use ReflectionFunction;
use ReflectionMethod;

function reflexify($f) {
if (is_string($f) && strpos($f, '::', 1) !== false) {
$reflection = new ReflectionMethod($f);
} elseif (is_array($f) && count($f) === 2) {
$reflection = new ReflectionMethod($f[0], $f[1]);
} elseif (is_object($f) && method_exists($f, '__invoke')) {
$reflection = new ReflectionMethod($f, '__invoke');
} else {
$reflection = new ReflectionFunction($f);
}

return $reflection;
}
11 changes: 2 additions & 9 deletions src/curry.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace fphp;

use function fphp\Utils\reflexify;
use ReflectionFunction;
use ReflectionMethod;
use Closure;
Expand All @@ -18,15 +19,7 @@ function curry(callable $f) {
if (method_exists('Closure', 'fromCallable')) {
$reflection = new ReflectionFunction(Closure::fromCallable($f));
} else {
if (is_string($f) && strpos($f, '::', 1) !== false) {
$reflection = new ReflectionMethod($f);
} elseif (is_array($f) && count($f) === 2) {
$reflection = new ReflectionMethod($f[0], $f[1]);
} elseif (is_object($f) && method_exists($f, '__invoke')) {
$reflection = new ReflectionMethod($f, '__invoke');
} else {
$reflection = new ReflectionFunction($f);
}
$reflection = reflexify($f);
}

$count = $reflection->getNumberOfRequiredParameters();
Expand Down
14 changes: 14 additions & 0 deletions src/not.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace fphp;

/**
* Return true for every falsy value and false for every truthy value.
*
* @param mixed $value value
* @return mixed
* @author Rémy Peru <peru.remy@gmail.com>
*/
function not($value) {
return !$value;
}
26 changes: 26 additions & 0 deletions src/reject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace fphp;

/**
* Complement of filter
*
* @param callable $f function
* @param array $collection collection
* @return $array
* @author Rémy Peru <peru.remy@gmail.com>
*/
function reject(...$args) {
$reject = function (callable $f, $collection) {
$result = [];
foreach ($collection as $element) {
if (!$f($element)) {
$result[] = $element;
}
}

return $result;
};

return curry($reject)(...$args);
}
34 changes: 34 additions & 0 deletions tests/Utils/reflexify.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use function fphp\Utils\reflexify;

class TestClass {
public function __invoke() {
echo 'test';
}

static public function staticTest() {
return true;
}
}

describe('reflexify', function () {
it('should get reflection of callable', function () {
$closure = reflexify('TestClass::staticTest');
expect($closure)->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');
});
});
17 changes: 17 additions & 0 deletions tests/not.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

use function fphp\not;

describe('not', function () {
it('should return true', function () {
$res = not(0);

expect($res)->toBe(true);
});

it('should return false', function () {
$res = not(true);

expect($res)->toBe(false);
});
});
22 changes: 22 additions & 0 deletions tests/reject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use function fphp\reject;

describe('reject', function () {
it('should keep every dogs', function () {
$f = function($x) { return $x !== 'dog'; };

$res = reject($f, ['cat', 'cat', 'dog', 'cat', 'dog']);

expect($res)->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']);
});
});

0 comments on commit c8ff22a

Please sign in to comment.