본문으둜 κ±΄λ„ˆλ›°κΈ°

🐀 Chapter 5: λ°°μ—΄κ³Ό νŠœν”Œ

πŸ¦„ λ°°μ—΄ μ΄ν•΄ν•˜κΈ°β€‹

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ 배열은 Array 클래슀의 μΈμŠ€ν„΄μŠ€μ΄λ‹€.
let array = new Array;
array.push(1);
array.push(2);
array.push(3);
console.log(array); // [1, 2, 3]
  • 배열에 λ‹΄κΈ΄ 각각 값을 μ•„μ΄ν…œ λ˜λŠ” μ›μ†ŒλΌκ³  ν•œλ‹€.

πŸ“š [] 단좕 ꡬ문​

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” []λΌλŠ” 단좕 ꡬ문을 μ œκ³΅ν•œλ‹€.
let numbers = [1, 2, 3];
let strings = ['Hello', 'World'];
console.log(numbers, strings); // [1, 2, 3] ['Hello', 'World']

πŸ“š μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ 배열은 객체닀​

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ 배열은 객체이닀.
  • 배열은 Array 클래슀의 μΈμŠ€ν„΄μŠ€μΈλ°, 클래슀의 μΈμŠ€ν„΄μŠ€λŠ” 객체이기 λ•Œλ¬Έμ΄λ‹€.
  • Array.isArrayλŠ” λ§€κ°œλ³€μˆ˜λ‘œ 전달받은 μ‹¬λ²Œμ΄ 배열인지 객체인지 μ•Œλ €μ€€λ‹€.
let a = [1, 2, 3];
let o = { name: 'Jack', age: 32 };
console.log(Array.isArray(a), Array.isArray(o)); // true false

πŸ“š λ°°μ—΄μ˜ νƒ€μž…β€‹

  • νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œ λ°°μ—΄μ˜ νƒ€μž…μ€ μ•„μ΄ν…œ νƒ€μž…[]이닀. 예λ₯Ό λ“€μ–΄, λ°°μ—΄μ˜ μ•„μ΄ν…œμ΄ number νƒ€μž…μ΄λ©΄ λ°°μ—΄μ˜ νƒ€μž…μ€ number[]이고, μ•„μ΄ν…œμ΄ string νƒ€μž…μ΄λ©΄ string[]이닀.
let numArray: number[] = [1, 2, 3];
let strArray: string[] = ['Hello', 'World'];

type IPerson = { name: string, age?: number };
let personArray: IPerson[] = [
{ name: 'Jack' },
{ name: 'Jane', age: 32 },
];

// [ { name: 'Jack' }, { name: 'Jane', age: 32 } ]

πŸ“š λ¬Έμžμ—΄κ³Ό λ°°μ—΄ κ°„ λ³€ν™˜β€‹

  • νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” 문자 νƒ€μž…μ΄ μ—†κ³  λ¬Έμžμ—΄μ˜ λ‚΄μš© λ˜ν•œ λ³€κ²½ν•  수 μ—†λ‹€. μ΄λŸ¬ν•œ νŠΉμ§• λ•Œλ¬Έμ— λ¬Έμžμ—΄μ„ κ°€κ³΅ν•˜λ €λ©΄ λ¨Όμ € λ¬Έμžμ—΄μ„ λ°°μ—΄λ‘œ μ „ν™˜ν•΄μ•Ό ν•œλ‹€.
  • 보톡 λ¬Έμžμ—΄μ„ λ°°μ—΄λ‘œ μ „ν™˜ν•  λ•ŒλŠ” String 클래슀의 split λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
const split = (str: string, delim: string = ''): string[] => str.split(delim);

console.log(
split('hello'), // [ 'h', 'e', 'l', 'l', 'o' ]
split('h_e_l_l_o', '_'), // [ 'h', 'e', 'l', 'l', 'o' ]
);
  • string[] νƒ€μž…μ˜ 배열을 λ‹€μ‹œ string νƒ€μž…μœΌλ‘œ λ³€ν™˜ν•˜λ €λ©΄ Array 클래슀의 join λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€.
  • λ‹€μŒμ€ join λ©”μ„œλ“œλ₯Ό μ΄μš©ν•˜λŠ” μ‚¬μš©μž μ •μ˜ ν•¨μˆ˜ join을 μž‘μ„±ν•œ μ˜ˆμ΄λ‹€.
const join = (strArray: string[], delim: string=''): string =>
strArray.join(delim);

console.log(
join(['h', 'e', 'l', 'l', 'o']), // hello
join(['h', 'e', 'l', 'l', 'o'], '_'), // h_e_l_l_o
);

πŸ“š 인덱슀 μ—°μ‚°μžβ€‹

  • 배열이 λ‹΄κ³  μžˆλŠ” μ•„μ΄ν…œ 쀑 νŠΉμ • μœ„μΉ˜μ— μžˆλŠ” μ•„μ΄ν…œμ„ μ–»κ³ μž ν•  λ•ŒλŠ” 인덱슀 μ—°μ‚°μž[인덱슀]λ₯Ό μ‚¬μš©ν•œλ‹€.
const numbers: number[] = [1, 2, 3, 4, 5];

for(let index = 0; index < numbers.length; index++) {
const item: number = numbers[index];
console.log(item); // 1 2 3 4 5
}

πŸ“š λ°°μ—΄μ˜ 비ꡬ쑰화 할당​

  • λ°°μ—΄μ˜ 비ꡬ쑰화 ν• λ‹Ήλ¬Έμ—μ„œλŠ” 객체와 달리 [] 기호λ₯Ό μ‚¬μš©ν•œλ‹€.
let array: number[] = [1, 2, 3, 4, 5];
let [first, second, third, ...rest] = array;

console.log(first, second, third, rest); // 1 2 3 [4, 5]

πŸ“š for...in 문​

  • for...in 문은 객체λ₯Ό λŒ€μƒμœΌλ‘œ μ‚¬μš©ν•˜μ§€λ§Œ 배열도 κ°μ²΄μ΄λ―€λ‘œ λ°°μ—΄μ—μ„œ μ‚¬μš©ν•  수 μžˆλ‹€.
let names = ['Jack', 'Jane', 'Steve'];

for (let index in names) {
const name = names[index];
console.log(`[${index}]: ${name}`); // [0]: Jack [1]: Jane [2]: Steve
}
  • λ§Œμ•½ for...in 문에 객체λ₯Ό μ‚¬μš©ν•  λ•ŒλŠ” 객체가 가진 속성을 λŒ€μƒμœΌλ‘œ μˆœνšŒν•œλ‹€.
let jack = { name: 'Jack', age: 32 };
for(let property in jack) {
console.log(`${property}: ${jack[property]}`); // name: 'Jack' age: 32
}

πŸ“š for...of 문​

  • for...in 문은 λ°°μ—΄μ˜ μΈλ±μŠ€κ°’μ„ λŒ€μƒμœΌλ‘œ μˆœνšŒν•˜μ§€λ§Œ, for...of 문은 λ°°μ—΄μ˜ μ•„μ΄ν…œκ°’μ„ λŒ€μƒμœΌλ‘œ μˆœνšŒν•œλ‹€.
for(let name of ['Jack', 'Jane', 'Steve'])
console.log(name); // Jack Jane Steve

πŸ“š μ œλ„€λ¦­ 방식 νƒ€μž…β€‹

  • 배열을 λ‹€λ£¨λŠ” ν•¨μˆ˜λ₯Ό μž‘μ„±ν•  λ•ŒλŠ” number[]와 같이 νƒ€μž…μ΄ κ³ μ •λœ ν•¨μˆ˜λ₯Ό λ§Œλ“€κΈ°λ³΄λ‹€λŠ” T[] ν˜•νƒœλ‘œ λ°°μ—΄μ˜ μ•„μ΄ν…œ νƒ€μž…μ„ ν•œκΊΌλ²ˆμ— ν‘œν˜„ν•˜λŠ” 것이 νŽΈλ¦¬ν•˜λ‹€.
  • νƒ€μž…μ„ T와 같은 μΌμ’…μ˜ λ³€μˆ˜λ‘œ μ·¨κΈ‰ν•˜λŠ” 것을 μ œλ„€λ¦­(generics) νƒ€μž…μ΄λΌκ³  ν•œλ‹€.
const arrayLength = (array: T[]): number => array.length;
  • 그런데 μ΄λ ‡κ²Œ ν•˜λ©΄ μ»΄νŒŒμΌλŸ¬κ°€ T의 의미λ₯Ό μ•Œ 수 μžˆμ–΄μ•Ό ν•œλ‹€.
  • 즉, Tκ°€ νƒ€μž… λ³€μˆ˜λΌκ³  μ•Œλ €μ€˜μ•Ό ν•œλ‹€.
export const arrayLength = <T>(array: T[]): number => array.length;
export const isEmpty = <T>(array: T[]): boolean => arrayLength<T>(array) == 0;
  • μ œλ„€λ¦­ ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν–ˆμœΌλ―€λ‘œ λ‹€μ–‘ν•œ λ°°μ—΄ νƒ€μž…μ— λͺ¨λ‘ μ •μƒμ μœΌλ‘œ λŒ€μ‘ν•˜λŠ” 것을 λ³Ό 수 μžˆλ‹€.
import { arrayLength, isEmpty } from "./arrayLength";

let numArray: number[] = [1, 2, 3];
let strArray: string[] = ['Hello', 'World'];

type IPerson = {
name: string,
age?: number,
};

let personArray: IPerson[] = [
{ name: 'Jack'},
{ name: 'Jane', age: 32 },
];

console.log(
arrayLength(numArray), // 3
arrayLength(strArray), // 2
arrayLength(personArray), // 2
isEmpty([]), // true
isEmpty([1]), // false
);

πŸ“š μ œλ„€λ¦­ ν•¨μˆ˜μ˜ νƒ€μž… 좔둠​

  • λ‹€μŒ μ½”λ“œμ˜ 1ν–‰μ˜ identity ν•¨μˆ˜λŠ” μ œλ„€λ¦­ ν˜•νƒœλ‘œ κ΅¬ν˜„λ˜μ–΄ μžˆλ‹€.
  • μ œλ„€λ¦­ ν˜•νƒœλ‘œ κ΅¬ν˜„λœ ν•¨μˆ˜λŠ” μ›μΉ™μ μœΌλ‘œ 3ν–‰μ²˜λŸΌ νƒ€μž… λ³€μˆ˜λ₯Ό λ‹€μŒκ³Ό 같은 ν˜•νƒœλ‘œ λͺ…μ‹œν•΄ μ£Όμ–΄μ•Ό ν•œλ‹€.
const identity = <T>(n: T): T => n;

console.log(
identity<boolean>(true), // true
identity(true), // true
);
  • ν•˜μ§€λ§Œ 이런 μ½”λ“œλŠ” λ²ˆκ±°λ‘œμ›Œμ„œ νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” 4ν–‰μ²˜λŸΌ νƒ€μž… λ³€μˆ˜ 뢀뢄을 μƒλž΅ν•  수 있게 ν•œλ‹€.
ν•¨μˆ˜μ΄λ¦„<νƒ€μž…λ³€μˆ˜>(λ§€κ°œλ³€μˆ˜)
  • νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” νƒ€μž… λ³€μˆ˜κ°€ μƒλž΅λœ μ œλ„€λ¦­ ν•¨μˆ˜λ₯Ό λ§Œλ‚˜λ©΄ νƒ€μž… 좔둠을 톡해 μƒλž΅λœ νƒ€μž…μ„ μ°Ύμ•„λ‚Έλ‹€.

πŸ“š μ œλ„€λ¦­ ν•¨μˆ˜μ˜ ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜β€‹

  • νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” μ–΄λ–€ 경우 ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜μ˜ λ§€κ°œλ³€μˆ˜ 뢀뢄에 λ³€μˆ˜ 이름을 κΈ°μž…ν•˜λΌκ³  μš”κ΅¬ν•œλ‹€.
  • λ‹€μŒ ν™”λ©΄μ—μ„œ normal ν•¨μˆ˜λŠ” cbλΌλŠ” μ΄λ¦„μ˜ λ§€κ°œλ³€μˆ˜μ— ν•¨μˆ˜ μ‹œκ·Έλ‹ˆμ²˜λ₯Ό μ‚¬μš©ν–ˆλ‹€. 그런데 normalκ³Ό 달리 errorλŠ” 였λ₯˜κ°€ λ°œμƒν•œλ‹€.
const normal = (cb: (number) => number): void => {};
// error: number μ‹λ³„μžκ°€ μ€‘λ³΅λ˜μ—ˆμŠ΅λ‹ˆλ‹€.
const error = (cb: (number, number?) => number): void => {};
const fixed = (cb: (a:number, number?) => number): void => {};
  • 이런 였λ₯˜κ°€ λ°œμƒν•˜λ©΄ 3ν–‰μ˜ fixed μ„ μ–Έλ¬Έμ²˜λŸΌ νƒ€μž…μŠ€ν¬λ¦½νŠΈκ°€ ν•΄μ„ν•˜μ§€ λͺ»ν•˜λŠ” 뢀뢄에 λ³€μˆ˜λ₯Ό μ‚½μž…ν•˜κ³  이 λ³€μˆ˜μ— νƒ€μž…μ„ λͺ…μ‹œν•΄ ν•΄κ²°ν•œλ‹€.
  • μ œλ„€λ¦­ νƒ€μž…μ˜ ν•¨μˆ˜μ—μ„œλ„ 같은 λ¬Έμ œκ°€ λ°œμƒν•˜λŠ”λ°, ν•΄κ²° 방법은 μ•žμ„œ fixedμ—μ„œμ™€ κ°™λ‹€.
const f = <T>(cb: (arg: T, i?: number) => number): void => {};

πŸ“š μ „κ°œ μ—°μ‚°μžβ€‹

  • μ „κ°œ μ—°μ‚°μžλŠ” λ°°μ—΄μ—μ„œλ„ μ μš©ν•  수 μžˆλ‹€.
let array1: number[] = [1];
let array2: number[] = [2, 3];
let mergedArray: number[] = [...array1, ...array2, 4];
console.log(mergedArray); // [1, 2, 3, 4]

πŸ“š range ν•¨μˆ˜ κ΅¬ν˜„β€‹

  • ramda의 μ™ΈλΆ€ νŒ¨ν‚€μ§€κ°€ μ œκ³΅ν•˜λŠ” R.rangeλž€ ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄ λ΄€μ—ˆλŠ”λ°, λ°°μ—΄μ˜ μ „κ°œ μ—°μ‚°μžλ₯Ό μ μš©ν•˜λ©΄ R.range와 같은 ν•¨μˆ˜λ₯Ό μ‰½κ²Œ λ§Œλ“€ 수 μžˆλ‹€.
  • λ‹€μŒ range ν•¨μˆ˜λŠ” μž¬κ·€ ν•¨μˆ˜ μŠ€νƒ€μΌλ‘œ λ™μž‘ν•˜λ©°, R.range처럼 fromμ—μ„œ toκΉŒμ§€ 수둜 κ΅¬μ„±λœ 배열을 생성해 μ€€λ‹€.
const range = (from: number, to: number): number[] =>
from < to ? [from, ...range(from + 1, to)]: [];

let numbers: number[] = range(1, 10);
console.log(numbers); // [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

πŸ¦„ μ„ μ–Έν˜• ν”„λ‘œκ·Έλž˜λ°κ³Ό 배열​

πŸ“š λͺ…λ Ήν˜• ν”„λ‘œκ·Έλž˜λ°μ΄λž€?​

  • ν”„λ‘œκ·Έλž¨μ˜ κΈ°λ³Έ ν˜•νƒœλŠ” μž…λ ₯ 데이터λ₯Ό μ–»κ³  κ°€κ³΅ν•œ λ‹€μŒ, κ²°κ³Όλ₯Ό 좜λ ₯ν•˜λŠ” ν˜•νƒœλ‘œ κ΅¬μ„±λœλ‹€.
  1. μž…λ ₯ 데이터 μ–»κΈ°
  2. μž…λ ₯ 데이터 가곡해 좜λ ₯ 데이터 생성
  3. 좜λ ₯ 데이터 좜λ ₯
  • λ°˜λ©΄μ— μ„ μ–Έν˜• ν”„λ‘œκ·Έλž˜λ°μ€ μ‹œμŠ€ν…œ μžμ›μ˜ 효율적인 μš΄μš©λ³΄λ‹€λŠ” μΌκ΄„λœ 문제 ν•΄κ²° ꡬ쑰에 더 μ§‘μ€‘ν•œλ‹€. μ„ μ–Έν˜• ν”„λ‘œκ·Έλž˜λ°μ€ λͺ…λ Ήν˜• ν”„λ‘œκ·Έλž˜λ°μ²˜λŸΌ for문을 μ‚¬μš©ν•˜μ§€ μ•Šκ³  λͺ¨λ‘ 데이터λ₯Ό 배열에 λ‹΄κ³  κ·Έ λ¬Έμ œκ°€ 해결될 λ•ŒκΉŒμ§€ λŠμž„μ—†μ΄ 또 λ‹€λ₯Έ ν˜•νƒœμ˜ λ°°μ—΄λ‘œ κ°€κ³΅ν•˜λŠ” λ°©μ‹μœΌλ‘œ κ΅¬ν˜„ν•œλ‹€.
  1. 문제λ₯Ό ν‘ΈλŠ” 데 ν•„μš”ν•œ λͺ¨λ“  데이터 배열에 μ €μž₯
  2. μž…λ ₯ 데이터 배열을 가곡해 좜λ ₯ 데이터 λ°°μ—΄ 생성
  3. 좜λ ₯ 데이터 배열에 λ‹΄κΈ΄ μ•„μ΄ν…œ 좜λ ₯

πŸ“š 1λΆ€ν„° 100κΉŒμ§€ λ”ν•˜κΈ° 문제 풀이​

  • μ•„λž˜ κ΅¬μ‘°λŠ” λͺ…λ Ήν˜• ν”„λ‘œκ·Έλž˜λ° 방식이닀.
let sum = 0;
for(let val = 1; val <= 100;)
sum += val++;
console.log(sum); // 5050
  • λ‹€μŒ μ½”λ“œλŠ” μ„ μ–Έν˜•μœΌλ‘œ κ΅¬ν˜„ν•œ 것이닀.
  • λͺ…λ Ήμ–΄ μ½”λ“œλŠ” 데이터와 가곡이 for λ¬Έ μ•ˆμ—μ„œ μ΄λ£¨μ–΄μ‘Œμ§€λ§Œ, μ„ μ–Έν˜•μ€ 데이터 생성과 가곡 과정을 λΆ„λ¦¬ν•œλ‹€.
import { range } from "./range";

let numbers: number[] = range(1, 100 + 1);
console.log(numbers); // [1, 2, ..., 100]

πŸ“š fold: λ°°μ—΄ 데이터 접기​

  • ν΄λ“œλŠ” [1, 2, 3, ...] ν˜•νƒœμ˜ λ°°μ—΄ 데이터λ₯Ό 가곡해 5050κ³Ό 같은 ν•˜λ‚˜μ˜ 값을 μƒμ„±ν•˜λ €κ³  ν•  λ•Œ μ‚¬μš©ν•œλ‹€.
  • ν΄λ“œ ν•¨μˆ˜λŠ” T[] νƒ€μž… 배열을 가곡해 T νƒ€μž…μ˜ κ²°κ³Όλ₯Ό λ§Œλ“€μ–΄ μ€€λ‹€.
export const fold = <T>(array: T[], callback: (result: T, val: T) => T, initValue: T) => {
let result: T = initValue;

for (let i = 0; i < array.length; ++i) {
const value = array[i];
result = callback(result, value);
}

return result;
}
  • λ‹€μŒμ€ fold ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•΄ μ„ μ–Έν˜• ν”„λ‘œκ·Έλž˜λ° λ°©μ‹μœΌλ‘œ 1λΆ€ν„° 100κΉŒμ§€ λ”ν•˜λŠ” μ½”λ“œλ₯Ό κ΅¬ν˜„ν•œ 것이닀.
import { range } from "./range";
import { fold } from './fold';

// μž…λ ₯ 데이터 생성
let numbers: number[] = range(1, 100 + 1);

// μž…λ ₯ 데이터 가곡
let result = fold(numbers, (result, value) => result + value, 0);
console.log(result); // 5050
  • λͺ…λ Ήν˜• 방식은 μ‹œμŠ€ν…œ μžμ›μ˜ νš¨μœ¨μ„ μ΅œμš°μ„ μœΌλ‘œ μƒκ°ν•˜μ§€λ§Œ, μ„ μ–Έν˜• 방식은 ν΄λ“œμ²˜λŸΌ λ²”μš©μœΌλ‘œ κ΅¬ν˜„λœ ν•¨μˆ˜λ₯Ό μž¬μ‚¬μš©ν•˜λ©΄μ„œ 문제λ₯Ό ν•΄κ²°ν•œλ‹€.

πŸ“š 1λΆ€ν„° 100κΉŒμ§€ ν™€μˆ˜μ˜ ν•© κ΅¬ν•˜κΈ°β€‹

  • λ‹€μŒμ€ λͺ…λ Ήν˜• λ°©μ‹μœΌλ‘œ κ΅¬ν˜„ν•œ μ½”λ“œμ΄λ‹€.
let oddSum = 0;
for (let val = 1; val <= 100; val += 2) {
oddSum += val;
}

console.log(oddSum); // 2500

πŸ“š filter: 쑰건에 λ§žλŠ” μ•„μ΄ν…œλ§Œ 좔렀내기​

  • ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ—μ„œ ν”νžˆ λ³΄λŠ” filterλΌλŠ” μ΄λ¦„μ˜ ν•¨μˆ˜λŠ” μž…λ ₯ 배열을 가곡해 쑰건에 λ§žλŠ” κ°’λ§Œ μΆ”λ €λ‚΄λŠ” κΈ°λŠ₯을 ν•œλ‹€.
  • filter ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•œ μ˜ˆμ΄λ‹€.
export const filter = <T>(array: T[], callback: (value: T, index?: number) => boolean): T[] => {
let result: T[] = [];

for (let index: number = 0; index < array.length; ++index) {
const value = array[index];

if(callback(value, index)) {
result = [...result, value];
}

return result;
}
}
  • filterλ₯Ό μ‚¬μš©ν•΄ κ΅¬ν˜„ν•œλ‹€.
import { range } from "./range";
import { fold } from './fold';
import { filter } from './filter';

let numbers: number[] = range(1, 100 + 1);

const isOdd = (n: number): boolean => n % 2 !== 0;
let result = fold(filter(numbers, isOdd), (result, value) => result + value, 0);

console.log(result); // 2500

πŸ“š 12 + 22 + ... + 1002​

  • λͺ…λ Ήν˜• λ°©μ‹μœΌλ‘œ κ΅¬ν˜„ν•œ 것이닀.
let squareSum = 0;
for (let val = 1; val <= 100; ++val ) {
squareSum += val * val;
}
console.log(squareSum);
  • μ„ μ–Έν˜• λ°©μ‹μœΌλ‘œ μž…λ ₯ 데이터λ₯Ό 이와 같이 κ΅¬ν˜„ν•˜λ €λ©΄ mapμ΄λΌλŠ” ν•¨μˆ˜κ°€ ν•„μš”ν•˜λ‹€.

πŸ“š map: λ°°μ—΄ 데이터 κ°€κ³΅ν•˜κΈ°β€‹

  • λ³€μˆ˜ x와 y의 νƒ€μž…κΉŒμ§€ μƒκ°ν•˜λ©΄ map은 x: T -> y: Q 처럼 μž…λ ₯κ³Ό 좜λ ₯의 λ³€μˆ˜μ˜ νƒ€μž…μ΄ μ„œλ‘œ λ‹€λ₯Ό 수 μžˆμŒμ„ κ³ λ €ν•΄μ•Ό ν•œλ‹€.
export const map = <T, Q>(array: T[], callback: (value: T, index?: number) => Q): Q[] => {
let result: Q[] = [];

for(let index = 0; index < array.length; ++index) {
const value = array[index];
result = [...result, callback(value, index)];
}

return result;
}
  • 이제 map ν•¨μˆ˜λ₯Ό μ΄μš©ν•˜λ©΄ μ„ μ–Έν˜• λ°©μ‹μ˜ μ½”λ“œλ‘œ μž‘μ„±ν•  수 μžˆλ‹€.
import { range } from "./range";
import { fold } from './fold';
import { map } from './map';

let numbers: number[] = range(1, 100 + 1);
let result = fold(
map(numbers, value => value * value),
(result, value) => result + value,
0
)

console.log(result); // 338350

πŸ¦„ λ°°μ—΄μ˜ map, reduce, filter λ©”μ„œλ“œβ€‹

πŸ“š filter λ©”μ„œλ“œβ€‹

  • λ°°μ—΄μ˜ νƒ€μž…μ΄ T[]일 λ•Œ λ°°μ—΄μ˜ filter λ©”μ„œλ“œλŠ” λ‹€μŒκ³Ό 같은 ν˜•νƒœλ‘œ μ„€κ³„λ˜μ—ˆλ‹€.
filter(callback: (value: T, index?: number): boolean): T[]
  • λ‹€μŒ μ½”λ“œλŠ” filter λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ κ΅¬ν˜„ν•œ μ˜ˆμ΄λ‹€.
import { range } from "./range";

const array: number[] = range(1, 10 + 1);

let odds: number[] = array.filter((value) => value % 2 !== 0);
let evens: number[] = array.filter((value) => value % 2 === 0);

console.log(odds, evens); // [ 1, 3, 5, 7, 9 ] [ 2, 4, 6, 8, 10 ]

πŸ“š map λ©”μ„œλ“œβ€‹

  • λ°°μ—΄μ˜ νƒ€μž…μ΄ T[]일 λ•Œ λ°°μ—΄μ˜ map λ©”μ„œλ“œλŠ” λ‹€μŒκ³Ό 같은 ν˜•νƒœλ‘œ μ„€κ³„λ˜μ—ˆμœΌλ©°, filter와 달리 map λ©”μ„œλ“œλŠ” μž…λ ₯ νƒ€μž…κ³Ό λ‹€λ₯Έ νƒ€μž…μ˜ 배열을 λ§Œλ“€ 수 μžˆλ‹€.
map(callback: (value: T, index? number): Q): Q[]
  • λ‹€μŒ μ½”λ“œλŠ” map λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ κ΅¬ν˜„ν•œ 예둜 number[] νƒ€μž… λ°°μ—΄μ˜ string[] νƒ€μž… λ°°μ—΄λ‘œ κ°€κ³΅ν•˜λŠ” μ˜ˆμ΄λ‹€.
import { range } from "./range";

let square: string[] = range(1, 5 + 1)
.map((val, index) => `[${index}]: ${val}`);

console.log(square); // [ '[0]: 1', '[1]: 2', '[2]: 3', '[3]: 4', '[4]: 5' ]

πŸ“š reduce λ©”μ„œλ“œβ€‹

  • κ΅¬ν˜„ν•œ fold ν•¨μˆ˜λŠ” νƒ€μž…μŠ€ν¬λ¦½νŠΈ λ°°μ—΄μ˜ reduce λ©”μ„œλ“œλ‘œ λŒ€μ²΄ν•  수 μžˆλ‹€.
  • λ°°μ—΄μ˜ νƒ€μž…μ΄ T[]일 λ•Œ λ°°μ—΄μ˜ reduce λ©”μ„œλ“œλŠ” λ‹€μŒκ³Ό 같은 ν˜•νƒœλ‘œ μ„€κ³„λ˜μ—ˆλ‹€.
reduce(callback: (result: T, value: T), initialValue: T): T;
  • λ‹€μŒμ€ 1λΆ€ν„° 100κΉŒμ§€ λ”ν•˜λŠ” λ‘œμ§μ„ reduce λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•΄ λ‹€μ‹œ κ΅¬ν˜„ν•œ μ˜ˆμ΄λ‹€.
import { range } from "./range";

let reduceSum: number = range(1, 100 + 1)
.reduce((result: number, value: number) => result + value, 0);

console.log(reduceSum); // 5050

πŸ¦„ 순수 ν•¨μˆ˜μ™€ 배열​

  • ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ—μ„œ ν•¨μˆ˜λŠ” 순수 ν•¨μˆ˜(pure function)λΌλŠ” 쑰건을 λ§Œμ‘±ν•΄μ•Ό ν•œλ‹€.
  • κ·ΈλŸ¬λ‚˜ νƒ€μž…μŠ€ν¬λ¦½νŠΈμ˜ Array ν΄λž˜μŠ€μ—λŠ” 순수 ν•¨μˆ˜ 쑰건에 λΆ€ν•©ν•˜μ§€ μ•ŠλŠ” λ©”μ„œλ“œκ°€ λ§Žλ‹€.

πŸ“š 순수 ν•¨μˆ˜λž€?​

  • 순수 ν•¨μˆ˜λŠ” λΆ€μˆ˜ 효과(side-effect)κ°€ μ—†λŠ” ν•¨μˆ˜λ₯Ό λ§ν•œλ‹€.
  • μ—¬κΈ°μ„œ λΆ€μˆ˜ νš¨κ³Όλž€ ν•¨μˆ˜κ°€ 가진 κ³ μœ ν•œ λͺ©μ  이외에 λ‹€λ₯Έ νš¨κ³Όκ°€ λ‚˜νƒ€λ‚˜λŠ” 것을 μ˜λ―Έν•˜λ©° λΆ€μž‘μš©μ΄λΌκ³ λ„ ν•œλ‹€.
  • λ°˜λ©΄μ— λΆ€μˆ˜ νš¨κ³Όκ°€ μžˆλŠ” ν•¨μˆ˜λ₯Ό 뢈순 ν•¨μˆ˜(impure function)라고 ν•œλ‹€.
  • ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ—μ„œ λ°œμƒν•˜λŠ” λΆ€μˆ˜ νš¨κ³ΌλŠ” ν•¨μˆ˜λ₯Ό 순수 ν•¨μˆ˜ ν˜•νƒœλ‘œ μž‘μ„±ν•΄μ•Όλ§Œ μ œκ±°ν•  수 μžˆλ‹€.
  • λΆ€μˆ˜ νš¨κ³Όκ°€ μ—†λŠ” μˆœμˆ˜ν•œ ν•¨μˆ˜μ΄λ €λ©΄ λ‹€μŒκ³Ό 같은 쑰건을 μΆ©μ‘±ν•΄μ•Ό ν•œλ‹€.
  • ν•¨μˆ˜ λͺΈν†΅μ— μž…μΆœλ ₯ κ΄€λ ¨ μ½”λ“œκ°€ μ—†μ–΄μ•Ό ν•œλ‹€.
  • ν•¨μˆ˜ λͺΈν†΅μ—μ„œ λ§€κ°œλ³€μˆ«κ°’μ„ λ³€κ²½μ‹œν‚€μ§€ μ•ŠλŠ”λ‹€. (즉, λ§€κ°œλ³€μˆ˜λŠ” constλ‚˜ readonly ν˜•νƒœλ‘œλ§Œ μ‚¬μš©ν•œλ‹€.)
  • ν•¨μˆ˜ λͺΈν†΅μ—μ„œ λ§Œλ“€μ–΄μ§„ κ²°κ³Όλ₯Ό μ¦‰μ‹œ λ°˜ν™˜ν•œλ‹€.
  • ν•¨μˆ˜ 내뢀에 μ „μ—­ λ³€μˆ˜λ‚˜ 정적 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜μ§€ μ•ŠλŠ”λ‹€.
  • ν•¨μˆ˜κ°€ μ˜ˆμ™Έλ₯Ό λ°œμƒμ‹œν‚€μ§€ μ•ŠλŠ”λ‹€.
  • ν•¨μˆ˜κ°€ 콜백 ν•¨μˆ˜λ‘œ κ΅¬ν˜„λ˜μ–΄μžˆκ±°λ‚˜ ν•¨μˆ˜ λͺΈν†΅μ— 콜백 ν•¨μˆ˜λ₯Ό μ‚¬μš©ν•˜λŠ” μ½”λ“œκ°€ μ—†λ‹€.
  • ν•¨μˆ˜ λͺΈν†΅μ— Promise와 같은 비동기 λ°©μ‹μœΌλ‘œ λ™μž‘ν•˜λŠ” μ½”λ“œκ°€ μ—†λ‹€.
  • 예λ₯Ό λ“€μ–΄, λ‹€μŒ pure ν•¨μˆ˜λŠ” 이런 쑰건을 λͺ¨λ‘ λ§Œμ‘±ν•˜λŠ” 순수 ν•¨μˆ˜μ΄λ‹€.
function pure(a: number, b: number): number {
return a + b;
}
  • κ·Έλ‘œλ‚˜ λ‹€μŒ impure1 ν•¨μˆ˜λŠ” λ§€κ°œλ³€μˆ˜λ₯Ό λ³€κ²½ν•˜λ―€λ‘œ λΆ€μˆ˜ νš¨κ³Όκ°€ λ°œμƒν•œλ‹€. 즉, λ§€κ°œλ³€μˆ˜κ°€ readonly ν˜•νƒœλ‘œ λ™μž‘ν•˜μ§€ μ•ŠμœΌλ―€λ‘œ 뢈순 ν•¨μˆ˜μ΄λ‹€.
function impure1(array: number[]): void {
array.push(1);
array.splice(0, 1);
}
  • λ‹€μŒ impure2 ν•¨μˆ˜λŠ” gλΌλŠ” μ™ΈλΆ€ λ³€μˆ˜λ₯Ό μ‚¬μš©ν•˜λ―€λ‘œ 뢈순 ν•¨μˆ˜μ΄λ‹€.
let g = 10;
function impure2(x: number) {
return x + g;
}

πŸ“š νƒ€μž… μˆ˜μ •μž readonly​

  • νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” 순수 ν•¨μˆ˜ κ΅¬ν˜„μ„ μ‰½κ²Œ ν•˜λ„λ‘ readonly ν‚€μ›Œλ“œλ₯Ό μ œκ³΅ν•œλ‹€.
  • readonly νƒ€μž…μœΌλ‘œ μ„ μ–Έλœ λ§€κ°œλ³€μˆ«κ°’μ„ λ³€κ²½ν•˜λŠ” μ‹œλ„κ°€ 있으면 λ‹€μŒμ²˜λŸΌ λ¬Έμ œκ°€ μžˆλŠ” μ½”λ“œλΌκ³  μ•Œλ €μ€˜μ„œ 뢈순 ν•¨μˆ˜κ°€ λ˜μ§€ μ•Šκ²Œ λ°©μ§€ν•œλ‹€.
function forcePure(array: readonly number[]){
array.push(1); // 'readonly number[]' ν˜•μ‹μ— 'push' 속성이 μ—†μŠ΅λ‹ˆλ‹€.
}
  • νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œ μΈν„°νŽ˜μ΄μŠ€, 클래슀, ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜ 등은 letμ΄λ‚˜ const ν‚€μ›Œλ“œ 없이 μ„ μ–Έν•˜κΈ° λ•Œλ¬Έμ— 이런 μ‹¬λ²Œμ— const와 같은 효과λ₯Ό μ£Όλ €λ©΄ readonlyλΌλŠ” νƒ€μž… μˆ˜μ •μžκ°€ ν•„μš”ν•˜λ‹€.

πŸ“š λΆˆλ³€κ³Ό 가변​

  • λ³€μˆ˜ constλ‚˜ readonlyλ₯Ό λͺ…μ‹œν•˜κ³  있으면 λ³€μˆ«κ°’μ€ μ΄ˆκΉƒκ°’μ„ 항상 μœ μ§€ν•œλ‹€.
  • 이런 λ³€μˆ˜λ₯Ό λΆˆλ³€(immutable) λ³€μˆ˜λΌκ³  ν•œλ‹€.
  • λ°˜λ©΄μ— letμ΄λ‚˜ readonlyλ₯Ό λͺ…μ‹œν•˜μ§€ μ•Šμ€ λ³€μˆ˜λ₯Ό μ–Έμ œλ“  값을 λ³€κ²½ν•  수 μžˆλ‹€. 이런 λ³€μˆ˜λŠ” κ°€λ³€(mutable) λ³€μˆ˜λΌκ³  ν•œλ‹€.

πŸ“š κΉŠμ€ 볡사와 앝은 볡사​

  • 순수 ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•  λ•ŒλŠ” λ§€κ°œλ³€μˆ˜κ°€ λΆˆλ³€μ„±μ„ μœ μ§€ν•΄μ•Ό ν•˜λ―€λ‘œ, λ§€κ°œλ³€μˆ˜λ₯Ό κ°€κ³΅ν•˜λ €κ³  ν•  λ•Œ κΉŠμ€ 볡사가 μ‹€ν–‰ν•΄ λ§€κ°œλ³€μˆ˜κ°’μ΄ λ³€κ²½λ˜μ§€ μ•Šκ²Œ ν•΄μ•Ό ν•œλ‹€.
  • κΉŠμ€ λ³΅μ‚¬λŠ” λŒ€μƒ λ³€μˆ«κ°’μ΄ λ°”λ€” λ•Œ 원본 λ³€μˆ˜κ°’μ€ κ·ΈλŒ€λ‘œμΈ ν˜•νƒœλ‘œ λ™μž‘ν•œλ‹€.
let original = 1;
let copied = original;
copied += 2;
console.log(original, copied); // 1 3
  • νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œ number와 boolean νƒ€μž…μ€ κΉŠμ€ 볡사 ν˜•νƒœλ‘œ λ™μž‘ν•œλ‹€.
  • κ·ΈλŸ¬λ‚˜ 객체와 배열은 얕은 볡사 λ°©μ‹μœΌλ‘œ λ™μž‘ν•œλ‹€.
const originalArray = [5, 3, 9, 7];
const shallowCopiedArray = originalArray;
shallowCopiedArray[0] = 0;
console.log(originalArray, shallowCopiedArray); // [ 0, 3, 9, 7 ] [ 0, 3, 9, 7 ]

πŸ“š μ „κ°œ μ—°μ‚°μžμ™€ κΉŠμ€ 볡사​

  • μ „κ°œ μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•΄ 배열을 λ³΅μ‚¬ν•˜λ©΄ κΉŠμ€ 볡사λ₯Ό ν•  수 μžˆλ‹€.
const oArray = [1, 2, 3, 4];
const deepCopiedArray = [...oArray];
deepCopiedArray[0] = 0;
console.log(oArray, deepCopiedArray); // [ 1, 2, 3, 4 ] [ 0, 2, 3, 4 ]

πŸ“š λ°°μ—΄μ˜ sort λ©”μ„œλ“œλ₯Ό 순수 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜κΈ°β€‹

  • sort λ©”μ„œλ“œλŠ” 원본 λ°°μ—΄μ˜ λ‚΄μš©μ„ λ³€κ²½ν•œλ‹€.
  • λ‹€μŒ pureSort ν•¨μˆ˜λŠ” readonly νƒ€μž…μœΌλ‘œ μž…λ ₯ λ°°μ—΄μ˜ λ‚΄μš©μ„ μœ μ§€ν•œ 채 μ •λ ¬ν•  수 μžˆλ„λ‘ μ „κ°œ μ—°μ‚°μžμ˜ κΉŠμ€ 볡사 κΈ°λŠ₯을 μ‚¬μš©ν•œλ‹€.
const pureSort = <T>(array: readonly T[]): T[] => {
let deepCopied = [...array];
return deepCopied.sort();
}

let beforeSort = [6, 2, 9, 0];
const afterSort = pureSort(beforeSort);
console.log(beforeSort, afterSort); // [ 6, 2, 9, 0 ] [ 0, 2, 6, 9 ]

πŸ“š λ°°μ—΄μ˜ filter λ©”μ„œλ“œμ™€ μˆœμˆ˜ν•œ μ‚­μ œβ€‹

  • λ°°μ—΄μ—μ„œ νŠΉμ • μ•„μ΄ν…œμ„ μ‚­μ œν•  λ•ŒλŠ” splice λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 그런데 spliceλŠ” 원본 λ°°μ—΄μ˜ λ‚΄μš©μ„ λ³€κ²½ν•˜λ―€λ‘œ 순수 ν•¨μˆ˜μ—μ„œλŠ” μ‚¬μš©ν•  수 μ—†λ‹€. κ·Έλ ‡κΈ° λ•Œλ¬Έμ— filter λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•  수 μžˆλ‹€.
  • filter λ©”μ„œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ 원본 λ°°μ—΄μ˜ λ‚΄μš©μ„ ν›Όμ†ν•˜μ§€ μ•ŠμœΌλ©΄μ„œ 쑰건에 λ§žμ§€ μ•ŠλŠ” μ•„μ΄ν…œμ„ μ‚­μ œν•  수 μžˆλ‹€.
const pureDelete = <T>(array: readonly T[], cb: (val: T, index?: number) 
=> boolean): T[] => array.filter((val, index) => cb(val, index) == false);

const mixedArray: object[] = [
[], { name: 'Jack' }, { name: 'Jane', age: 32 }, ['description']
]

const objectsOnly: object[] = pureDelete(mixedArray, (val) => Array.isArray(val));

console.log(mixedArray, objectsOnly);
// [ [], { name: 'Jack' }, { name: 'Jane', age: 32 }, [ 'description' ] ]
// [ { name: 'Jack' }, { name: 'Jane', age: 32 } ]

πŸ“š κ°€λ³€ 인수 ν•¨μˆ˜μ™€ 순수 ν•¨μˆ˜β€‹

  • ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ μ „λ‹¬ν•˜λŠ” 인수의 개수λ₯Ό μ œν•œν•˜μ§€ μ•ŠλŠ” 것을 κ°€λ³€ 인수(variadic arguments)라고 ν•œλ‹€.
const mergedArray: number[] = mergeArray(
[1], [2, 3], [4, 5, 6], [7, 8, 9, 10],
)

console.log(mergedArray); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  • λ‹€μŒ μ½”λ“œλŠ” κ°€λ³€ 인수둜 ν˜ΈμΆœν•  수 μžˆλŠ” mergeArray ν•¨μˆ˜μ΄λ‹€.
export const mergeArray = (...arrays) => {};
  • νƒ€μž…μ— 상관 없이 λ™μž‘ν•˜κ²Œ ν•˜λ €λ©΄ λ‹€μŒ 처럼 μ œλ„€λ¦­ νƒ€μž…μœΌλ‘œ κ΅¬ν˜„ν•œλ‹€.
export const mergeArray = <T>(...arrays) => {};
  • λ˜ν•œ, mergeArray ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•  λ•Œ μ „λ‹¬ν•˜λŠ” 값은 λͺ¨λ‘ λ°°μ—΄μ΄μ˜€λ‹€. λ”°λΌμ„œ λ§€κ°œλ³€μˆ˜ arrays의 νƒ€μž…μ€ λ°°μ—΄μ˜ λ°°μ—΄λ‘œ μ„ μ–Έν•œλ‹€.
export const mergeArray = <T>(...arrays: T[][]) => {};
  • mergeArray ν•¨μˆ˜μ˜ λ§€κ°œλ³€μˆ˜ arraysλŠ” λ°°μ—΄μ˜ 배열인 T[][] νƒ€μž…μΌμ§€λΌλ„ 좜λ ₯은 T[]ν˜•νƒœμ˜ 배열을 λ°˜ν™˜ν•΄μ•Ό ν•œλ‹€.
export const mergeArray = <T>(...arrays: T[][]): T[] => {};
  • λ§ˆμ§€λ§‰μœΌλ‘œ mergeArray ν•¨μˆ˜λ₯Ό 순수 ν•¨μˆ˜λ‘œ κ΅¬ν˜„ν•˜λ €λ©΄ λ§€κ°œλ³€μˆ˜μ˜ λ‚΄μš©μ„ ν›Όμ†ν•˜μ§€ 말아야 ν•œλ‹€. λ”°λΌμ„œ λ‹€μŒμ²˜λŸΌ λ§€κ°œλ³€μˆ˜ νƒ€μž… μ•žμ— readonly ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•œλ‹€.
export const mergeArray = <T>(...arrays: readonly T[][]): T[] => {};
  • mergeArray ν•¨μˆ˜λ₯Ό κ΅¬ν˜„ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.
export const mergeArray = <T>(...arrays: readonly T[][]): T[] => {
let result: T[] = [];

for (let index = 0; index < arrays.length; index++) {
const array: T[] = arrays[index];

result = [...result, ...array];
}

return result;
}

πŸ¦„ νŠœν”Œ μ΄ν•΄ν•˜κΈ°β€‹

  • μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” νŠœν”Œμ΄ μ—†μœΌλ©° λ‹¨μˆœνžˆ λ°°μ—΄μ˜ ν•œ μ’…λ₯˜λ‘œ μ·¨κΈ‰λœλ‹€.
  • λ‹€μŒμ€ μ—¬λŸ¬ νƒ€μž…μ— λŒ€μ‘ν•˜λŠ” any νƒ€μž… 배열을 μ„ μ–Έν•œ μ˜ˆμ΄λ‹€.
let tuple: any[] = [true, 'the result is ok'];
  • 그런데 any[] ν˜•νƒœλŠ” νƒ€μž…μŠ€ν¬λ¦½νŠΈμ˜ νƒ€μž… κΈ°λŠ₯을 무λ ₯ν™”ν•˜λ―€λ‘œ, νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” νŠœν”Œμ˜ νƒ€μž… ν‘œκΈ°λ²•μ„ λ°°μ—΄κ³Ό λ‹€λ₯΄κ²Œ μ„ μ–Έν•  수 μžˆλ‹€.
const array: number[] = [1, 2, 3, 4];
const tuple: [boolean, string] = [true, 'the result is ok'];

πŸ“š νŠœν”Œμ—μ„œ νƒ€μž… 별칭 μ‚¬μš©ν•˜κΈ°β€‹

  • 보톡 νŠœν”Œμ„ μ‚¬μš©ν•  λ•ŒλŠ” νƒ€μž… 별칭(alias)으둜 νŠœν”Œμ˜ 의미λ₯Ό λͺ…ν™•ν•˜κ²Œ ν•œλ‹€.
  • 예λ₯Ό λ“€μ–΄ [boolean, string]이라고 νƒ€μž…μ„ μ§€μ •ν•˜λŠ” 것보닀 λ‹€μŒμ²˜λŸΌ νƒ€μž… 별칭을 μ‚¬μš©ν•΄ 이 νŠœν”Œμ΄ μ–΄λ–€ μš©λ„λ‘œ μ‚¬μš©λ˜λŠ”μ§€ μ’€ 더 λΆ„λͺ…ν•˜κ²Œ μ•Œλ €μ£ΌλŠ” 것이 μ’‹λ‹€.
export type ResultType = [boolean, string];
  • λ‹€μŒ μ½”λ“œλŠ” μ˜ˆμ™Έ 처리 ꡬ문을 μ‚¬μš©ν•΄ μ˜ˆμ™Έκ°€ λ°œμƒν–ˆμ„ λ•Œ ꡬ체적인 λ‚΄μš©μ„ νŠœν”Œλ‘œ λ°˜ν™˜ν•œλ‹€. μ΄λ•Œ λ³„μΉ­μœΌλ‘œ μ •μ˜ν•œ ResultType을 μ‚¬μš©ν•œλ‹€.
import { ResultType } from "./ResultType";

export const doSomething = (): ResultType => {
try {
throw new Error('Some error occurs...');
} catch (e) {
return [false, e.message];
}
}
  • μ΄λŸ¬ν•œ μ˜ˆμ™Έ 초리 μ½”λ“œλŠ” λΆˆμˆœν•œ ν•¨μˆ˜λ₯Ό 순수 ν•¨μˆ˜λ‘œ λ°”κΏ”μ£ΌλŠ” μ „ν˜•μ μΈ μ½”λ“œ 섀계 방식이닀.

πŸ“š νŠœν”Œμ— μ μš©ν•˜λŠ” 비ꡬ쑰화 할당​

  • νŠœν”Œμ€ λ¬Όλ¦¬μ μœΌλ‘œλŠ” λ°°μ—΄μ΄λ―€λ‘œ λ°°μ—΄μ²˜λŸΌ 인덱슀 μ—°μ‚°μžλ‚˜ 비ꡬ쑰화 할당문을 μ μš©ν•  수 μžˆλ‹€.
import { doSomething } from './doSomething';

const [result, errorMessage] = doSomething();

console.log(result, errorMessage); // false Some error occurs...