How can I convert this large factorial function to a higher-order function?

wonderful world

The following code uses a cache object outside of the factorial function. The factorial function itself is large which has too many concerns of finding factorial and caching.

How can I convert this code to a higher-order function and generate the same result when I call

console.log(factorial(5));
console.log(factorial(7));

cache = { }

function factorial(n) {
  
  if (n === 0) {
    return 1;
  }
  
  if (cache[n])
  {
    return cache[n];
  }
  
  console.log("Stack Up: " + n);
  
  var value = n * factorial(n - 1);
  
  console.log("Stack Down: " + value);
  
  cache[n] = value;
  
  return value;
}

console.log(factorial(5));
console.log(factorial(7));

Thank you

There's already other answers out there for memoising recursive functions, but I'll adapt that answer to factorial in javascript so you can see how it works more easily

The secret to writing memoised recursive functions is continuation passing style. A similar technique works when you want to make a non-tail recursive function stack-safe.

I'll leave some console.log statements in this first example so you can see when it's actually computing and when it's just doing a memo lookup.

const memoise = f => {
  const memo = new Map()
  const compute = (x, k) =>
    (console.log('compute', x),
    memo.get(x, memo.set(x, f(x,k))))
  const lookup = x =>
    (console.log('lookup', x),
    memo.has(x) ? memo.get(x) : compute(x, lookup))
  return lookup
}

const factk = (x, k) => {
  if (x === 0)
    return 1
  else
    return x * k(x - 1)
}

const memfact = memoise(factk)

console.log(memfact(5)) // 120
console.log(memfact(7)) // 5040


Here I've removed the console.log calls inside of memoise and instead demonstrate a memoised fibonacci function vs an unmemoised one. Compare the dramatic time difference between memoise(fibk) and badfib

const memoise = f => {
  const memo = new Map()
  const compute = (x, k) =>
    memo.get(x, memo.set(x, f(x,k)))
  const lookup = x =>
    memo.has(x) ? memo.get(x) : compute(x, lookup)
  return lookup
}

const fibk = (x, k) => {
  if (x < 2)
    return x
  else
    return k(x - 1) + k(x - 2)
}

const badfib = x => {
  if (x < 2)
    return x
  else
    return badfib(x - 1) + badfib(x - 2)
}

console.time('memoised')
console.log(memoise (fibk) (35)) // 9227465 1.46ms
console.timeEnd('memoised')

console.time('unmemoised')
console.log(badfib(35))          // 9227465 135.85ms
console.timeEnd('unmemoised')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Please explain how I can make this higher order function work

From Dev

How can I mock a higher order function in Jest

From Dev

How can you loop this higher-order function in R?

From Dev

How can I unit test for a stack overflow in my factorial function?

From Dev

How to convert this function/for loop into a list comprehensionor higher order function using python?

From Dev

How to access to the parameter of the higher order function in scala

From Dev

Higher order filter function

From Dev

higher order function in javascript

From Dev

Higher order function in scala

From Dev

Can I Create a Higher Order Function With Funciton Parameters from Different Classes?

From Dev

How to apply higher order function to an effectful function in Haskell?

From Dev

How can I convert a string into a function in Python?

From Dev

How can I convert a combination function to permutation function?

From Dev

How can I convert recursive function to iterative function?

From Dev

Higher-order function and parentheses

From Dev

Java streaming higher order function

From Dev

higher order function python walkthrough

From Dev

Higher order function in Python exercise

From Dev

higher-order-function toList

From Dev

Higher Order Function with `Val` and `Def`

From Dev

Factorial function returning negative number for large input

From Dev

Kotlin higher order function for passing a function to a map

From Dev

How does this function modify the other? (Higher Order Functions)

From Dev

How do rewrite this higher order function without using static functions?

From Dev

haskell: factorial using high order function

From Dev

haskell: factorial using high order function

From Dev

How can I swap the order of application in a defined F# function?

From Dev

How can I create a function for the color in decreasing order

From Dev

Why can't the F# compiler fully inline function arguments of a higher order function?

Related Related

  1. 1

    Please explain how I can make this higher order function work

  2. 2

    How can I mock a higher order function in Jest

  3. 3

    How can you loop this higher-order function in R?

  4. 4

    How can I unit test for a stack overflow in my factorial function?

  5. 5

    How to convert this function/for loop into a list comprehensionor higher order function using python?

  6. 6

    How to access to the parameter of the higher order function in scala

  7. 7

    Higher order filter function

  8. 8

    higher order function in javascript

  9. 9

    Higher order function in scala

  10. 10

    Can I Create a Higher Order Function With Funciton Parameters from Different Classes?

  11. 11

    How to apply higher order function to an effectful function in Haskell?

  12. 12

    How can I convert a string into a function in Python?

  13. 13

    How can I convert a combination function to permutation function?

  14. 14

    How can I convert recursive function to iterative function?

  15. 15

    Higher-order function and parentheses

  16. 16

    Java streaming higher order function

  17. 17

    higher order function python walkthrough

  18. 18

    Higher order function in Python exercise

  19. 19

    higher-order-function toList

  20. 20

    Higher Order Function with `Val` and `Def`

  21. 21

    Factorial function returning negative number for large input

  22. 22

    Kotlin higher order function for passing a function to a map

  23. 23

    How does this function modify the other? (Higher Order Functions)

  24. 24

    How do rewrite this higher order function without using static functions?

  25. 25

    haskell: factorial using high order function

  26. 26

    haskell: factorial using high order function

  27. 27

    How can I swap the order of application in a defined F# function?

  28. 28

    How can I create a function for the color in decreasing order

  29. 29

    Why can't the F# compiler fully inline function arguments of a higher order function?

HotTag

Archive