Skip to content

Instantly share code, notes, and snippets.

@krtek4
Last active Feb 11, 2021
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.

Copy link

@vkalinsky 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