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

Iteration Protocol #29

Open
BKJang opened this issue Oct 31, 2019 · 1 comment
Open

Iteration Protocol #29

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

Comments

@BKJang
Copy link
Owner

BKJang commented Oct 31, 2019

@BKJang BKJang added the Basic of JS It is related to basic concept of JS. label Oct 31, 2019
@BKJang BKJang changed the title Interation Protocol Iteration Protocol Oct 31, 2019
@BKJang
Copy link
Owner Author

BKJang commented Oct 31, 2019

Iteration Protocol

Iteration Protocol์€ ES6์—์„œ ๋„์ž…๋˜์—ˆ๋‹ค. ์ด๋Š” ์ƒˆ๋กœ์šด ๊ตฌ๋ฌธ์ด ์•„๋‹Œ ํ•˜๋‚˜์˜ ํ”„๋กœํ† ์ฝœ, ๊ทœ์•ฝ์ด๋‹ค. ์ฆ‰, ๊ฐ™์€ ๊ทœ์น™์„ ์ค€์ˆ˜ํ•˜๋Š” ๊ฐ์ฒด์— ์˜ํ•ด ๊ตฌํ˜„๋  ์ˆ˜ ์žˆ๋‹ค. Iteration Protocol์—๋Š” Iterable Protocol ๊ณผ Iterator Protocol ์ด ์žˆ๋‹ค.

Iterable

Iterable์€ Iterable Protocol์„ ์ค€์ˆ˜ํ•œ ๊ฐ์ฒด๋‹ค. Iterable์€ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•œ ๊ฐ์ฒด์ด๋ฉฐ Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜๊ฑฐ๋‚˜ ํ”„๋กœํ† ํƒ€์ž… ์ฒด์ธ์— ์˜ํ•ด ์ƒ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค. Iterable์€ for...of๋ฌธ์—์„œ ๋ฐ˜๋ณต ๊ฐ€๋Šฅํ•˜๋ฉฐ Spread Operator์˜ ๋Œ€์ƒ์ด ๋  ์ˆ˜ ์žˆ๋‹ค.

Iterable์€ Symbol.iterator์„ ๊ฐ€์ง€๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋ฉ”์„œ๋“œ๊ฐ€ ์—†๋Š” ๊ฐ์ฒด๋Š” Iterable ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋‹ค.

Iterable ๊ฐ์ฒด๋กœ๋Š” ๋‚ด์žฅ ๊ฐ์ฒด์ธ Array, Map, Set, String ๋“ฑ์ด ์žˆ๋‹ค.

const iterator = [1, 2, 3][Symbol.iterator]();

iterator.next().value; // 1
iterator.next().value; // 2
iterator.next().value; // 3
iterator.next().done; // true

๋ฐ˜๋ฉด, ์ผ๋ฐ˜ ๊ฐ์ฒด๋Š” Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋‹ค. ๋”ฐ๋ผ์„œ ์ผ๋ฐ˜ ๊ฐ์ฒด๋Š” Iterable ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋‹ค.

const obj = { a: 1, b: 2 };

console.log(Symbol.iterator in obj); // false

// TypeError: obj is not iterable
for (const item of obj) {
  console.log(item);
}

ํ•˜์ง€๋งŒ ์ผ๋ฐ˜ ๊ฐ์ฒด๋„ Iterable Protocol์„ ์ค€์ˆ˜ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋ฉด Iterable ๊ฐ์ฒด๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.

const iterableObj = function (max) {
  let i = 0;

  return {
    [Symbol.iterator]() {
      return {
        next() {
          return {
            value: ++i,
            done: i === max
          };
        }
      };
    }
  };
};

const iterator = iterableObj(10);

for (let item of iterator) {
  console.log(item);
}

Iterator

Iterator Protocol์€ next ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง„๋‹ค. next ๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด Iterable ๊ฐ์ฒด๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ value, done ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ–๋Š” Iterator Reuslt ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๊ทœ์•ฝ์„ ์ค€์ˆ˜ํ•œ ๊ฐ์ฒด๊ฐ€ Iterator ๊ฐ์ฒด๋‹ค.

์œ„์—์„œ ์ž ๊น ๋ดค๋˜ ์ฝ”๋“œ์˜ ์ผ๋ถ€๋ถ„์„ ๋‹ค์‹œ ํ•œ ๋ฒˆ ์‚ดํŽด๋ณด์ž.

[Symbol.iterator]() {
  return {
    next() {
      return {
        value: ++i,
        done: i === max
      };
    }
  };
}

Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด next ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  next ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด value, done ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง„ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค.
Iterable ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง„ Symbol.iterator ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•˜๋ฉด Iterator ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

const array = [1, 2, 3];

const iterator = array[Symbol.iterator]();

console.log('next' in iterator); // true

iterator.next().value; // 1
iterator.next().value; // 2
iterator.next().value; // 3
iterator.next(); // { value: undefined, done: true }

Iterator ๊ฐ์ฒด์˜ next ๋ฉ”์„œ๋“œ๊ฐ€ ๋ฐ˜ํ™˜ํ•˜๋Š” Iterator Result ๊ฐ์ฒด์˜ value ํ”„๋กœํผํ‹ฐ๋Š” Iterable ๊ฐ์ฒด์˜ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  done ํ”„๋กœํผํ‹ฐ๋Š” Iterable ๊ฐ์ฒด์˜ ๋ฐ˜๋ณต ์™„๋ฃŒ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

Iteration Protocol์ด ์™œ ํ•„์š”ํ• ๊นŒ?

for...of, Spread Operator, Destructuring, Map/Set constructor๋“ฑ์„ ๋ฐ์ดํ„ฐ ์†Œ๋น„์ž(Data Consumer) ๋ผ๊ณ  ํ•œ๋‹ค.
๋ฐ˜๋ฉด, ๋ฐฐ์—ด, ๋ฌธ์ž์—ด, Map, Set, DOM Data Structure ๋“ฑ๊ณผ ๊ฐ™์€ Iterable ๊ฐ์ฒด๋Š” ๋ฐ์ดํ„ฐ ๊ณต๊ธ‰์ž(Data Provider) ๋ผ๊ณ  ํ•œ๋‹ค.

๋งŒ์•ฝ, ์œ„์™€ ๊ฐ™์€ Data Provider์ธ Iterable ๊ฐ์ฒด๋“ค์ด ๊ฐ๊ฐ ๋‹ค๋ฅธ ๋ฐฉ์‹์˜ ์ˆœํšŒ ๋ฐฉ์‹์„ ๊ฐ–๋Š”๋‹ค๋ฉด ์–ด๋–จ๊นŒ? ๋‹น์—ฐํžˆ ํšจ์œจ์ ์ด์ง€ ๋ชปํ•˜๋‹ค.

์ˆœํšŒ ๋ฐฉ์‹์— ๋Œ€ํ•œ ํ•˜๋‚˜์˜ ๊ทœ์•ฝ์„ ์ •ํ•ด๋†“๊ณ  ์‚ฌ์šฉํ•œ๋‹ค๋ฉด Data Consumer๊ฐ€ ์—ฌ๋Ÿฌ ๊ตฌ์กฐ์˜ Iterable์„ ํšจ์œจ์ ์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ฆ‰, Iteration Protocol์€ Data Consumer์™€ Data Provider๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค ์—ญํ• ์„ ํ•ด์ฃผ๊ธฐ ๋•Œ๋ฌธ์— ํ•„์š”ํ•˜๋‹ค๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

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