A simple PHP extension written in C++ - with PHP-CPP - with additional userland helper functions.
- C++ 11
- PHP-CPP extension
- PHP 7 (or greater)
Because ext-func is an extension built on top of PHP-CPP, installation of PHP-CPP is a mandatory prerequisite for using this tool. There exists an elaborate installation guide on the PHP-CPP website - I suggest that you peruse it.
Once PHP-CPP is successfully installed on your system, type the following in a console to install ext-func on your machine.
git clone https://github.com/ace411/ext-func.git
cd ext-func
make && sudo make install
Because the functions in this package are probably better suited for the PHP-userland, I'd recommend usage of libraries like bingo-functional from which the package borrows multiple ideas, functional-php, or even phunkie or lodash-php.
Returns the value received with no transformations.
$identity = identity(12);
//outputs 12
Transforms each entry in a collection.
$map = map(function (int $val) : double {
return (double) ($val * 4) / 2;
}, range(1, 4));
//outputs [2, 4, 6, 8]
Transforms all list entries in a multi-dimensional array.
$map = mapDeep(function ($val) : int {
return pow($val, 2);
}, [2, 5, 7, 11, [3, 6, 9]]);
//outputs [4, 25, 49, 121, [9, 36, 81]]
Select values based on a boolean predicate.
$filter = filter(function (int $val) : bool {
return pow($val, 2) > 36;
}, range(4, 9));
//outputs [7, 8, 9]
Performs the filter action on a multi-dimensional array.
$filter = filterDeep(function ($val) {
return mb_strlen($val, 'utf-8') > 3;
}, array('foo', array('bar', 'baz'), array('foo-bar')));
//outputs [['foo-bar']]
Selects values that do not conform to a boolean predicate.
$list = A\reject(function (int $val) : bool {
return $val % 2 == 0;
}, array(12, 13, 19, 15, 22));
//outputs [13, 19, 15]
Transforms a list into a single value.
const LIST = array(4, 5, 7, 3);
$fold = fold(function (int $acc, int $val) : int {
return $val > $acc ? $val : $acc;
}, LIST, reset(LIST));
//outputs 7
Checks if a single value in a list conforms to a boolean predicate.
$check = any(function ($val) : bool {
return is_bool($val);
}, array(false, 'mike', 12));
//outputs true
Checks if a boolean predicate holds for every value in a list.
$check = every(function (int $val) : bool {
return is_int($val);
}, range(5, 12));
//outputs true
Truncates array dimensionality.
$flat = flatten(array(4, 6, array(9, 84), array(12, 15)));
//outputs [4, 6, 9, 84, 12, 15]
Checks if specified keys exist in an array.
$check = keysExist(
array(
'username' => 'ace411',
'packages' => array('bingo-functional', 'extfunc_cpp')
),
array('password', 'twitter')
);
//outputs false
Computes the mean of the numbers in an array.
$avg = mean(range(12, 35));
//outputs 23.5
Eliminates all the falsey values from an array.
$purged = purge(array(false, null, 'rainbow', 12));
//outputs ['rainbow', 12]
Outputs the first value in a list.
$first = head(array('foo', 'bar'));
//outputs 'foo'
Outputs the key that corresponds with a value in a list.
const LIST = array(
'name' => 'mike',
'isWriter' => true
);
$index = indexOf('mike');
//outputs 'name'