ES6收率(收率1)(收率2)(收率3)()

时间:2022-06-17 05:07:55
function* generatorFunction() {
  yield (yield 1)(yield 2)(yield 3)();
}
var iterator = generatorFunction();

// [1, 2, 3]
var iteratedOver = [iterator.next().value, iterator.next().value, iterator.next().value];

I'm not sure how this works.

我不确定这是怎么回事。

yield doesn't return a function reference, so what are the parenthetical statements like (yield 2) doing - are they fat arrow anonymous functions without bodies? How are they called using partial application like that?

yield不返回函数引用,那么像(yield 2)那样的括号语句是什么 - 它们是没有实体的胖箭头匿名函数吗?他们如何使用这样的部分应用程序?

I'm missing something here, can someone explain?

我在这里遗漏了一些东西,有人可以解释一下吗?


Update: Tried on three browsers, Chrome 50.0.2661.86, Safari 9.1 (50.0.2661.86), Firefox 44.0.2, all perform without errors.

更新:在三个浏览器上试用,Chrome 50.0.2661.86,Safari 9.1(50.0.2661.86),Firefox 44.0.2,都可以正常运行。

ESFiddle also executes it without errors.

ESFiddle也可以毫无错误地执行它。

Commenters report Babel executes without errors as well.

评论者报告Babel也没有错误地执行。

The source of the question is from http://tddbin.com/#?kata=es6/language/generator/send-function, the second kata.

问题的来源是http://tddbin.com/#?kata=es6/language/generator/send-function,第二个kata。

1 个解决方案

#1


4  

I'm not sure how this works.

我不确定这是怎么回事。

Uh, yeah, it shouldn't work. It's only working because of a bug in Babel.

呃,是的,它应该不起作用。这只是因为Babel中的一个bug而起作用。

yield doesn't return a function reference, so what are the parenthetical statements like (yield 2) doing - are they fat arrow anonymous functions without bodies? How are they called using partial application like that?

yield不返回函数引用,那么像(yield 2)那样的括号语句是什么 - 它们是没有实体的胖箭头匿名函数吗?他们如何使用这样的部分应用程序?

No, it's really just standard function application, no magic. yield could return a function reference, and when it does this might work. When it doesn't, it will throw an exception on the third .next() call.

不,它只是标准的功能应用,没有魔力。 yield可以返回一个函数引用,当它发生时,这可能会起作用。如果没有,它将在第三个.next()调用上抛出异常。

As an example for a working version:

作为工作版本的示例:

function* generatorFunction() {
  yield (yield 1)(yield 2)(yield 3)();
}
var test = (a) => {
  console.log(a);
  return (b) => {
    console.log(b);
    return (c) => {
      console.log(c);
      return 4;
    };
  };
};
var iterator = generatorFunction();
iterator.next(); // {value: 1, done: false}
iterator.next(test); // {value: 2, done: false}
iterator.next("a"); // "a" {value: 3, done: false}
iterator.next("b"); // "b" undefined {value: 4, done: false}
iterator.next("d"); // {value: undefined, done: true}

So how does this work? Those nested/chained yield statements should better be written as

那怎么办?那些嵌套/链接的yield语句最好写成

function* generatorFunction() {
  let fn1 = yield 1;
  let a = yield 2;
  let fn2 = fn1(a);
  let b = yield 3;
  let fn3 = fn2(b);
  let res = fn3();
  let d = yield res;
  return undefined;
}

Commenters report Babel executes without errors as well.

评论者报告Babel也没有错误地执行。

That's because of a babel bug. If you check the transpiler output, it actually behaves like

那是因为一个巴贝尔虫。如果检查转换器输出,它实际上就像

function* generatorFunction() {
  let fn1 = yield 1;
  let a = yield 2;
  let b = yield 3;
  // these are no more executed with only 3 `next` calls
  let fn2 = fn1(a);
  let fn3 = fn2(b);
  let res = fn3();
  let d = yield res;
  return undefined;
}

#1


4  

I'm not sure how this works.

我不确定这是怎么回事。

Uh, yeah, it shouldn't work. It's only working because of a bug in Babel.

呃,是的,它应该不起作用。这只是因为Babel中的一个bug而起作用。

yield doesn't return a function reference, so what are the parenthetical statements like (yield 2) doing - are they fat arrow anonymous functions without bodies? How are they called using partial application like that?

yield不返回函数引用,那么像(yield 2)那样的括号语句是什么 - 它们是没有实体的胖箭头匿名函数吗?他们如何使用这样的部分应用程序?

No, it's really just standard function application, no magic. yield could return a function reference, and when it does this might work. When it doesn't, it will throw an exception on the third .next() call.

不,它只是标准的功能应用,没有魔力。 yield可以返回一个函数引用,当它发生时,这可能会起作用。如果没有,它将在第三个.next()调用上抛出异常。

As an example for a working version:

作为工作版本的示例:

function* generatorFunction() {
  yield (yield 1)(yield 2)(yield 3)();
}
var test = (a) => {
  console.log(a);
  return (b) => {
    console.log(b);
    return (c) => {
      console.log(c);
      return 4;
    };
  };
};
var iterator = generatorFunction();
iterator.next(); // {value: 1, done: false}
iterator.next(test); // {value: 2, done: false}
iterator.next("a"); // "a" {value: 3, done: false}
iterator.next("b"); // "b" undefined {value: 4, done: false}
iterator.next("d"); // {value: undefined, done: true}

So how does this work? Those nested/chained yield statements should better be written as

那怎么办?那些嵌套/链接的yield语句最好写成

function* generatorFunction() {
  let fn1 = yield 1;
  let a = yield 2;
  let fn2 = fn1(a);
  let b = yield 3;
  let fn3 = fn2(b);
  let res = fn3();
  let d = yield res;
  return undefined;
}

Commenters report Babel executes without errors as well.

评论者报告Babel也没有错误地执行。

That's because of a babel bug. If you check the transpiler output, it actually behaves like

那是因为一个巴贝尔虫。如果检查转换器输出,它实际上就像

function* generatorFunction() {
  let fn1 = yield 1;
  let a = yield 2;
  let b = yield 3;
  // these are no more executed with only 3 `next` calls
  let fn2 = fn1(a);
  let fn3 = fn2(b);
  let res = fn3();
  let d = yield res;
  return undefined;
}