为什么生成器不支持 map()? [英] Why do generators not support map()?

查看:25
本文介绍了为什么生成器不支持 map()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,生成器的功能与数组非常相似,应该支持非常基本的列表操作,例如 map()filter(),这似乎很自然,和 reduce().我错过了什么吗?

It seems utterly natural to me that generators, which function very much like Arrays, should support the very basic list operations, like map(), filter(), and reduce(). Am I missing something?

我为 map 编写了代码,看起来很简单,但最好将所有函数都嵌入到所有生成器中:

I wrote the code for map and it seems simple enough, but it would be much better to have all the functions embedded in all the generators:

let fancyGen = g => {
  let rv = function*() {
    for (let x of g) 
      yield x;
  }
  rv.map = function*(p) {
   for (let x of g) 
      yield p(x);
  } 
  return rv;
}

我是生成器的新手,因此欢迎对代码提出任何意见.特别是,这是编写身份生成器"的最佳方式吗?

I'm new to generators, so any comments on the code are welcome. In particular, is that the best way to write "the identity generator"?

推荐答案

为什么生成器不支持 map()?

Why do generators not support map()?

因为作为用户空间实现填写太容易了.ES3 也不包含数组迭代方法,也许会在 ES7 中看到用于迭代器的转换器 :-)

Because it's too easy to fill in as a userland implementation. ES3 didn't include Array iteration methods either, maybe will see transformers for iterators in ES7 :-)

生成器,其功能与数组非常相似

generators, which function very much like Arrays

不,请停止并区分迭代器生成器:

  • 迭代器是具有符合迭代器协议的 .next() 方法的对象.
  • 生成器是由生成器函数 (function*) 创建的迭代器.它的 .next() 方法接受一个参数,它是生成器函数中每个 yield 的结果.它还有 .return().throw() 方法.
  • An iterator is an object with a .next() method that conforms to the iterator protocol.
  • A generator is an iterator created by a generator function (function*). Its .next() method takes an argument which is the result of each yield inside the generator function. It also has .return() and .throw() methods.

您最感兴趣的是迭代器,我们不将值传递给 next,也不关心最终结果 - 就像 for of循环做.我们可以轻松地使用所需的方法扩展它们:

You'll mostly be interested in iterators, where we don't pass values to next, and don't care about the end result - just like for of loops do. We can extend them with the desired methods easily:

var IteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf([][Symbol.iterator]()));
IteratorPrototype.map = function*(f) {
    for (var x of this)
        yield f(x);
};
IteratorPrototype.filter = function*(p) {
    for (var x of this)
        if (p(x))
            yield x;
};
IteratorPrototype.scan = function*(f, acc) {
    for (var x of this)
        yield acc = f(acc, x);
    return acc;
};
IteratorPrototype.reduce = function(f, acc) {
    for (var x of this)
        acc = f(acc, x);
    return acc;
};

这些应该足以开始和最常见的用例.一个合适的库会将其扩展到生成器,以便适​​当地传递值,并且还将处理迭代器在耗尽之前只能使用一次的问题(与数组相反).

These should suffice for the start, and most common use cases. A proper library will extend this to generators so that values are passed through appropriately, and also will deal with the problem that iterators can be used only once before they are exhausted (in contrast to arrays).

这篇关于为什么生成器不支持 map()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆