ForNext
Only Do What Only You Can Do
122. reduce
Hyperpolyglot Interpreted Languages: PHP, Perl, Python, Ruby より (2012.05.08)
VBScript
JScript
Perl
use List::Util 'reduce'; reduce { $x + $y } 0, (1,2,3)
PHP
array_reduce(array(1,2,3), function($x,$y) { return $x+$y; }, 0)
Python
# import needed in Python 3 only from functools import reduce reduce(lambda x, y: x+y, [1,2,3], 0)
Ruby
[1,2,3].inject(0) { |m,o| m+o }