Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generator #30

Open
BKJang opened this issue Nov 10, 2019 · 1 comment
Open

Generator #30

BKJang opened this issue Nov 10, 2019 · 1 comment
Labels
Basic of JS It is related to basic concept of JS.

Comments

@BKJang BKJang added the Basic of JS It is related to basic concept of JS. label Nov 10, 2019
@BKJang
Copy link
Owner Author

BKJang commented Nov 10, 2019

Generator

Generator๋Š” ES6์—์„œ ๋„์ž…๋˜์—ˆ์œผ๋ฉฐ Iterable์„ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. Generator๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด Iteration Protocol์„ ์‚ฌ์šฉํ•˜์—ฌ Iterable์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ์‹๋ณด๋‹ค ๊ฐ„ํŽธํ•˜๋‹ค.

Iteration Protocol์— ๋Œ€ํ•œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•˜๊ธธ ๋ฐ”๋ž€๋‹ค.

Generator ํ•จ์ˆ˜์˜ ์ •์˜

Generator ํ•จ์ˆ˜๋Š” ์ฝ”๋“œ ๋ธ”๋ก์„ ํ•œ ๋ฒˆ์— ์‹คํ–‰ํ•˜์ง€ ์•Š๊ณ  ํ•จ์ˆ˜ ์ฝ”๋“œ ๋ธ”๋ก์˜ ์‹คํ–‰์„ ์ค‘์ง€ํ–ˆ๋‹ค๊ฐ€ ํ•„์š”ํ•œ ์‹œ์ ์— ๋‹ค์‹œ ์‹œ์ž‘ํ•  ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜๋‹ค.

Generator ํ•จ์ˆ˜๋Š” function * ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉฐ ํ•˜๋‚˜ ์ด์ƒ์˜ yield ๋ฌธ์„ ํฌํ•จํ•œ๋‹ค.

// ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ
function* decFunc() {
  yield 1;
}

let genObj = decFunc();

// ํ•จ์ˆ˜ ํ‘œํ˜„์‹
const expFunc = function* () {
  yield 1;
};

genObj = expFunc();

// ๋ฉ”์„œ๋“œ
const obj = {
  * objectMethod() {
    yield 1;
  }
};

genObj = obj.objectMethod();

// ํด๋ž˜์Šค ๋ฉ”์„œ๋“œ
class GenClass {
  * classMethod() {
    yield 1;
  }
}

const genClass = new GenClass();
genObj = genClass.classMethod();

Generator ๊ฐ์ฒด

Generator ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์ฝ”๋“œ ๋ธ”๋ก์ด ์‹คํ–‰๋˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ Generator ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. Generator ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ๋™์‹œ์— ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋‹ค. ๋”ฐ๋ผ์„œ Symbol.iterator๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ดํ„ฐ๋ ˆ์ดํ„ฐ๋ฅผ ์ƒ์„ฑํ•  ํ•„์š” ์—†๋‹ค.

function* counter() {
  console.log('First');
  yield 1;
  console.log('Second');
  yield 2;
  console.log('Third');
  yield 3;
  console.log('The end');
}

const genObj = counter();

console.log(genObj.next()) //{value: 1, done: false}
console.log(genObj.next()) //{value: 2, done: false}
console.log(genObj.next()) //{value: 3, done: false}
console.log(genObj.next()) //{value: undefined, done: true}

Generator ๊ฐ์ฒด๋Š” ์ดํ„ฐ๋Ÿฌ๋ธ”์ด๋ฉด์„œ ์ดํ„ฐ๋ ˆ์ดํ„ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— next()๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ next() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด yield๋ฌธ๊นŒ์ง€ ์‹คํ–‰๋˜๊ณ  ์ผ์‹œ ์ค‘์ง€๋œ๋‹ค. ๋‹ค์‹œ next() ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ๋‹ค์Œ yield๋ฌธ์„ ๋งŒ๋‚  ๋•Œ๊นŒ์ง€ ์‹คํ–‰๋œ ๋’ค ์ผ์‹œ ์ค‘์ง€๋œ๋‹ค.

Generator ๊ฐ์ฒด๋ฅผ ์ด์šฉํ•œ ์ดํ„ฐ๋Ÿฌ๋ธ” ๊ตฌํ˜„

const genObj = (function* () {
  let i = 0;

  while(true) {
    yield ++i;
  }
}());

for (let item of genObj) {
  if (item === 10) break;
  console.log(item);
}
// Generator ํ•จ์ˆ˜์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ
const genObj = function* (max) {
  let i = 0;

  while(true) {
    if (i === max) break;
    yield ++i;
  }
}

for (let item of genObj(10)) {
  console.log(item);
}
// next ๋ฉ”์„œ๋“œ์— ํŒŒ๋ผ๋ฏธํ„ฐ ์ „๋‹ฌ
function* genFunc(n) {
  let res;
  res = yield n;

  console.log(res);
  res = yield res;

  console.log(res);
  res = yield res;

  console.log(res);
  return res;
}
const genObj = genFunc(0);

console.log(genObj.next());
console.log(genObj.next(1));
console.log(genObj.next(2));
console.log(genObj.next(3));

Generator๋ฅผ ์ด์šฉํ•œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ

Generator์˜ ์ง„๋ฉด๋ชฉ์€ ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰ ๋„์ค‘์— ๋ฉˆ์ถ˜๋‹ค๋‹ˆ. ์–ธ์ œ ์‘๋‹ต์ด ์˜ฌ์ง€ ์•Œ ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์—, callback์„ ๋“ฑ๋กํ•˜๋Š” ๋น„๋™๊ธฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ์‘์šฉํ•˜๋ฉด callback hell์„ ํƒˆ์ถœํ•  ์ˆ˜ ์žˆ์ง€ ์•Š์„๊นŒ?

function getId(phoneNumber) {
    // โ€ฆ
    iterator.next(result);
}

function getEmail(id) {
    // โ€ฆ
    iterator.next(result);
}

function getName(email) {
    // โ€ฆ
    iterator.next(result);
}

function order(name, menu) {
    // โ€ฆ
    iterator.next(result);
}
function* orderCoffee(phoneNumber) {
    const id = yield getId(phoneNumber);
    const email = yield getEmail(id);
    const name = yield getName(email);
    const result = yield order(name, 'coffee');
    return result;
}

const iterator = orderCoffee('010-1234-1234');
iterator.next();

Generator๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ์„๊นŒ?

// ES6
function* foo(){
    yield bar();
}

// ES5 Compiled
"use strict";

var _marked = /*#__PURE__*/ regeneratorRuntime.mark(foo);

function foo() {
    return regeneratorRuntime.wrap(
        function foo$(_context) {
            while (1) {
                switch ((_context.prev = _context.next)) {
                    case 0:
                        _context.next = 2;
                        return bar();
                    case 2:
                    case "end":
                        return _context.stop();
                }
            }
        },
        _marked, this
    );
}

Genrator๋Š” ๊ฒฐ๊ตญ iterable Protocol๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ์ฒด์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ”„๋กœํ† ์ฝœ๊ณผ ๊ด€๋ จ๋œ ์–ด๋Š๊ฒƒ๋„ ๋ณด์ด์ง€ ์•Š๋Š”๋‹ค.

๋Œ€์‹  regeneratorRuntime์ด ๋ณด์ธ๋‹ค.

babel์—์„œ๋Š” regeneratorRuntime๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ตฌํ˜„์„ ํ–ˆ๋‹ค.

์ฝ”๋“œ์˜ ์—ญ์‚ฌ๋ฅผ ๋”ฐ๋ผ๊ฐ€๋‹ค ๋ณด๋ฉด facebook/regenerator repository์— ๋„๋‹ฌํ•˜๊ฒŒ ๋œ๋‹ค.

์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” 2013๋…„ Node.js v0.11.2์—์„œ generator syntax๋ฅผ ์ง€์›ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์–ด ์กŒ์œผ๋ฉฐ, Babel์—์„œ๋Š” ์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ generator๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค. ์‹ค์ œ ์ฝ”๋“œ๋ฅผ ๋“ค์—ฌ๋‹ค๋ณด๋ฉด Symbol๊ณผ Iterator๋ฅผ ์ด์šฉํ•ด์„œ Iterable Protocol์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋‹ค.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Basic of JS It is related to basic concept of JS.
Projects
None yet
Development

No branches or pull requests

1 participant