3 ms·
The biggest problem with js right now is the fragmented and inconsistent collections apis. Collections in js are modelled with Arrays, (Weak)Maps, (Weak)Sets,
by kybernetikos 10y ago
The biggest problem with js right now is the fragmented and inconsistent collections apis.
Collections in js are modelled with Arrays, (Weak)Maps, (Weak)Sets, Iterators, iterables/iterators, strings, promises, Nodelists, typed arrays, objects (where structs would be used in other languages), generators, collections provided by third party libraries (e.g. immutable structures)
There is no convenient set of methods that work on all of these, and no protocol that you could use in your thirdparty library to behave in a consistent and expected way.
The closest we have is iterables, but you can do very little with an iterable beyond iterating it. No map, no filter, no flatmap, no reduce. You can create a new array out of it and then use the array methods (which don't exist on any other collection), but that's pretty dumb too.
"We'll let libraries decide" is the usual cry, but it's impossible to write functions that will appear only on iterables or iterators, since they have no common prototype other than Object.
Ideally the language itself would provide a small, coherent protocol that all collection and collection-like things could implement, and then all the good stuff could be built on top.
Languages like clojure do a good job of this.
- xamuel 10y ago>no map, no filter, no flatmap, no reduce ES6 generators pretty much solve this, especially once you learn "yield*"
- WorldMaker 10y agoES6 generators are a low level construct. The concern is that you still need to add a dependency to a library such as wu.js [1] for the higher order functions around generators/iterables. (Compare with C# where a laundry list of higher order LINQ operators are provided out of the box as extensions to IEnumerable. Or Python's 2.x itertools modules, many of which have filtered into the top level of the standard library in Python 3.) [1] http://fitzgen.github.io/wu.js/ http://fitzgen.github.io/wu.js/