memoization 이해 by factorial

Dan.kimhaejun·2020년 12월 30일
0

Try Code First

목록 보기
3/3

memoization, factorial 간편 생성

const memo = (func) => {

  const memo = {};

  return index => {
    console.log('memo[index]', memo[index]);

   return memo[index] ??= func(index)
  }
}

const fac = (num) => {
  if (num < 2) {
    return 1;
  }

  return fac(num-1) * num
}

Try it

const memoFac = memo(fac);
memoFac(3)
memoFac(3)
profile
제가 겪은 이슈에 대해서 정리합니다. 기억보다는 기록이 더 낫다고 생각합니다.

0개의 댓글