Ds\Sequence::reduce() 函數可以使用回調函數將序列減少到單個值。
用法
public abstract mixed Ds\Sequence::reduce( callable $callback [, mixed $initial ] )
Ds\Sequence::reduce() 函數可以返回最終回調的值。
例子1
<?php
$seq = new \Ds\Vector([1, 2, 3]);
$callback = function($carry, $value) {
return $carry * $value;
};
var_dump($seq->reduce($callback, 5));
?>
例子2
<?php
$seq = new \Ds\Vector([1, 2, 3]);
var_dump($seq->reduce(function($carry, $value) {
return $carry + $value + 5;
}));
?>
相關用法
- PHP Ds Sequence remove()用法及代碼示例
- PHP Ds Sequence reverse()用法及代碼示例
- PHP Ds Sequence reversed()用法及代碼示例
- PHP Ds Sequence rotate()用法及代碼示例
- PHP Ds Sequence capacity()用法及代碼示例
- PHP Ds Sequence sort()用法及代碼示例
- PHP Ds Sequence insert()用法及代碼示例
- PHP Ds Sequence slice()用法及代碼示例
- PHP Ds Sequence sorted()用法及代碼示例
- PHP Ds Sequence shift()用法及代碼示例
- PHP Ds Sequence get()用法及代碼示例
- PHP Ds Sequence join()用法及代碼示例
- PHP Ds Sequence pop()用法及代碼示例
- PHP Ds Sequence contains()用法及代碼示例
注:本文由純淨天空篩選整理自 PHP - Ds Sequence reduce() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。