๐ŸŽ„ Tree Mthod - Map

๋งˆ๋ฐ์Šพ : My Dev Spaceยท2019๋…„ 12์›” 19์ผ
0

Reference ๋ถ„์„

๋ชฉ๋ก ๋ณด๊ธฐ
1/1

Tree Map Mathod

Tree์˜ ๋ฉ”์†Œ๋“œ์ธ map์„ ๊ตฌํ˜„ํ•˜๋Š”๋ฐ ์‹œ๊ฐ„์ด ์˜ค๋ž˜ ๊ฑธ๋ ธ๋‹ค..
map ๋ฉ”์†Œ๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ํŠน์„ฑ์„ ๊ฐ€์ง„๋‹ค.
1. ๊ธฐ์กด ๊ฐ์ฒด์™€ ๊ฐ™์€ ๊ตฌ์กฐ์ด๋˜ ๊ฐ์ฒด์˜ ๊ฐ’๋“ค์€ callback ํ•จ์ˆ˜์— ์ˆœ์„œ๋Œ€๋กœ ๋„ฃ์–ด ์‹คํ–‰์‹œํ‚จ ๊ฐ’์ด์–ด์•ผ ํ•œ๋‹ค.
- ๊ธฐ์กด ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’์„ ์ฐธ์กฐํ•˜๋ฉด ์•ˆ๋จ => ์–‡์€ ๋ณต์‚ฌ(shallow copy)
- ์ƒˆ๋กœ์šด ๊ณต๊ฐ„์— ๊ธฐ์กด ๊ฐ์ฒด์˜ ์ฃผ์†Œ๊ฐ’๊ณผ ์ƒ๊ด€์—†๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์•ผํ•จ => ๊นŠ์€ ๋ณต์‚ฌ(deep copy)
2. ๊ธฐ์กด ๊ฐ์ฒด๋Š” ์ˆ˜์ •๋˜๋ฉด ์•ˆ๋œ๋‹ค.
3. ์ƒˆ๋กœ ๋งŒ๋“ค์–ด์ง„ ๊ฐ์ฒด๋“ค์€ Tree Class์˜ instance์—ฌ์•ผ ํ•œ๋‹ค.

1๋ฒˆ์„ ๊ตฌํ˜„ํ•˜๊ธฐ ์œ„ํ•ด deep copy๋ฅผ ๊ตฌ๊ธ€๋งํ•˜์—ฌ ์ฐพ์•„๋ณด์•˜๋‹ค.
JSON.stringify๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ์ฒด๋ฅผ ๋ฌธ์ž๋กœ ๋ฐ”๊พธ๊ณ , JSON.parse๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ž๋ฅผ ๊ฐ์ฒด๋กœ ๋‹ค์‹œ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋ณต์‚ฌํ•œ ๊ฐ์ฒด์™€ ์—ฐ๊ฒฐ์ด ๋Š๊ธด๋‹ค๊ณ ํ•œ๋‹ค.

  • JSON.parse(JSON.stringify(this))

๋“œ๋””์–ด ์ด์ œ ๋ฌธ์ œ๋ฅผ ํ‘ธ๋Š”๊ฐ€์‹ถ์–ด ๊ธฐ์œ๋งˆ์Œ์œผ๋กœ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์™€ ๊ฐ™์ด ์ž‘์„ฑํ•˜์˜€๋‹ค.

let cloneObj = new Tree();
Object.assign(cloneObj, JSON.parse(JSON.stringify(this)));

๊ทธ๋Ÿฐ๋ฐ..๋ฃจํŠธ ๋…ธ๋“œ์˜ constructor๋งŒ Tree๋กœ ์„ค์ •๋˜์žˆ๊ณ , ์ž์‹๋…ธ๋“œ๋“ค์€ Object๋กœ ๋˜์–ด์žˆ๋‹ค..
image.png

assign์„ ํ†ตํ•ด Tree์˜ Instance์— JSON.parse(JSON.stringify(this))๋ฅผ ์—ฐ๊ฒฐํ•ด์ฃผ๋ฉด Prototype์ด ๋ชจ๋“  ๋…ธ๋“œ์— ์ƒ์†๋˜๋Š”์ค„ ์•Œ์•˜๋Š”๋ฐ.. ๋ฃจํŠธ ๋…ธํŠธ์—๋งŒ ์ƒ์†๋œ๋‹ค..

๋ชจ๋“  ๋…ธ๋“œ๋ฅผ ์ˆœํšŒํ•˜๋ฉด์„œ Tree์˜ Instance๋กœ ๋ฐ”๊ฟ”์ฃผ๋ ค๋ฉด ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ•˜๋Š”์ง€..๋„๋ฌด์ง€ ๊ฐ์ด ์•ˆ์žกํžŒ๋‹ค.

๊ทธ๋ž˜์„œ ๊ฒฐ๊ตญ ๋ช‡์‹œ๊ฐ„์„ ๊ณ ๋ฏผํ•˜๋ฉฐ ์‹œ๋„ํ•ด๋ณด๋‹ค๊ฐ€.. ๊ฒฐ๊ตญ ๋‹ต์ง€๋ฅผ ๋ณด์•˜๋‹ค.. ใ…œ

reference ์ฝ”๋“œ ์ „๋ฌธ์ด๋‹ค.

var Tree = function(value) {
  this.value = value;
  this.children = [];
};

Tree.prototype.addChild = function(child) {
  if (!child || !(child instanceof Tree)) {
    child = new Tree(child);
  }
  this.children.push(child);
  // return the tree for convenience
  return this;
};

Tree.prototype.map = function(callback) {
  return this.children.reduce(function(tree, child) {
    return tree.addChild(child.map(callback));
  }, new Tree(callback(this.value)));
};

์ž ์ด์ œ ๋‚ด๊ฐ€ ๊ณ ๋ฏผํ•˜๊ณ  ๊ณ ๋ฏผํ–ˆ๋˜ map ๋ฉ”์†Œ๋“œ๋ฅผ ๋ถ„์„ํ•ด๋ณด์ž.

Tree.prototype.map = function(callback) {
  return this.children.reduce(function(tree, child) {
    return tree.addChild(child.map(callback));
  }, new Tree(callback(this.value)));
};

๐Ÿ˜Š reference ๋ถ„์„

  1. ๋ฃจํŠธ ๋…ธ๋“œ์˜ ์ž์‹๋…ธ๋“œ๊ฐ€ ๋‹ด๊ธด ๋ฐฐ์—ด์˜ ๊ฐ ์ž์‹๋…ธ๋“œ์— ๋ฆฌ๋“€์Šค๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
  2. ์ด๋•Œ ์ดˆ๊ธฐ๊ฐ’(initial value) ์€ ๋ฃจํŠธ ๋…ธ๋“œ์˜ value๋ฅผ ์ธ์ž๋กœ ๋ฐ›์€ callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋…ธ๋“œ์ด๋‹ค.
  3. ๊ฐ๊ฐ์˜ ์ž์‹๋…ธ๋“œ์— ๋ฆฌ๋“€์Šค๋ฅผ ์‹คํ–‰ํ•˜๋ฉด
    1. ์ฒซ๋ฒˆ์งธ๋กœ ํ˜„์žฌ ์ž์‹๋…ธ๋“œ์˜ ์ž์‹๋…ธ๋“œ๋ฐฐ์—ด์˜ ๊ฐ ์ž์‹ ์š”์†Œ์— ๋ฆฌ๋“€์Šค ๋ฉ”์†Œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.
    2. ์ด๋•Œ ์ดˆ๊ธฐ๊ฐ’(initial value)์€ ํ˜„์žฌ ์ž์‹ ๋…ธ๋“œ์˜ value๋ฅผ ์ธ์ž๋กœ ๋ฐ›์€ callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ๊ฒฐ๊ณผ๊ฐ’์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋…ธ๋“œ์ด๋‹ค.
    3. ๋งŒ์•ฝ ํ˜„์žฌ ์ž์‹๋…ธ๋“œ์˜ ์ž์‹๋…ธ๋“œ๋ฐฐ์—ด์ด ๋นˆ๋ฐฐ์—ด์ด๋ผ๋ฉด reduce๋ฅผ ์‹คํ–‰ํ•ด ์ค„ ์š”์†Œ๋“ค์ด ์—†๊ธฐ๋•Œ๋ฌธ์— ์ดˆ๊ธฐ๊ฐ’๋งŒ ๋ฐ˜ํ™˜ํ•˜์—ฌ map ๋ฉ”์†Œ๋“œ๋ฅผ ์ข…๋ฃŒํ•˜๊ณ  ๋น ์ ธ๋‚˜์˜จ๋‹ค.
  4. ํ˜„์žฌ ์ดˆ๊ธฐ๊ฐ’(initial value)์— 3๋ฒˆ์—์„œ ์–ป์€ ๋ฐ˜ํ™˜๊ฐ’์ธ ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋…ธ๋“œ๋ฅผ ์ธ์ž๋กœ ๋„ฃ๊ณ  addChild ๋ฉ”์„œ๋“œ๋ฅผ ์ ์šฉ์‹œํ‚จ๋‹ค.
  5. addChild ๋ฉ”์„œ๋“œ๋ฅผ ์‹คํ–‰ํ•œ ํ˜„์žฌ ์ดˆ๊ธฐ๊ฐ’(initial value)์ด this๊ฐ€ ๋˜๊ณ  ์ด this์˜ children์— 3๋ฒˆ์—์„œ ์–ป์€ ๋ฐ˜ํ™˜๊ฐ’์ธ ์ƒˆ๋กœ์šด ํŠธ๋ฆฌ๋…ธ๋“œ๋ฅผ pushํ•œ๋‹ค.
  6. ๊ทธ๋ฆฌ๊ณ  this๋ฅผ ๋ฐ˜ํ™˜ ํ•œ๋‹ค.
  7. addChild ๋ฉ”์„œ๋“œ ์‹คํ–‰์ด ๋๋‚˜๊ณ  reduce๋กœ ๋Œ์•„์˜ค๋ฉด ๋ฐ˜ํ™˜๋œ this๊ฐ€ ๋‹ค์Œ ์ž์‹ ์š”์†Œ์— reduce๋ฅผ ์‹คํ–‰ํ• ๋•Œ ๋ˆ„์ ๊ฐ’(accumulate)์ด ๋œ๋‹ค.
var identity = function(value) {
  return value;
};
var input = new Tree(1);
input.addChild(2);
input.addChild(3);

var result = input.map(identity);

map method ์‹คํ–‰์ˆœ์„œ

  • ํ˜„์žฌ input = {value : 1, children : [{value : 2, children : []}, {value : 3, children : []}]}
  1. map ๋ฉ”์†Œ๋“œ ์‹คํ–‰
  2. ์ฒซ๋ฒˆ์งธ ๋ˆ„์ ๊ฐ’ = Tree {value : 1, children : []}
  3. ๋‘๋ฒˆ์งธ ๋ˆ„์ ๊ฐ’ = Tree {value : 1, children : [{value : 2, children : []}]}
  4. ์„ธ๋ฒˆ์งธ ๋ˆ„์ ๊ฐ’ = Tree {value : 1, children : [{value : 2, children : []}, {value : 3, children : []}]}

๐Ÿ˜’ ๋‚˜์˜ ๋ฌธ์ œ์ 

  • ๋‚˜๋Š” ๊ณ„์† ์ด๋ฏธ ๋งŒ๋“ค์–ด์ ธ ์žˆ๋Š” ๊ฐ์ฒด์ž์ฒด๋ฅผ ์ „๋ถ€ ๋ณต์‚ฌํ•ด์„œ ๊ทธ ์ƒํƒœ์—์„œ ํ”„๋กœํ† ํƒ€์ž…์„ Tree๋กœ ๋ฐ”๊ฟ”์ค„ ์ƒ๊ฐ๋งŒ ๊ณ„์† ํ–ˆ๋‹ค..
  • ํ•œ๊ฐ€์ง€ ๋ฐฉ๋ฒ•๋งŒ ์ƒ๊ฐํ•œ๋‹ค
  • callback ํ•จ์ˆ˜๋ฅผ ์žฌ๊ท€ํ•จ์ˆ˜ ์•ˆ์—์„œ ์‹คํ–‰์‹œ์ผœ return ํ•ด์ฃผ๋ ค๊ณ  ํ•˜๋‹ˆ ๊ณ„์† for๋ฌธ์ด loop๋ฅผ ๋Œ์ง€์•Š๊ณ  callbackํ•จ์ˆ˜๋ฅผ ํ•œ๋ฒˆ ๋Œ๊ณ  return์œผ๋กœ ๋Š๊ฒจ ๋ฒ„๋ฆฌ๋‹ˆ ๋Œ€์ฒด ์–ด๋–ป๊ฒŒํ•˜๋ฉด ์žฌ๊ท€ํ•จ์ˆ˜์ฒ˜๋Ÿผ loop๋ฅผ ๋„๋Š” ์ฝ”๋“œ ์•ˆ์—์„œ callback์„ returnํ• ๊นŒ.. ์ด ์ƒ๊ฐ๋งŒ ๊ณ„์†ํ–ˆ๋‹ค. callback ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰์‹œ์ผœ ๋ฐ˜ํ™˜๋œ ๊ฐ’์„ ์ด์šฉํ•ด ์ƒˆ๋กœ์šด ์ธ์Šคํ„ด์Šค ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค๋ฉด ๋˜๋Š” ๊ฒƒ์„..! callback ํ•จ์ˆ˜๊ฐ€ ํ•ด๋‹น ์ฝ”๋“œ์—์„œ ์–ด๋– ํ•œ ์—ญํ• ์„ ํ•˜๋Š”์ง€ ์ œ๋Œ€๋กœ ํŒŒ์•…์„ ๋ชปํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ์ž˜๋ชป๋œ ๋ฐฉํ–ฅ์œผ๋กœ ์ ‘๊ทผํ•œ ๊ฒƒ ๊ฐ™๋‹ค. ๋‹ค์Œ๋ถ€ํ„ฐ๋Š” ํ…Œ์ŠคํŠธ์ฝ”๋“œ๋ฅผ ํ†ตํ•ด ๋ฉ”์†Œ๋“œ๋‚˜ callback ํ•จ์ˆ˜์˜ ์‚ฌ์šฉ ์˜๋„๋ฅผ ํ™•์‹คํ•˜๊ฒŒ ํŒŒ์•…ํ•œ ํ›„ ์ฝ”๋“œ์ž‘์„ฑ์— ๋“ค์–ด๊ฐ€์•ผ๊ฒ ๋‹ค.
profile
๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป ๐Ÿš€

0๊ฐœ์˜ ๋Œ“๊ธ€