πŸ“•[μ±…] 쏙쏙 λ“€μ–΄μ˜€λŠ” ν•¨μˆ˜ν˜• μ½”λ”© - 33,34

rivolt0421Β·2023λ…„ 4μ›” 27일
0

πŸ“•μ±…

λͺ©λ‘ 보기
19/26

371p ~ 394p

μš”μ•½

πŸ˜‡ "μ‘°νšŒν•˜κ³  λ³€κ²½ν•˜κ³  μ„€μ •ν•˜λŠ” 것을 update()둜 κ΅μ²΄ν•˜κΈ°" λ¦¬νŒ©ν„°λ§

/// Step 1: Identify get, modify, set

function halveField(item, field) {
  var value = item[field];		// get
  var newValue = value / 2;		// modify
  var newItem = objectSet(item, field, newValue);	// set
  return newItem;
}

/// Step 2: Replace with call to update()

function halveField(item, field) {
  return update(item, field, function(value) {
    return value / 2;
  });
}

πŸ˜‡ μ€‘μ²©λœ 데이터에 update() μ‚¬μš©ν•˜κΈ°

  • 같은 λ°©μ‹μœΌλ‘œ λ¦¬νŒ©ν„°λ§μ„ μ—¬λŸ¬λ²ˆ ν•˜λ©΄ λœλ‹€.
function incrementSize(item) {
  var options = item.options;	// 1. 쑰회
  var size = options.size;		// 2. 쑰회
  var newSize = size + 1;		// 2. λ³€κ²½
  var newOptions = objectSet(options, 'size', newSize);	// 2. μ„€μ • // 1. λ³€κ²½
  var newItem = objectSet(item, 'options', newOptions);	// 1. μ„€μ •
  return newItem;
}

/// Refactored

function incrementSize(item) {
  var options = item.options;	// 1. 쑰회
  var newOptions = update(options, 'size', increment);	// here // 1. λ³€κ²½
  var newItem = objectSet(item, 'options', newOptions);	// 1. μ„€μ •
  return newItem;
}

/// Refactored twice

function incrementSize(item) {
  return update(item, 'options', function(options) {	// here
    return update(options, 'size', increment);
  });
}

πŸ˜‡ update2() λ„μΆœν•˜κΈ°

  • μœ„μ˜ 결과와, 암묡적 인자λ₯Ό λ“œλŸ¬λ‚΄κΈ° λ¦¬νŒ©ν„°λ§μ„ μ΄μš©ν•΄,
    2번 μ€‘μ²©λœ 데이터에 μ‚¬μš© κ°€λŠ₯ν•œ update2()λ₯Ό λ„μΆœν•  수 μžˆλ‹€.
function incrementSize(item) {	// 1. "Size" -> (option)
  return update(item, 'options', function(options) {
    return update(options, 'size', increment);	// 1. "'size'" -> (option)
  });
}

/// 1. With explicit option argument

function incrementOption(item, option) {	// 2. "increment" -> (modify)
  return update(item, 'options', function(options) {
    return update(options, option, increment);	// 2. "increment" -> (modify)
  });
}

/// 2. With explicit modify argument

function updateOption(item, option, modify) {	// 3-1. "Option" -> (key1)	// 3-2. "option" -> (key2)
  return update(item, 'options', function(options) {	// 3-1. "'options'" -> (key1)		
    return update(options, option, modify);		// 3-2. "option" -> (key2)
  });
}

/// 3. With explicit argument

function update2(object, key1, key2, modify) {
  return update(object, key1, function(value1) {
    return update(value1, key2, modify);
  });
}

/// cc. Original

function incrementSize(item) {
  var options = item.options;
  var size = options.size;
  var newSize = size + 1;
  var newOptions = objectSet(options, 'size', newSize);
  var newItem = objectSet(item, 'options', newOptions);
  return newItem;
}

πŸ˜‡ update3() λ„μΆœν•˜κΈ°

  • μ—­μ‹œλ‚˜ 같은 방법.
function update3(object, key1, key2, key3, modify) {
  return update(object, key1, function(object2) {
    return update2(object2, key2, key3, modify);
  });
}

πŸ˜‡ nestedUpdate() λ„μΆœν•˜κΈ°

  • μž¬κ·€ 호좜 (recursive call)을 ν™œμš©ν•œλ‹€.
function nestedUpdate(object, keys, modify) {
  if(keys.length === 0)	// base case
    return modify(object);
  
  var key1 = keys[0];
  var restOfKeys = drop_first(keys);
  return update(object, key1, function(value1) {
    return nestedUpdate(value1, restOfKeys, modify);
  });
}

πŸ˜‡ 깊이 μ€‘μ²©λœ 데이터에 좔상화 λ²½ μ‚¬μš©ν•˜κΈ°

  • 깊이 μ€‘μ²©λœ ꡬ쑰에 nestedUpdate()λ₯Ό μ“°λ €λ©΄ κΈ΄ key 경둜 배열이 ν•„μš”ν•˜λ‹€.

  • 이것이 λ„ˆλ¬΄ κΈΈλ©΄ 쀑간에 μžˆλŠ” 객체듀이 μ–΄λ–€ ν‚€λ₯Ό κ°€μ‘ŒλŠ”μ§€, ꡬ쑰λ₯Ό κΈ°μ–΅ν•˜κΈ° μ–΄λ ΅λ‹€.

  • 예λ₯Ό λ“€μ–΄, μ•„λž˜μ™€ 같은 μ½”λ“œκ°€ μžˆλ‹€.

    httpGet("http://my-blog.com/api/category/blog", function(blogCategory) {
      renderCategory(nestedUpdate(blogCategory, ['posts', '12', 'author', 'name'], capitalize));
    });

    api둜 κ°€μ Έμ˜¨ κ°’ blogCategory λ₯Ό μ½œλ°±μ„ 톡해 μ²˜λ¦¬ν•˜λŠ” μ½”λ“œμ΄λ‹€.
    이 μ½”λ“œλŠ” λ‹€μŒκ³Ό 같은 이해λ₯Ό ν•¨μΆ•ν•˜κ³  μžˆλ‹€.

    1. blogCategoryλŠ” posts key μ•„λž˜ "λΈ”λ‘œκ·Έ κΈ€"을 λ‹΄κ³  μžˆλŠ” ꡬ쑰이닀.
    2. 각 "λΈ”λ‘œκ·Έ κΈ€"은 ID(12) λ₯Ό 톡해 μ ‘κ·Όν•  수 μžˆλ‹€.
    3. "λΈ”λ‘œκ·Έ κΈ€"은 author key μ•„λž˜ "글쓴이"λ₯Ό λ‹΄κ³  μžˆλŠ” ꡬ쑰이닀.
    4. "글쓴이"λŠ” name key μ•„λž˜ "μ‚¬μš©μž 이름"을 λ‹΄κ³  μžˆλŠ” ꡬ쑰이닀.

    경둜만 보고, 이 λͺ¨λ“  정보λ₯Ό κΈ°μ–΅ν•˜κΈ°λŠ” 쉽지 μ•Šλ‹€.

  • κ·Έλž˜μ„œ, 좔상화 벽을 λ§Œλ“€κ³  μ˜λ―ΈμžˆλŠ” 이름을 λΆ™μ—¬, κ΅¬ν˜„μ„ 감좔고 κΈ°μ–΅ν•΄μ•Ό ν•  점을 쀄일 수 μžˆλ‹€.

  • λ‹€μ‹œ 말해, 좔상화 벽에 μ‘΄μž¬ν•˜λŠ” ν•¨μˆ˜ μžμ‹ μ΄ ν•΄λ‹Ή κ΅¬ν˜„μ„ 본문에 ν’ˆκ³  있으면 λœλ‹€.

    1. μ£Όμ–΄μ§„ ID둜 "λΈ”λ‘œκ·Έ κΈ€"을 λ³€κ²½ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“ λ‹€.
    function updatePostById(category, id, modifyPost) {
      return nestedUpdate(category, ['posts', id], modifyPost);	// modifyPost // λΈ”λ‘œκ·Έ κΈ€ ꡬ쑰에 λŒ€ν•΄μ„œλŠ” μ½œλ°±μ— λ§‘κΈ΄λ‹€. -> 2.
      				     // key 경둜 배열에 ν•¨μΆ•λœ category의 κ΅¬μ‘°λŠ” 좔상화 λ²½ λ’€λ‘œ μˆ¨κΈ΄λ‹€.
                         // 즉, 이 ν•¨μˆ˜ μžμ‹ μ΄ μ—¬κΈ°(λ³Έλ¬Έ)에 ν’ˆλŠ”λ‹€.
    }
    1. 글쓴이λ₯Ό μˆ˜μ •ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“ λ‹€.
    function updateAuthor(post, modifyUser) {
      return update(post, 'author', modifyUser);	// modifyUser // μ‚¬μš©μž ꡬ쑰에 λŒ€ν•΄μ„œλŠ” μ½œλ°±μ— λ§‘κΈ΄λ‹€. -> 3.
                 // λΈ”λ‘œκ·Έ κΈ€μ˜ κ΅¬μ‘°λŠ” 이 ν•¨μˆ˜ μžμ‹ μ΄ μ—¬κΈ°(λ³Έλ¬Έ)에 ν’ˆλŠ”λ‹€.
    }
    1. μ‚¬μš©μž 이름을 λŒ€λ¬Έμžλ‘œ λ°”κΎΈλŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“ λ‹€.
    function capitalizeName(user) {
      return update(user, 'name', capitalize);
                 // μ‚¬μš©μžμ˜ κ΅¬μ‘°λŠ” 이 ν•¨μˆ˜ μžμ‹ μ΄ μ—¬κΈ°(λ³Έλ¬Έ)에 ν’ˆλŠ”λ‹€.    
    }
    1. ν•©μΉ˜κΈ°
    updatePostById(blogCategory, '12', function(post) {
      return updateAuthor(post, capitalizeUserName);
    });
  • κΈ°μ–΅ν•΄μ•Ό ν•  사항이 간단해 μ‘Œλ‹€.

    • category μ•ˆμ— λΈ”λ‘œκ·Έ 글이 μžˆλ‹€λŠ” μ‚¬μ‹€λ§Œ μ•Œμ•„λ„ λœλ‹€. μ–΄λ–€ 킀에 μ–΄λ–»κ²Œ λ“€μ–΄μžˆλŠ”μ§€ λͺ°λΌλ„ λœλ‹€.
    • λΈ”λ‘œκ·Έ 글에 글쓴이가 ν•˜λ‚˜ μžˆλ‹€λŠ” μ‚¬μ‹€λ§Œ μ•Œμ•„λ„ λœλ‹€. μ–΄λ–€ 킀에 μ–΄λ–»κ²Œ λ“€μ–΄μžˆλŠ”μ§€ λͺ°λΌλ„ λœλ‹€.

πŸ˜‡ κ³ μ°¨ ν•¨μˆ˜ 청리

  • λ°°μ—΄ 처리
    • forEach(), map(), filter(), reduce()
  • μ€‘μ²©λœ 데이터 처리
    • update(), nestedUpdate()
  • copy-on-write 원칙 적용
    • withArrayCopy(), withObjectCopy()
  • try-catch λ‘œκΉ… κ·œμΉ™ μ½”λ“œν™”
    • wrapLogging()

발췌

λ©”λͺ¨

λ‹€μŒ μ‹œκ°„λΆ€ν„΄ λ“œλ””μ–΄ λΆ„μ‚° μ‹œμŠ€ν…œμ— λŒ€ν•΄μ„œ μ•Œμ•„λ³΄μž.

profile
I think I think too much.

0개의 λŒ“κΈ€