들어가기전에
시작
map
1.일단 코딩
const productList = [
{ name: '반팔티', price: 15000 },
{ name: '긴팔티', price: 45000 },
{ name: '반반팔티', price: 35000 },
{ name: '긴긴팔티', price: 56000 },
]
const nameList = []
const priceList = []
for (const product of productList) {
names.push(product.name)
priceList.push(product.price)
}
2. 개선
함수형 프로그래밍은 외부의 상태를 변경시키지 않고, 함수가 인자와 리턴값으로 소통하는 것을 권장
const nameList = productList.map((product) => product.name)
console.log(nameList)
const customMap = (f, iter) => {
let res = []
for (const element of iter) {
res.push(f(element))
}
return res
}
const nameList = customMap((e) => e.name, productList)
console.log(nameList)
이터러블 프로토콜 + map의 다형성
document.querySelectorAll('*').map((el) => el.nodeName)
customMap((el) => el.nodeName, document.querySelectorAll('*'))
filter
1. 일단 코딩
let under20000 = []
for (const product of productList) {
if (product.price < 20000) under20000.push(product)
}
console.log(under20000)
2. 개선
const customFilter = (iter, f) => {
let res = []
for (const el of iter) {
if (f(el)) res.push(el)
}
return res
}
const under20000 = customFilter(productList, (el) => el.price < 20000)
console.log(under20000)
const test = customFilter([1, 2, 3, 4, 5], (el) => el < 3)
console.log(test)
reduce
1. 일단 코딩
const numList = [1, 2, 3, 4, 5]
let total = 0
for (const num of numList) total = total + num
console.log(total)
2. 목표하는 형태
const reduce = () => {}
const add = (a, b) => a + b
const result = reduce(add, 0, [1, 2, 3, 4, 5])
const toBe = add(add(add(add(add(0, 1), 2), 3), 4), 5)
reduce(add, [1,2,3,4,5]) 형태도 가능하게 만들어주기
const reduce = (f, acc, iter) => {
for (const a of iter) acc = f(acc, a)
return acc
}
const add = (a, b) => a + b
const result = reduce(add, 0, [1, 2, 3, 4, 5])
console.log(result)
const reduce = (f, acc, iter) => {
if (!iter) {
iter = acc[Symbol.iterator]()
acc = iter.next().value
}
for (const a of iter) {
acc = f(acc, a)
}
return acc
}
const add = (a, b) => a + b
const result = reduce(add, [1, 2, 3, 4, 5])
console.log(result)