Instantly share code, notes, and snippets.

@krtek4 /utilities.php Secret
Last active Dec 9, 2018

Embed
What would you like to do?
Functional programming techniques in PHP
<?php
// Return the first / last element matching a predicate
function first(array $array, $test) {
foreach($array as $v)
if($test($v))
return $v;
return null;
}
// Return true if at least one element matches the predicate
function any($array, $test) {
foreach($array as $v)
if($test($v))
return true;
return false;
}
// Return true if all elements match the predicate
function all($array, $test) {
foreach($array as $v)
if(! $test($v))
return false;
return true;
}
function last(array $array, $test) {
return first(array_reverse($array), $test);
}
// Compose two functions together
function compose($f, $g) {
return function() use($f,$g) {
return $f(call_user_func_array($g, func_get_args()));
};
}
@vkalinsky

This comment has been minimized.

vkalinsky commented May 5, 2015

Semicolon in line 28?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment