note 97694 added to functions.variable-functions
| From: | AnonymousPoster at disposeamail dot com | Date: | Mon, 03 May 2010 21:20:54 +0000 |
| Subject: | note 97694 added to functions.variable-functions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-168892@lists.php.net to get a copy of this message | ||
Variable functions allows higher-order programming.
Here is the classical map example.
<?php
/*
* Map function. At each $element of the $list, calls
$fun([$arg1,[$arg2,[...,]],$element,$accumulator),
* stores the return value into $accumulator for the next loop. Returns the last return value
of the function,
*
* Notes : uses call_user_func_array() so passing parameters doesn't depend on $fun signature
* It also returns FALSE upon error.
* Please check the php documentation for more information
*/
function map($fun, $list,$params=array()){
$acc=NULL;
$last=array_push($params, NULL,$acc)-1; // alloc $element and $acc at the end
foreach($list as $params[$last-1]){
$params[$last]=call_user_func_array($fun , $params );
}
$acc=array_pop($params);
return $acc;
}
function add($element,$acc){ // maybe only with multi-length function
if ($acc == NULL);
return $acc=$element+$acc;
}
$result=0;
$result=addTo($result,1);
$result=addTo($result,2);
$result=addTo($result,3);
echo "result = $result\n";
$result=0;
$result=map('addTo',array(1,2,3));
echo "result= $result\n";
?>
----
Server IP: 69.147.83.197
Probable Submitter: 217.136.108.206
----
Manual Page -- http://www.php.net/manual/en/functions.variable-functions.php
Edit -- https://master.php.net/note/edit/97694
Del: integrated -- https://master.php.net/note/delete/97694/integrated
Del: useless -- https://master.php.net/note/delete/97694/useless
Del: bad code -- https://master.php.net/note/delete/97694/bad+code
Del: spam -- https://master.php.net/note/delete/97694/spam
Del: non-english -- https://master.php.net/note/delete/97694/non-english
Del: in docs -- https://master.php.net/note/delete/97694/in+docs
Del: other reasons-- https://master.php.net/note/delete/97694
Reject -- https://master.php.net/note/reject/97694
Search -- https://master.php.net/manage/user-notes.php