Implements array_reduce functionality.