Closed jwestbrook closed 10 years ago
Juriy Zaytsev December 18th, 2010 @ 12:16 AM
Importance changed from “” to “Low” Very interesting method; looks useful indeed. I do think that mash would be an overkill here, considering that ES5 allows to achieve this same task rather elegantly. And simple.
['exit', 'the', 'dragon'].reduce(function(obj, key) {
obj[key] = key.length;
return obj;
}, { });
See Array.prototype.reduce (on MDN or in ES5 spec).
Arnau Sanchez December 18th, 2010 @ 12:36 AM
Oops, my bad, I keep forgetting about foldl/reduce/inject while writing JS, which is obviously the right approach to implement mash (and that's how it's also implemented in Ruby). Well, compact as it with reduce, the point was more about encouraging good practices, I think that having standard methods for the two-way Array<->Hash conversions is important. Thanks for taking it into consideration, though.
previous lighthouse ticket #1186 by Arnau Sanchez
Programmers who have used Ruby's Facets know how useful Enumerable#mash is:
My proposal is to add this method to Prototype. I have no deep knowledge of the library internals, but this may be a possible implementation:
And now you can write:
I'll understand if the answer is 'no, we prefer not to bloat the library', but I think Enumerable#mash would encourage sound programming practices (compare it with the -IMHO- hideous imperative pattern 'init empty + iterate each + modify inplace + return') in the same way Enumerable#map does