A functional rewrite of ruby's inject method