코드스피츠73 part3_2
ITERATION & GENERATOR
ES6 LOOP 지연루프
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 
 | const loop = (iter, f) => {
 
 if(typeof iter[Symbol.iterator] == 'function'){
 iter = iter[Symbol.iterator]();
 }
 
 if(typeof iter.next != 'function') return;
 
 while (true){
 const v = iter.next();
 if(v.done) return;
 f(v.value);
 }
 
 };
 
 const iter = {
 [Symbol.iterator](){return this;},
 arr : [1,2,3,4],
 next() {
 return {
 
 value : this.arr.pop(),
 
 done : this.arr.length === 0
 };
 }
 };
 
 loop(iter, console.log);
 
 
 
 
 
 
 | 
위에 loop 함수를 사용하지 않고 내장 반복처리기들
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 
 | const iter = {
 [Symbol.iterator](){return this;},
 data : [1,2,3,4],
 next() {
 return {
 
 done : this.data.length === 0,
 
 value : this.data.pop()
 
 };
 }
 };
 
 
 const [a,...b] = iter;
 
 
 
 
 
 
 
 
 console.log(a,b);
 
 
 
 | 
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | const iter = {
 [Symbol.iterator](){return this;},
 data : [1,2,3,4],
 next() {
 return {
 
 done : this.data.length === 0,
 
 value : this.data.pop()
 
 };
 }
 };
 
 const a =[...iter];
 
 console.log(a);
 
 
 
 | 
배열을 전달 하는것 보다 객체를 전달하는것은 통제권을 객체안에서 가지고 올수 있다.
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 
 | 
 const iter = {
 [Symbol.iterator](){return this;},
 data : [1,2,3,4],
 next() {
 return {
 
 done : this.data.length === 0,
 
 value : this.data.pop()
 
 };
 }
 };
 
 const test = (...arg)=> console.log(arg);
 test(...iter);
 
 
 
 | 
위에 펼치기와 비슷 하지만 함수 내부에서 해체되어 들어온다.
- 함수 선언시에 사용되면 Rest Parameter
- 함수가 아닌곳에서 사용하면 Spread
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 
 | function() {
 
 arguments
 
 location
 
 this
 }
 
 () => {
 
 arguments
 location
 this
 }
 
 
 | 
보다명확하게 단일한 규칙으로 표현하기 위해서 위처럼 지원한다.(ES6)
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 
 | 
 const cls = function() {
 this.base = arguments;
 }
 
 new cls(1,2);
 new cls(1,2,3);
 
 const arr = [1,2,3,4,5,6,7,8];
 
 new cls();
 
 
 
 const cls = function(...arg){
 this.base = arg;
 }
 
 new cls(...arr);
 
 
 | 
FOR OF
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 
 | const iter = {
 [Symbol.iterator](){return this;},
 data : [1,2,3,4],
 next() {
 return {
 
 done : this.data.length === 0,
 
 value : this.data.pop()
 
 };
 }
 };
 
 
 for (const v of iter){
 console.log(v);
 }
 
 
 
 
 
 
 | 
연습 문제
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 
 | const N2 = class{
 constructor(max){
 this.max = max
 
 },
 [Symbol.iterator](){
 let cursor = 0, max = this.max;
 return {
 done : false,
 next(){
 if(cursor > max){
 this.done = true;
 }else{
 this.value = cursor * cursor;
 cursor ++;
 }
 return this;
 }
 };
 }
 };
 
 
 console.log([... new N2(5)]);
 
 for (const v of new N2(5)){
 console.log(v);
 }
 
 
 | 
LOOP를 실행 할때 메모리에 값을 가지고 실행 했는데 지금은 함수에서 값을 가지고 있어서 지연 실행이 가능
Generator
위에 함수를 generator로 재구현
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 
 | const generator = function*(max) {
 let cursor = 0;
 while (cursor < max){
 yield cursor * cursorl
 cursor++;
 }
 };
 
 
 console.log([... generator(5));
 
 for (const v of generator(5)){
 console.log(v);
 }
 
 
 | 
그런데 while을 썼는데 멈추었다. 이건 코루틴에 기초이다.
generator 블록은 코루틴이라서 함수에서 지연호출이 가능함
현대 모던 언어에서는 코루틴을 가지고 있다.
오늘도 최고의 강의 였습니다.
감사합니다. ㅜㅜ
참조