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

rivolt0421Β·2023λ…„ 4μ›” 20일
1

πŸ“•μ±…

λͺ©λ‘ 보기
16/26

303p ~ 326p

μš”μ•½

πŸ˜‡
reduce()
: 배열을 ν•˜λ‚˜μ˜ κ°’μœΌλ‘œ "reduce"

// origin
function countAllPurchases(customers) {
  var total = 0;
  forEach(customers, function(cutomer) {
    total = total + customer.purchases.length;
  }
        );
};
  });
  return total;
}

// with reduce
function countAllPurchases(customers) {
  return reduce (customers, 0, function(total, customer) {
      return total + customer.purchases.length;
    }
  );
}

function reduce(array, init, f) {
  var accum = init;
  forEach(array, function(elem) {
    accum = f(accum, elem);
  });
  return accum;
}
  • ν˜„μž¬ λˆ„μ λœ κ°’κ³Ό λ°°μ—΄μ˜ ν˜„μž¬ ν•­λͺ©μ„ 가지고 μƒˆλ‘œμš΄ λˆ„μ κ°’μ„ 계산.
  • μƒˆλ‘œμš΄ λˆ„μ κ°’μ„ κΈ°μ‘΄ λˆ„μ κ°’μ΄ λ‹΄κΈ΄ λ³€μˆ˜μ— ν• λ‹Ή.

πŸ˜‡ reduce μ΄ˆκΈ°κ°’ μ£Όμ˜ν•  점

  1. 계산이 μ–΄λ–€ κ°’μ—μ„œ μ‹œμž‘λ˜λŠ”κ°€?
    • 예λ₯Ό λ“€μ–΄ λ”ν•˜κΈ°λŠ” 0, κ³±ν•˜κΈ°λŠ” 1.
  2. 배열이 λΉ„μ–΄μžˆλ‹€λ©΄ μ–΄λ–€ 값을 리턴할 것인가?
    • μ΄ˆκΈ°κ°’μœΌλ‘œ 이λ₯Ό κ²°μ •. 배열이 λΉ„μ–΄μžˆλ‹€λ©΄ forEach κ°€ 아무것도 ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ μ΄ˆκΈ°κ°’μ΄ κ·ΈλŒ€λ‘œ 리턴될 것.

πŸ˜‡ μ„Έ 가지 ν•¨μˆ˜ν˜• 도ꡬ 비ꡐ

  1. map
    : μ–΄λ–€ λ°°μ—΄μ˜ λͺ¨λ“  ν•­λͺ©μ— ν•¨μˆ˜λ₯Ό μ μš©ν•΄, μƒˆλ‘œμš΄ λ°°μ—΄λ‘œ λ°”κΎΌλ‹€.

  2. filter
    : μ–΄λ–€ λ°°μ—΄μ˜ ν•˜μœ„ 집합을 선택해 μƒˆλ‘œμš΄ λ°°μ—΄λ‘œ λ§Œλ“ λ‹€.

  3. reduce
    : μ–΄λ–€ λ°°μ—΄μ˜ ν•­λͺ©μ„ μ‘°ν•©ν•΄ μ΅œμ’…κ°’μ„ λ§Œλ“­λ‹ˆλ‹€. 데이터 μš”μ•½ or μ‹œν€€μŠ€λ₯Ό ν•˜λ‚˜μ˜ κ°’μœΌλ‘œ λ§Œλ“€ λ•Œ 주둜 μ‚¬μš©.

πŸ˜‡ ν•¨μˆ˜ν˜• 도ꡬ 체인
: 단계듀을 μ‘°ν•©ν•΄ ν•˜λ‚˜μ˜ 쿼리둜 λ§Œλ“œλŠ” 것. 이 κ³Όμ •μ—μ„œ ν•¨μˆ˜ν˜• 도ꡬλ₯Ό μ‚¬μš©ν•˜λŠ” 것.

πŸ˜‡ 체인 λͺ…ν™•ν•˜κ²Œ λ§Œλ“€κΈ° ➑️ "μ½œλ°±μ— 이름 뢙이기" λ₯Ό μ„ ν˜Έ.

  • 인라인 λŒ€μ‹  이름을 λΆ™μ—¬ μ½œλ°±μ„ μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ—,
    • μž¬μ‚¬μš©μ„±μ΄ μ’‹λ‹€.
    • 단계가 μ€‘μ²©λ˜λŠ” 것을 막을 수 μžˆλ‹€.
// μ›λž˜ μ½”λ“œ
function biggestPurchasesBestCustomers(customers) {
  var bestCustomers = filter(customers, function(customer) {	// 1단계
    return customer.purchases.length >= 3;
  });
  var biggestPurchases = map(bestCustomers, function(customer) {	// 2단계
    return maxKey(customer.purchases, {total: 0}, function(purchase) {
      return purchase.total;
    });
  });
  return biggestPurchases;
}

// 1. 단계에 이름 뢙이기
function biggestPurchasesBestCustomers(customers) {
  var bestCustomers    = selectBestCustomers(customers);		// 1단계
  var biggestPurchases = getBiggestPurchases(bestCustomers);	// 2단계
  return biggestPurchases;
}

function selectBestCustomers(customers) {
  return filter(customers, function(customer) {		// κ·ΈλŒ€λ‘œ 뢄리
    return customer.purchases.length >= 3;
  });
}

function getBiggestPurchases(customers) {		// κ·ΈλŒ€λ‘œ 뢄리
  return map(customers, getBiggestPurchase);
}

function getBiggestPurchase(customer) {		// κ·ΈλŒ€λ‘œ 뢄리
  return maxKey(customer.purchases, {total: 0}, function(purchase) {	// 쀑첩
    return purchase.total;												// 쀑첩
  });
}

// 2. μ½œλ°±μ— 이름 뢙이기
/// extracted and named callbacks
function biggestPurchasesBestCustomers(customers) {
  var bestCustomers    = filter(customers, isGoodCustomer);			// 이름뢙인 콜백 전달
  var biggestPurchases = map(bestCustomers, getBiggestPurchase);	// 이름뢙인 콜백 전달
  return biggestPurchases;
}

function isGoodCustomer(customer) {
  return customer.purchases.length >= 3;
}

function getBiggestPurchase(customer) {
  return maxKey(customer.purchases, {total: 0}, getPurchaseTotal);
}

function getPurchaseTotal(purchase) {
  return purchase.total;
}

발췌

λ©”λͺ¨

profile
I think I think too much.

2개의 λŒ“κΈ€

comment-user-thumbnail
2023λ…„ 4μ›” 23일

reduce만으둜 mapκ³Ό filterλ₯Ό λ§Œλ“€ 수 μžˆλ‹€λŠ” κ±Έ μ•„μ‹œλ‚˜μš”? μž¬λ°Œλ‹΅λ‹ˆλ‹€ πŸ˜€

1개의 λ‹΅κΈ€