
기능개발 문제를 함수형 프로그래밍으로 풀어보자.
function solution(progresses, speeds) {
const [answer] = go(
zip(progresses)(speeds),
map(([progress, speed]) => countPeriod(progress, speed)),
deploymentCount
);
return answer;
}
한줄씩 뜯어서 살펴보도록 하자.
zip(progresses)(speeds)zip을 이용해 progresses와 speeds의 값을 하나씩 받아온다.
map(([progress, speed]) => countPeriod(progress, speed))
zip에서받아온 진도와개발 속도를
countPeriod함수를 이용하여 새로운 객체 만든다.
countPeriod : 배포까지 걸리는 시간(작업일)을 계산하여 반환하는 함수
const countPeriod = (progress, speed) => {
return Math.ceil((100 - progress) / speed);
};
문제 해결의 핵심 함수이다.
deploymentCount 는 작업일 배열을 받아 문제에 맞춰 적합하게 배포일을 만들어나간다.
const deploymentCount = (periods) =>
reduce(([counts, total_period], period) =>
total_period < period ?
[inc(counts, counts.length), period]
: [inc(counts, counts.length - 1), total_period]
)([[], 0], periods);
zip을 한 결과가 periods 배열이라고 가정하면
periods =
[5, 10, 1, 1, 20, 1]
결과 =[ [ 1, 3, 2 ], 20 ]
reduce(f)([[], 0], periods) : periods를 함수f에 대해 reduce 실행
[counts, total_period], period =>
total_period < period ?
[inc(counts, counts.length), period]
: [inc(counts, counts.length - 1), total_period]
디버깅을 통해서 결과출력 과정을 자세히 살펴보자.
total_period < period의 값이true이므로
counts = inc(counts = [] , count.length = 0) // counts가 빈 배열이므로 counts = [1]이 된다.
total_period = period = 5
function inc(parent, k) {
parent[k] ? parent[k]++ : (parent[k] = 1);
return parent;
}

total_period < period의 값이true이므로
counts = inc(counts = [1] , count.length = 1) // counts = [1, 1]
total_period = period = 10

total_period < period의 값이false이므로
counts = inc(counts = [1, 1], counts.length - 1 = 1) // 함께 배포할 기능 개수를 증가한다. counts = [1, 2]
total_period = 10

total_period < period의 값이false이므로
counts = inc(counts = [1, 2], counts.length - 1 = 1) // 함께 배포할 기능 개수를 증가한다. counts = [1, 3]
total_period = 10

total_period < period의 값이true이므로
counts = inc(counts = [1, 3], counts.length = 2) // counts = [1, 3, 1]
total_period = 20

total_period < period의 값이false이므로
counts = inc(counts = [1, 3, 1], counts.length - 1 = 2) // 함께 배포할 기능 개수를 증가한다. counts = [1, 3, 2]
total_period = 20

멘토님이 추천해주신 이유가 있었군요!👍
글이 가독성이 좋아 이해 하는데 정말 많은 도움 도었습니다. 좋은포스팅 감사합니다!!!!!!😄