Calling a returned function

Michael Vannucci

So I am trying to write a general memoizing function in Javascript, using this as my reference material. I've implemented it as instructed:

function memoize(func) {
  var memo = {};
  var slice = Array.prototype.slice;

  return function() {
    var args = slice.call(arguments);

    if (args in memo)
      return memo[args]
    else
      return (memo[args] = func.apply(this, args));
  }
}

function fibonacci(n) {
  if (n === 0 || n === 1)
    return n;
  else
    return fibonacci(n - 1) + fibonacci(n - 2);
}

function memoFib = memoize(fibonacci);

console.log("Fibonacci of 6 is " + memoFib(6));

but for the life of me I cannot remember the proper syntax to calling the memoized function. How do I do this? How do I generalize this so that it's the memoized fibonacci function which is always called instead of the original function fibonacci?

Nicholas Tower

You're almost there; you just need to fix your syntax when creating the memoized function. Instead of function memoFib =, do var memoFib = (or if ES2015 is an option, const would be better than var). And presumably you'll want to have the recursion calling into the memoized version as well, so i updated those as well. In the following code it may look weird that i'm referencing memoFib on a line that comes earlier than the line with var memoFib but javascript allows that due to hoisting.

function memoize(func) {
  var memo = {};
  var slice = Array.prototype.slice;

  return function() {
    var args = slice.call(arguments);

    if (args in memo)
      return memo[args]
    else
      return (memo[args] = func.apply(this, args));
  }
}

function fibonacci(n) {
  if (n === 0 || n === 1)
    return n;
  else
    return memoFib(n - 1) + memoFib(n - 2);
}

var memoFib = memoize(fibonacci);

console.log("Fibonacci of 6 is " + memoFib(6));

And if you want to make absolutely sure no one can ever invoke the nonmemoized version, then you can hide it inside of an IIFE:

const fibonacci = (function () {
  function unmemoizedVersion(n) {
    if (n === 0 || n === 1)
      return n;
    else
      return fibonacci(n - 1) + fibonacci(n - 2);
  }
  return memoize(unmemoizedVersion);
})();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a returned function gives "not a function"

From Javascript

Calling a JavaScript function returned from an Ajax response

From Java

Calling a function when an activity is returned to from fragment

From Dev

Calling another returned function in the same module

From Dev

calling begin and end on vector returned by member function

From Dev

Calling C function from returned pointer in NodeJS

From Dev

C++: Calling a function on a returned class

From Dev

Combine dataframes returned from python multiprocess calling function

From Dev

What happens to the ownership of a value returned but not assigned by the calling function?

From Dev

unexpected integer returned from a C function by calling it using ctypes

From Dev

When calling same Rcpp function several times different results are returned

From Dev

From a tuple of types to an array of values returned by calling a template function

From Dev

What is returned by calling 'where'?

From Dev

How and where does a function save the 'returned value of one function' before calling other functions inside its body?

From Dev

Inline function with if/else: check returned value and append to list on condition, without calling function twice

From Dev

How to pass a local variable to a callback while getting something returned by the calling function?

From Dev

Calling a jQuery function on an HTML element returned from AngularJS's ng-view directive

From Dev

Unhandled exception after reading from a binary file, when control is returned to calling function

From Dev

Calling function object returned doesn't show same results? Python3.x

From Dev

Google Cloud Scheduler calling Google Cloud Function when Function has succeeded (returned 200 status) still gets 500 status

From Dev

Allocation function and pointer to returned

From Dev

Function returned without value

From Dev

dispatch argument to returned function

From Dev

Invoking procedure returned by function

From Dev

What is the value returned by this function?

From Dev

"Undefined" returned at end of the function

From Dev

undefined is returned from function

From Dev

Promise is not immediately returned for function

From Dev

Future returned by a function

Related Related

  1. 1

    Calling a returned function gives "not a function"

  2. 2

    Calling a JavaScript function returned from an Ajax response

  3. 3

    Calling a function when an activity is returned to from fragment

  4. 4

    Calling another returned function in the same module

  5. 5

    calling begin and end on vector returned by member function

  6. 6

    Calling C function from returned pointer in NodeJS

  7. 7

    C++: Calling a function on a returned class

  8. 8

    Combine dataframes returned from python multiprocess calling function

  9. 9

    What happens to the ownership of a value returned but not assigned by the calling function?

  10. 10

    unexpected integer returned from a C function by calling it using ctypes

  11. 11

    When calling same Rcpp function several times different results are returned

  12. 12

    From a tuple of types to an array of values returned by calling a template function

  13. 13

    What is returned by calling 'where'?

  14. 14

    How and where does a function save the 'returned value of one function' before calling other functions inside its body?

  15. 15

    Inline function with if/else: check returned value and append to list on condition, without calling function twice

  16. 16

    How to pass a local variable to a callback while getting something returned by the calling function?

  17. 17

    Calling a jQuery function on an HTML element returned from AngularJS's ng-view directive

  18. 18

    Unhandled exception after reading from a binary file, when control is returned to calling function

  19. 19

    Calling function object returned doesn't show same results? Python3.x

  20. 20

    Google Cloud Scheduler calling Google Cloud Function when Function has succeeded (returned 200 status) still gets 500 status

  21. 21

    Allocation function and pointer to returned

  22. 22

    Function returned without value

  23. 23

    dispatch argument to returned function

  24. 24

    Invoking procedure returned by function

  25. 25

    What is the value returned by this function?

  26. 26

    "Undefined" returned at end of the function

  27. 27

    undefined is returned from function

  28. 28

    Promise is not immediately returned for function

  29. 29

    Future returned by a function

HotTag

Archive