Array.reduce implementation