diff --git a/Chapter_12/README.md b/Chapter_12/README.md new file mode 100644 index 0000000..0981986 --- /dev/null +++ b/Chapter_12/README.md @@ -0,0 +1,238 @@ +## ๐Ÿ 12์žฅ ํ•จ์ˆ˜ํ˜• ๋ฐ˜๋ณต + +**์ด๋ฒˆ์žฅ์—์„œ ์‚ดํŽด๋ณผ ๋‚ด์šฉ** + +1. ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ map(), filter(), reduce() ์— ๋Œ€ํ•ด ๋ฐฐ์›๋‹ˆ๋‹ค. + +2. ๋ฐฐ์—ด์— ๋Œ€ํ•œ ๋ฐ˜๋ณต๋ฌธ์„ ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ๋กœ ๋ฐ”๊พธ๋Š” ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ด…๋‹ˆ๋‹ค. + +3. ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ๋ฅผ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜๋Š”์ง€ ์•Œ์•„๋ด…๋‹ˆ๋‹ค. + +### ์ด์ „ ๋‚ด์šฉ ํ•œ๋ฒˆ ๋” ์ •๋ฆฌ + +1. ์ฝ”๋“œ ๋ƒ„์ƒˆ: ํ•จ์ˆ˜ ์ด๋ฆ„์— ์žˆ๋Š” ์•”๋ฌต์  ์ธ์ž + ํŠน์ง•: ๊ฑฐ์˜ ๋˜‘๊ฐ€ํŒ… ๊ตฌํ˜„๋œ ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค. / ํ•จ์ˆ˜ ์ด๋ฆ„์ด ๊ตฌํ˜„์— ์žˆ๋Š” ๋‹ค๋ฅธ ๋ถ€๋ถ„์„ ๊ฐ€๋ฅดํ‚จ๋‹ค. + +2. ๋ฆฌํŒฉํ„ฐ๋ง1 - ์•”๋ฌต์  ์ธ์ž๋ฅผ ๋“œ๋Ÿฌ๋‚ด๊ธฐ + ๋ฆฌํŒฉํ„ฐ๋ง์€ ์•”๋ฌต์  ์ธ์ž๊ฐ€ ์ผ๊ธ‰ ๊ฐ’์ด ๋˜๋„๋ก ํ•จ์ˆ˜์— ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. + [๋‹จ๊ณ„] + 1 - ํ•จ์ˆ˜ ์ด๋ฆ„์— ์žˆ๋Š” ์•”๋ฌต์  ์ธ์ž๋ฅผ ํ™•์ธ + 2 - ๋ช…์‹œ์ ์ธ ์ธ์ž๋ฅผ ์ถ”๊ฐ€ + 3 - ํ•จ์ˆ˜ ๋ณธ๋ถ„์— ํ•˜๋“œ ์ฝ”๋”ฉ๋œ ๊ฐ’์„ ์ƒˆ๋กœ์šด ์ธ์ž๋กœ ๋ณ€๊ฒฝ + 4 - ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ณณ์„ ์ˆ˜์ • + +3. ๋ฆฌํŒฉํ„ฐ๋ง2 - ํ•จ์ˆ˜ ๋ณธ๋ฌธ์„ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ + 1 - ํ•จ์ˆ˜ ๋ณธ๋ฌธ์—์„œ ๋ฐ”๊ฟ€ ๋ถ€๋ถ„์˜ ์•ž๋ถ€๋ถ„๊ณผ ๋’ท๋ถ€๋ถ„์„ ํ™•์ธ + 2 - ๋ฆฌํŒฉํ„ฐ๋ง ํ•  ์ฝ”๋“œ๋ฅผ ํ•จ์ˆ˜๋กœ ๋นผ๋ƒ„ + 3 - ๋นผ๋‚ธ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ๋„˜๊ธธ ๋ถ€๋ถ„์„ ๋˜ ๋‹ค๋ฅธ ํ•จ์ˆ˜๋กœ ๋นผ๋ƒ„ + +### ์ƒˆ๋กœ์šด ์š”์ฒญ : ์ฟ ํฐ ์ด๋ฉ”์ผ ์ฒ˜๋ฆฌ + +[3์žฅ์— ๋‚˜์™”๋˜ ์ฝ”๋“œ] ๋ฅผ ๋ฆฌํŒฉํ„ฐ๋ง ํ•ด๋ณด์ž + +```js +function emailsForCustomers(customers, goods, bests) { + var emails = []; + for (var i = 0; i < customers.length; i++) { + var customer = customers[i]; + var email = emailForCustomer(customer, goods, bests); + emails.push(email); + } + return emails; +} +``` + +1. ์ผ๋‹จ `forEach` ๋กœ ๋ฐ˜๋ณต๋ฌธ์„ ๋ณ€๊ฒฝํ•ด๋ด…๋‹ˆ๋‹ค. + +```js +function emailsForCustomers(customers, goods, bests) { + var emails = []; + forEach(customers, function (customer) { + var email = emailForCustomer(customer, goods, bests); + emails.push(email); + }); + return emails; +} +``` + +2. `map` ํ•จ์ˆ˜๋ฅผ ๋„์ถœํ•˜๊ธฐ + ๋‹ค๋ฅธ ์ฝ”๋“œ๋“ค๋„ ์‚ดํŽด๋ด…๋‹ˆ๋‹ค. + +```js +function emailsForCustomers(customers, goods, bests) { + var emails = []; + forEach(customers, function (customer) { + var email = emailForCustomer(customer, goods, bests); + emails.push(email); + }); + return emails; +} +function biggestPurchasePerCustomer(customers) { + var purchases = []; + forEach(customers, function (customer) { + var purchase = biggestPurchase(customer); + purchases.push(purchase); + }); + return purchases; +} +``` + +์ง€๋‚œ ์žฅ์—์„œ ๋ฐฐ์šด ๊ฒƒ ์ฒ˜๋Ÿผ ์•ž๋ถ€๋ถ„ ๋ณธ๋ฌธ ๋’ท๋ถ€๋ถ„์„ ๊ตฌ๋ถ„ํ•ด์„œ ๋ณธ๋ฌธ์„ ์ฝœ๋ฐฑ๋นผ๋‚ด๊ณ  ๋ณ€๊ฒฝํ•ด๋ด…๋‹ˆ๋‹ค! + +![Alt text](image.png) + +```js +function map(array, f) { + var newArray = []; + forEach(array, function (element) { + newArray.push(f(element)); + }); + return newArray; +} +``` + +### ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ: map() + +```js +// ๋ฐฐ์—ด๊ณผ ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Œ +function map(array, f) { + var newArray = []; // ๋นˆ ๋ฐฐ์—ด + forEach(array, function (element) { + newArray.push(f(element)); + // ์›๋ž˜ ๋ฐฐ์—ด ํ•ญ๋ชฉ์œผ๋กœ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ํ•ญ๋ชฉ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด f( ) ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฅธ๋‹ค. + // ์›๋ž˜ ๋ฐฐ์—ด ํ•ญ๋ชฉ์— ํ•ด๋‹นํ•˜๋Š” ์ƒˆ๋กœ์šด ํ•ญ๋ชฉ์„ ์ถ”๊ฐ€ + }); + return newArray; + // ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ๋ฆฌํ„ด +} +``` + +![Alt text](image-1.png) + +์ „๋‹ฌํ•จ์ˆ˜๋Š” ์ต๋ช…ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ธ๋ผ์ธ์œผ๋กœ ์ •์˜ํ•จ. +ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•  ๋•Œ ์ธ์ž์˜ ์ด๋ฆ„์„ ํ†ตํ•ด ์ธ์ž๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค. + +```js +function emailsForCustomers(customers, goods, bests) { + return map(customers, function (customer) { + return emailForCustomer(customer, goods, bests); + }); +} +``` + +1. ์ต๋ช… ํ•จ์ˆ˜: ์ด๋ฆ„์ด ์—†๋Š” ํ•จ์ˆ˜. + ์ต๋ช… ํ•จ์ˆ˜๋Š” ํ•„์š”ํ•œ ๊ณณ์— ์ธ๋ผ์ธ์œผ๋กœ ์ •์˜ํ•จ + +2. ์ธ๋ผ์ธ ํ•จ์ˆ˜ : ์ด๋ฆ„์„ ๋ถ™์—ฌ์“ฐ๋Š” ๋Œ€์‹  ์“ฐ๋Š” ๊ณณ์—์„œ ๋ฐ”๋กœ ์ •์˜ํ•˜๋Š” ํ•จ์ˆ˜ + ( = ์ธ์ž๋ฅผ ๋„˜๊ธฐ๋Š” ๊ณณ์—์„œ ๋ฐ”๋กœ ์ •์˜ํ•œ๋‹ค๋ฉด ์ธ๋ผ์ธ ํ•จ์ˆ˜๋‹ค.) + +![Alt text](image-2.png) + +### ํ•จ์ˆ˜๋ฅผ ์ „๋‹ฌํ•˜๋Š” ์„ธ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• + +#### ์ „์—ญ์œผ๋กœ ์ •์˜ํ•˜๊ธฐ + +```js +function greet(name) { + return 'Hello, ' + name; +} +var friendGreetings = map(friendsNames, greet); +``` + +#### ์ง€์—ญ์ ์œผ๋กœ ์ •์˜ํ•˜๊ธฐ + +```js +function greetEverybody(friends) { + var greeting; + if (language === 'English') greeting = 'Hello, '; + else greeting = 'Salut, '; + var greet = function (name) { + return greeting + name; + }; + return map(friends, greet); +} +``` + +#### ์ธ๋ผ์ธ์œผ๋กœ ์ •์˜ํ•˜๊ธฐ + +```js +var friendGreetings = map(friendsNames, function (name) { + return 'Hello, ' + name; +}); +``` + +### ์ƒˆ๋กœ์šด ์š”์ฒญ: ์šฐ์ˆ˜ ๊ณ ๊ฐ ๋ชฉ๋ก + +์—ฌ๊ธฐ์—๋„ map() ์„ ์‚ฌ์šฉํ•˜๋ฉด ๋ ๊นŒ? +-> map ์€ ์ฃผ์–ด์ง„ ๋ฐฐ์—ด๊ณผ ๊ธธ์ด๊ฐ€ ๊ฐ™์€ ๋ฐฐ์—ด์„ ๋ฆฌํ„ดํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ ์ ˆํ•˜์ง€ ์•Š์Œ. + +์—ฌ๊ธฐ์„œ๋Š” ์šฐ์ˆ˜ ๊ณ ๊ฐ๋“ค๋งŒ ๊ฐ€์ ธ์™€์•ผํ•จ. + +![Alt text](image-3.png) + +```js +function selectBestCustomers(customers){ + return filter(customers, function(customer)){ + return customer.purchases.length > = 3; // ํ‘œํ˜„์‹์„ ํ•จ์ˆ˜๋กœ ๋นผ์„œ ์ธ์ž๋กœ ์ „๋‹ฌํ•จ + } +} +``` + +### ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ: filter() + +๋ฐฐ์—ด์—์„œ ์ผ๋ถ€ ํ•ญ๋ชฉ์„ ์„ ํƒํ•˜๋Š” ํ•จ์ˆ˜ + +```js +function filter(array, f) { + var newArray = []; + forEach(array, function (element) { + if (f(element)) newArray.push(element); + }); + return newArray; +} +``` + +#### ์ฃผ์˜ + +๊ฒฐ๊ณผ ๋ฐฐ์—ด์— null ์ด ๋ฐœ์ƒํ•  ์ˆ˜๋„ ์žˆ์Œ. + +```js +var allEmails = map(customers, function (customer) { + return customer.email; +}); +var emailsWithoutNulls = filter(emailsWithNulls, function (email) { + return email !== null; +}); +``` + +### ์ƒˆ๋กœ์šด ์š”์ฒญ : ๋ชจ๋“  ๊ณ ๊ฐ์˜ ์ „์ฒด ๊ตฌ๋งค ์ˆ˜ + +### reduce + +๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ ๊ฐ’์„ ๋ˆ„์ ํ•ด๊ฐ. + +```js +function reduce(array, init, f) { + // ๋ฐฐ์—ด๊ณผ ์ดˆ๊นƒ๊ฐ’, ๋ˆ„์  ํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›์Œ +ย ย ย ย var accum = init; +// ๋ˆ„์ ๋œ๊ฐ’์„ ์ดˆ๊ธฐํ™” +ย ย ย ย forEach(array, function(element) { +ย ย ย ย ย ย ย ย accum = f(accum, element); +// ๋ˆ„์  ๊ฐ’์„ ๊ณ„์‚ฐํ•˜๊ธฐ ์œ„ํ•ด ํ˜„์žฌ ๊ฐ’๊ณผ ๋ฐฐ์—ด ํ•ญ๋ชฉ์œผ๋กœ +f() ํ•จ์ˆ˜๋ฅผ ๋ถ€๋ฆ„ +ย ย ย ย }); +ย ย ย ย return accum; +// ๋ˆ„์ ๋œ ๊ฐ’์„ ๋ฆฌํ„ด +} +``` + +![Alt text](image-6.png) + +### ์„ธ๊ฐ€์ง€ ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ ๋น„๊ต + +1. Map + ์–ด๋–ค ๋ฐฐ์—ด์˜ ๋ชจ๋“  ํ•ญ๋ชฉํ—ค ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋ณ€๊ฒฝ +2. filter + ์–ด๋–ค ๋ฐฐ์—ด์˜ ํ•˜์œ„ ์ง‘ํ•ฉ์„ ์„ ํƒํ•ด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด๋กœ ๋งŒ๋“ฌ +3. reduce + ์–ด๋–ค ๋ฐฐ์—ด์˜ ํ•ญ๋ชฉ์„ ์กฐํ•ฉํ•ด ์ตœ์ข…๊ฐ’์œผ๋กœ ๋งŒ๋“ ๋‹ค. diff --git a/Chapter_12/image-1.png b/Chapter_12/image-1.png new file mode 100644 index 0000000..33af51b Binary files /dev/null and b/Chapter_12/image-1.png differ diff --git a/Chapter_12/image-2.png b/Chapter_12/image-2.png new file mode 100644 index 0000000..02ea2db Binary files /dev/null and b/Chapter_12/image-2.png differ diff --git a/Chapter_12/image-3.png b/Chapter_12/image-3.png new file mode 100644 index 0000000..d431a2c Binary files /dev/null and b/Chapter_12/image-3.png differ diff --git a/Chapter_12/image-4.png b/Chapter_12/image-4.png new file mode 100644 index 0000000..7e5cf57 Binary files /dev/null and b/Chapter_12/image-4.png differ diff --git a/Chapter_12/image-5.png b/Chapter_12/image-5.png new file mode 100644 index 0000000..e2a2414 Binary files /dev/null and b/Chapter_12/image-5.png differ diff --git a/Chapter_12/image-6.png b/Chapter_12/image-6.png new file mode 100644 index 0000000..eddbd11 Binary files /dev/null and b/Chapter_12/image-6.png differ diff --git a/Chapter_12/image.png b/Chapter_12/image.png new file mode 100644 index 0000000..c6c8336 Binary files /dev/null and b/Chapter_12/image.png differ diff --git a/Chapter_14/README.md b/Chapter_14/README.md new file mode 100644 index 0000000..2a30ba5 --- /dev/null +++ b/Chapter_14/README.md @@ -0,0 +1,344 @@ +## ๐Ÿ 14์žฅ ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ์— ํ•จ์ˆ˜ํ˜• ๋„๊ตฌ ์‚ฌ์šฉํ•˜๊ธฐ + +**์ด๋ฒˆ์žฅ์—์„œ ์‚ดํŽด๋ณผ ๋‚ด์šฉ** + +1. ํ•ด์‹œ ๋งต์— ์ €์žฅ๋œ ๊ฐ’์„ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๊ณ ์ฐจ ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ฌ. + +2. ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๊ณ ์ฐจ ํ•จ์ˆ˜๋กœ ์‰ฝ๊ฒŒ ๋‹ค๋ฃจ๋Š” ๋ฐฉ๋ฒ•์„ ๋ฐฐ์›€. + +3. ์žฌ๊ท€๋ฅผ ์ดํ•ดํ•˜๊ณ  ์•ˆ์ „ํ•˜๊ฒŒ ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ดํŽด๋ด„. + +4. ๊นŠ์ด ์ค‘์ฒฉ๋œ ์—”ํ‹ฐํ‹ฐ์— ์ถ”์ƒํ™” ๋ฒฝ์„ ์ ์šฉํ•ด์„œ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์„ ์ดํ•ด. + +### ๊ฐ์ฒด๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•œ ๊ณ ์ฐจ ํ•จ์ˆ˜ + +#### ํ•„๋“œ๋ช…์„ ๋ช…์‹œ์ ์œผ๋กœ ๋งŒ๋“ค๊ธฐ + +์›๋ž˜ ๋ƒ„์ƒˆ๊ฐ€ ๋‚˜๋˜ ์ฝ”๋“œ + +```js +function incrementQuantity(item) { + var quantity = item['quantity']; + var newQuantity = quantity + 1; + var newItem = objectSet(item, 'quantity', newQuantity); + return newItem; +} +``` + +์ธ์ž๋กœ ๋ฐ”๊พผ ์ฝ”๋“œ +(๋ช…์‹œ์ ์ธ ์ธ์ž๋กœ ๋ณ€๊ฒฝ) + +```js +function incrementField(item, field) { + var value = item[field]; + var newValue = value + 1; + var newItem = objectSet(item, field, newValue); + return newItem; +} +``` + +#### update() ๋„์ถœํ•˜๊ธฐ + +๋‘ ๊ฐ€์ง€๋ฅผ ๋ฆฌํŒฉํ„ฐ๋งํ•ด์•ผํ•œ๋‹ค. + +1. ํ•จ์ˆ˜ ์ด๋ฆ„์— ์žˆ๋Š” ์•”๋ฌต์ ์ธ ์ธ์ž๋Š” ์•”๋ฌต์  ์ธ์ž๋ฅผ ๋“œ๋Ÿฌ๋‚ด๊ธฐ ๋ฆฌํŒฉํ„ฐ๋ง์œผ๋กœ ๋™์ž‘ ์ด๋ฆ„์„ ๋ช…์‹œ์ ์ธ ์ธ์ž๋กœ ๋ณ€๊ฒฝ. + -> ๊ทผ๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”๊ฟ”์•ผ ํ•  ์ธ์ž๊ฐ€ ์ผ๋ฐ˜๊ฐ’์ด ์•„๋‹ˆ๊ณ  ๋™์ž‘์ด๊ธฐ ๋•Œ๋ฌธ์— + **ํ•จ์ˆ˜ ๋ณธ๋ฌธ์„ ์ฝœ๋ฐฑ์œผ๋กœ ๋ฐ”๊พธ๊ธฐ** ๋ฆฌํŒฉํ„ฐ๋ง์œผ๋กœ ๋™์ž‘์„ ํ•จ์ˆ˜ ์ธ์ž๋กœ ๋ฐ›๋„๋ก ํ•ด์•ผํ•จ. + +```js +function incrementField(item, field) { + var value = item[field]; + var newValue = value + 1; + var newItem = objectSet(item, field, newValue); + return newItem; +} +``` + +```js +function incrementField(item, field) { + return updateField(item, field, function (value) { + return value + 1; + }); +} + +function updateField(item, field, modify) { + var value = item[field]; + var newValue = modify(value); + var newItem = objectSet(item, field, newValue); + + return newItem; +} +``` + +๋ชจ๋“  ๋™์ž‘์„ ๊ณ ์ฐจ ํ•จ์ˆ˜ ํ•˜๋‚˜๋กœ ํ•ฉ์นจ. +๋ฐ”๊พธ๊ณ  ์‹ถ์€ ํ•„๋“œ์™€ ๋™์ž‘์„ ์ฝœ๋ฐฑ์œผ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋จ. + +๋‹ค๋งŒ, ํŠน์ • ํ•„๋“œ๋ฅผ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜ ์ด๋ฆ„์— field๋Š” ๋นผ๊ณ  ์ผ๋ฐ˜์ ์ธ ์ด๋ฆ„์ธ update()๋กœ ๋ณ€๊ฒฝ! + +```js +function update(object, key, modify) { + var value = object[key]; + var newValue = modify(value); + var newObject = objectSet(object, key, newValue); + return newObject; +} +``` + +**Q. update( ) ๊ฐ€ ์›๋ณธ ํ•ด์‹œ ๋งต์„ ๋ณ€๊ฒฝํ•˜๋Š”์ง€?** +์›๋ณธ ํ•ด์‹œ ๋งต์„ ๋ณ€๊ฒฝํ•˜์ง€ ์•Š์Œ. +์›๋ž˜ ํ•ด์‹œ ๋งต์˜ ๋ณต์‚ฌ๋ณธ์„ ๋ณ€๊ฒฝํ•ด์„œ ๋ฆฌํ„ด + +### ๊ฐ์ฒด์— ์žˆ๋Š” ๊ฐ’์„ ์‹œ๊ฐํ™” + +update()๊ฐ€ ๋™์ž‘ํ•˜๋Š” ๋ฐฉ์‹์„ ์‹œ๊ฐํ™”ํ•ด๋ณด์ž. + +```js +var shoes = { + name: 'shoes', + quantity: 3, + price: 7, +}; +``` + +```js +function update(object, key, modify) { + // 1. ์กฐํšŒ + var value = object[key]; + // 2. ๋ณ€๊ฒฝ + var newValue = modify(value); + // 3. ์„ค์ • + var newObject = objectSet(object, key, newValue); + return newObject; +} +``` + +#### ๋‹จ๊ณ„1: ํ‚ค๋ฅผ ๊ฐ€์ง€๊ณ  ๊ฐ์ฒด์—์„œ ๊ฐ’์„ ์กฐํšŒ + +![Alt text](image.png) + +#### ๋‹จ๊ณ„2: ํ˜„์žฌ ๊ฐ’์œผ๋กœ Modify()๋ฅผ ๋ถˆ๋Ÿฌ ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ƒ์„ฑ + +![Alt text](image-1.png) + +#### ๋‹จ๊ณ„3: ๋ณต์‚ฌ๋ณธ์„ ์ƒ์„ฑ + +![Alt text](image-2.png) + +### ์ค‘์ ‘๋œ ๋ฐ์ดํ„ฐ์— update() ์‚ฌ์šฉํ•˜๊ธฐ + +```js +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; +} +``` + +**์กฐํšŒํ•˜๊ณ  ๋ณ€๊ฒฝํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ update()๋กœ ๊ต์ฒดํ•˜๊ธฐ์˜ ๋‹จ๊ณ„** + +1. ์กฐํšŒํ•˜๊ณ  ๋ณ€๊ฒฝํ•˜๊ณ  ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์ฐพ๋Š”๋‹ค. +2. ๋ฐ”๊พธ๋Š” ๋™์ž‘์„ ์ฝœ๋ฐฑ์œผ๋กœ ์ „๋‹ฌํ•ด์„œ update()๋กœ ๊ต์ฒดํ•œ๋‹ค. + +**ํ•œ ๋ฒˆ ๋ฆฌํŒฉํ„ฐ๋งํ•œ ์ฝ”๋“œ** + +```js +function incrementSize(item) { + var options = item.options; + var newOptions = update(options, 'size', increment); + var newItem = objectSet(item, 'options', newOptions); + return newItem; +} +``` + +**๋‘ ๋ฒˆ ๋ฆฌํŒฉํ„ฐ๋งํ•œ ์ฝ”๋“œ** + +```js +function incrementSize(item) { + return update(item, 'options', function (options) { + return update(options, 'size', increment); + }); +} +``` + +update() ๋ฅผ ์ค‘์ฒฉํ•ด์„œ ๋ถ€๋ฅด๋ฉด ๋” ๊นŠ์€ ๋‹จ๊ณ„๋กœ ์ค‘์ฒฉ๋œ ๊ฐ์ฒด๋„ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค... +(\_> ์ด ๋‹จ๊ณ„์˜ ์ƒ๊ฐ์— ๋„๋‹ฌํ•˜๊ธฐ๊ฐ€ ์•„์ง๊นŒ์ง€๋Š”..์–ด๋ ค์šธ๋“ฏ) + +### ๊ทผ๋ฐ ๋‘ ๋ฒˆ ๋ฆฌํŒฉํ„ฐ๋งํ•œ ์ฝ”๋“œ์—์„œ ๋ƒ„์ƒˆ๊ฐ€ ๋‚œ๋‹ค..?? + +```js +function incrementSize(item) { + return update(item, 'options', function (options) { + return update(options, 'size', increment); + }); +} +``` + +์•”๋ฌต์ ์ธ์ž๊ฐ€ ์žˆ์Œ! +๋ช…์‹œ์ ์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ฃผ์ž! + +```js +function update2(object, key1, key2, modify) { + return update(object, key1, function (value1) { + return update(value1, key2, modify); + }); +} +``` + +**์•”๋ฌต์  ์ธ์ž๋ฅผ ๋“œ๋Ÿฌ๋‚ด๊ธฐ ๋ฆฌํŒฉํ„ฐ๋ง ๋‹จ๊ณ„** + +1. ํ•จ์ˆ˜ ์ด๋ฆ„์— ์žˆ๋Š” ์•”๋ฌต์  ์ธ์ž๋ฅผ ํ™•์ธ +2. ๋ช…์‹œ์ ์ธ ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. +3. ํ•จ์ˆ˜ ๋ณธ๋ฌธ์— ํ•˜๋“œ ์ฝ”๋”ฉ๋œ ๊ฐ’์„ ์ƒˆ๋กœ์šด ์ธ์ž๋กœ ๋ณ€๊ฒฝ. +4. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ณณ์„ ๊ณ ์นจ. + +### ๋ฌธ์ œ + +์˜ต์…˜ ์•ˆ์— ๋ฐ”๊ฟ”์•ผ ํ•  ๊ฐ์ฒด๊ฐ€ ์žˆ๊ณ  ์˜ต์…˜์€ ์ œํ’ˆ ๊ฐ์ฒด ์•ˆ์— ์žˆ์Œ. +์ œํ’ˆ ๊ฐ์ฒด๋Š” ๋˜ ์žฅ๋ฐ”๊ตฌ๋‹ˆ ๊ฐ์ฒด ์•ˆ์— ์žˆ๋Š” ๊ฒƒ์ด ๋ฌธ์ œ + +์ค‘์ฒฉ์ค‘์ฒฉ์ค‘์ฒฉ์ด ๋ฌธ์ œ + +```js +var cart = { + shirt: { + name: 'shirt', + price: 13, + options: { + color: 'blue', + size: 3, + }, + }, +}; +``` + +์ด ํ•จ์ˆ˜๋ฅผ `incrementSizeByName` ์œผ๋กœ ํ•˜์ž! + +### incrementSizeByName() ์„ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ• + +#### ์˜ต์…˜1: update()์™€ incrementSize()๋กœ ๋งŒ๋“ค๊ธฐ + +```js +function incrementSizeByName(cart, name) { + return update(cart, name, incrementSize); +} +``` + +#### ์˜ต์…˜2: update()์™€ update2() + +update2()๋ฅผ ์‚ฌ์šฉํ•ด incrementSize()๋ฅผ ์ธ๋ผ์ธ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. + +```js +function incrementSizeByName(cart, name) { + return update(cart, name, function (item) { + return update2(item, 'options', 'size', function (size) { + return size + 1; + }); + }); +} +``` + +#### ์˜ต์…˜3: update()๋กœ ๋งŒ๋“ค๊ธฐ + +```js +function incrementSizeByName(cart, name) { + return update(cart, name, function (item) { + return update(item, 'options', function (options) { + return update(options, 'size', function (size) { + return size + 1; + }); + }); + }); +} +``` + +#### ์˜ต์…˜4: ์กฐํšŒํ•˜๊ณ  ๋ฐ”๊พธ๊ณ  ์„ค์ •ํ•˜๋Š” ๊ฒƒ์„ ์ง์ ‘ ๋งŒ๋“ค๊ธฐ + +```js +function incrementSizeByName(cart, name) { + var item = cart[name]; + var options = item.options; + var size = options.size; + var newSize = size + 1; + var newOptions = objectSet(options, 'size', newSize); + var newItem = objectSet(item, 'options', newOptions); + var newCart = objectSet(cart, name, newItem); + return newCart; +} +``` + +### ๊ฒฐ๊ตญ 14์žฅ์—์„œ ํ•ต์‹ฌ: ์žฌ๊ท€ ํ•จ์ˆ˜ + +```js +function nestedUpdate(object, keys, modify) { + // ์ข…๋ฃŒ ์กฐ๊ฑด(๊ฒฝ๋กœ์˜ ๊ธธ์ด๊ฐ€ 0์ผ๋•Œ) + if (keys.length === 0) return modify(object); + var key1 = keys[0]; + // ์ข…๋ฃŒ ์กฐ๊ฑด์— ๊ณ„์† ๊ฐ€๊นŒ์›Œ์ง(ํ•ญ๋ชฉ์„ ํ•˜๋‚˜์”ฉ ์—†์•ฐ) + var restOfKeys = drop_first(keys); + return update(object, key1, function (value1) { + // ์žฌ๊ท€ ํ˜ธ์ถœ + return nestedUpdate(value1, restOfKeys, modify); + }); +} +``` + +**Q. ์–ด๋–ป๊ฒŒ ํ•จ์ˆ˜๊ฐ€ ์ž๊ธฐ ์ž์‹ ์„ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ๋Š”์ง€?** + +A. ํ•จ์ˆ˜๋Š” ์ž์‹ ์„ ํฌํ•จํ•ด์„œ ์–ด๋–ค ํ•จ์ˆ˜๋ผ๋„ ๋ถ€๋ฅผ ์ˆ˜ ์žˆ์Œ. +์ž์‹ ์„ ๋ถ€๋ฅผ ๋•Œ ์žฌ๊ท€๋ผ๊ณ  ํ•จ. + +### ์•ˆ์ „ํ•œ ์žฌ๊ท€ ์‚ฌ์šฉ๋ฒ• + +์žฌ๊ท€๊ณ  for / while ๋ฐ˜๋ณต๋ฌธ ์ฒ˜๋Ÿผ ๋ฌดํ•œ ๋ฐ˜๋ณต์— ๋น ์งˆ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Œ + +1. ์ข…๋ฃŒ ์กฐ๊ฑด + ์ข…๋ฃŒ ์กฐ๊ฑด์„ ํ•„์š”๋กœ ํ•จ +2. ์žฌ๊ท€ ํ˜ธ์ถœ + ์ตœ์†Œ ํ•˜๋‚˜์˜ ์žฌ๊ท€ ํ˜ธ์ถœ์ด ํ•„์š” +3. ์ข…๋ฃŒ ์กฐ๊ฑด์— ๋‹ค๊ฐ€๊ฐ€๊ธฐ + +```js +function nestedUpdate(object, keys, modify) { + // ์ข…๋ฃŒ ์กฐ๊ฑด(๊ฒฝ๋กœ์˜ ๊ธธ์ด๊ฐ€ 0์ผ๋•Œ) + if (keys.length === 0) return modify(object); + var key1 = keys[0]; + // ์ข…๋ฃŒ ์กฐ๊ฑด์— ๊ณ„์† ๊ฐ€๊นŒ์›Œ์ง(ํ•ญ๋ชฉ์„ ํ•˜๋‚˜์”ฉ ์—†์•ฐ) + var restOfKeys = drop_first(keys); + return update(object, key1, function (value1) { + // ์žฌ๊ท€ ํ˜ธ์ถœ + return nestedUpdate(value1, restOfKeys, modify); + }); +} +``` + +### ๊นŠ์ด ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ์— ์ถ”์ƒํ™” ๋ฒฝ ์‚ฌ์šฉํ•˜๊ธฐ + +- ์ถ”์ƒํ™”๋ฒฝ์€ ๊ตฌํ˜„์„ ๊ฐ์ถ”๊ธฐ ๋•Œ๋ฌธ์— ํ•จ์ˆ˜๋ฅผ ์“ธ ๋•Œ ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”์ง€๋Š” ๋ชฐ๋ผ๋„ ๋จ. + +๊นŠ์ด ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋„ˆ๋ฌด ๋งŽ์€ ๊ฒƒ์„ ๊ธฐ์–ตํ•ด์•ผํ•˜๋Š” ์–ด๋ ค์›€์ด ์žˆ๋‹ค. + +```js +// ๋ถ„๋ฅ˜์— ์žˆ๋Š” ๋ธ”๋กœ๊ทธ ๊ธ€์ด ์–ด๋–ค +// ๊ตฌ์กฐ์ธ์ง€๋Š” ๋ชฐ๋ผ๋„ ํ•จ์ˆ˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ. +function updatePostById(category, id, modifyPost) { + return nestedUpdate(category, ['posts', id], modifyPost); + // ['posts', id] ๋ถ„๋ฅ˜์˜ ๊ตฌ์กฐ ๊ฐ™์€ + // ๊ตฌ์ฒด์ ์ธ ๋ถ€๋ถ„์€ ์ถ”์ƒํ™” ๋ฒฝ ๋’ค๋กœ ์ˆจ๊น€ +} +``` + +### ์š”์  ์ •๋ฆฌ + +1. ๋ณดํ†ต ์ผ๋ฐ˜์ ์ธ ๋ฐ˜๋ณต๋ฌธ์€ ์žฌ๊ท€๋ณด๋‹ค ๋ช…ํ™•ํ•˜์ง€๋งŒ, ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃฐ ๋•Œ๋Š” ์žฌ๊ท€๊ฐ€ ๋” ์‰ฝ๊ณ  ๋ช…ํ™•ํ•˜๋‹ค. + +2. ์žฌ๊ท€๋Š” ์Šค์Šค๋กœ ๋ถˆ๋ €๋˜ ๊ณณ์ด ์–ด๋””์ธ์ง€ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์Šคํƒ์„ ์‚ฌ์šฉ. + ์žฌ๊ท€ํ•จ์ˆ˜์—์„œ ์Šคํƒ์€ ์ค‘์ฒฉ๋œ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜์˜ํ•จ. diff --git a/Chapter_14/image-1.png b/Chapter_14/image-1.png new file mode 100644 index 0000000..dbb56fb Binary files /dev/null and b/Chapter_14/image-1.png differ diff --git a/Chapter_14/image-2.png b/Chapter_14/image-2.png new file mode 100644 index 0000000..966ab7f Binary files /dev/null and b/Chapter_14/image-2.png differ diff --git a/Chapter_14/image.png b/Chapter_14/image.png new file mode 100644 index 0000000..ab8bc27 Binary files /dev/null and b/Chapter_14/image.png differ