함수형 프로그래밍 예제(장바구니)

joDMSoluth·2020년 3월 13일
0

함수형프로그래밍

목록 보기
6/17
const products = [
  { name: '반팔티', price: 15000, quantity: 1 },
  { name: '긴팔티', price: 20000, quantity: 2 },
  { name: '휴대폰케이스', price: 15000, quantity: 3 },
  { name: '후드티', price: 30000, quantity: 4 },
  { name: '바지', price: 25000, quantity: 5 },
];
const add = (a, b) => a + b;

const sum = curry((f, iter) => go(
  iter,
  map(f),
  reduce(add)));


const total_quantity = sum(p => p.quantity);
// const total_quantity = products => sum(p => p.quantity, products);
console.log(total_quantity(products));


const total_price = sum(p => p.price * p.quantity);
// const total_price = products => sum(p => p.price * p.quantity, products);
console.log(total_price(products));
profile
풀스택이 되고 싶은 주니어 웹 개발자입니다.

0개의 댓글