JavaScript has map, filter and reduce methods on Array. However, JavaScript allows you to define your own iterable types, and it allows you to define your own generator functions. You can’t currently map, filter or reduce over anything other than arrays, without writing your own generic map/filter/reduce functions. It would probably be nice to have generic map/filter/reduce functions in the standard library.
One of the classic examples of a JavaScript iterable is one returned by a Fibonacci generator function. As handy as map/filter/reduce methods are, TC39 can be forgiven for omitting them given that iterables like these are infinite.
Generators are common in a lot of other languages and you can still iterate over them, at your own peril, or slice them to a suitable length. Why release something so half-cooked as this?
You can, but as @mort said you have to use for of instead of methods. Which seems reasonable at first glance, given that to do otherwise with user-specified iterators would be akin to magically adding methods to your object. What’s more egregious though is the fact that nobody bothered to add those functions we’re used to from Array to either Set or Map, and that there’s basically nothing at all added for async interation anywhere except for await of which is as confusing and rarely used as you’re imagining.
Javascript has had
mapandfilterfor a long time — why do they no longer work?JavaScript has
map,filterandreducemethods onArray. However, JavaScript allows you to define your own iterable types, and it allows you to define your own generator functions. You can’t currently map, filter or reduce over anything other than arrays, without writing your own generic map/filter/reduce functions. It would probably be nice to have generic map/filter/reduce functions in the standard library.Wow, that sounds… well, I can’t fathom what would even be the point in having iterators if you can’t even… well, iterate over them.
Well, you can iterate over them. But currently only via the
for (<variable> of <iterable>)loop, no cool functional stuff.One of the classic examples of a JavaScript iterable is one returned by a Fibonacci generator function. As handy as map/filter/reduce methods are, TC39 can be forgiven for omitting them given that iterables like these are infinite.
Generators are common in a lot of other languages and you can still iterate over them, at your own peril, or slice them to a suitable length. Why release something so half-cooked as this?
You can, but as @mort said you have to use
for ofinstead of methods. Which seems reasonable at first glance, given that to do otherwise with user-specified iterators would be akin to magically adding methods to your object. What’s more egregious though is the fact that nobody bothered to add those functions we’re used to fromArrayto eitherSetorMap, and that there’s basically nothing at all added for async interation anywhere exceptfor await ofwhich is as confusing and rarely used as you’re imagining.