A rewrite of Ruby's inject method