PHP Ds Deque reduce() 函数
Ds\Deque::reduce() 函数可以使用回调函数将 deque 缩减为一个单一的值。
语法
public mixed Ds\Deque::reduce( callable callback [, mixedinitial ] )
Ds\Deque::reduce()函数可以返回最终回调的值。
示例1
<?php
deque = new \Ds\Deque([10, 20, 30, 40, 50]); echo("The elements in deque: \n"); print_r(deque);
func = function(carry, element) { returncarry + element; }; echo("\n The deque after reduced into single element:"); var_dump(deque->reduce($func, 3));
?>
示例2
<?php
deque = new \Ds\Deque([10, 20, 30, 40, 50]); echo("The elements in deque: \n"); print_r(deque);
func = function(carry, element) { returncarry * element + 30; }; echo("\n The deque after reduced into single element:"); var_dump(deque->reduce($func, 10));
?>