By amaurymartiny


2018-09-14 15:39:30 8 Comments

I can memoize a function with:

  • const f = memoize(function(){/* ... */})
  • const f = memoize(() => {/* ... */})

However, for the purpose of generating nice docs, I need to use the syntax:

function f() {
  /* ... */
}

How can I memoize that?

3 comments

@Matt McCutchen 2018-09-14 22:15:27

Do the const f1 = memoize(function(){/* ... */}) or const f1 = memoize(() => {/* ... */}) as you prefer, and then write function f() { ... } that just calls f1 with its parameters and returns the result.

@amaurymartiny 2018-09-15 17:19:03

In my docs, I would then have the function definition of f1.

@Matt McCutchen 2018-09-15 20:19:14

I don't understand. If this is the typical JSDoc kind of thing and you put the documentation comment on f, then f should appear in your documentation. Or what tool are you using?

@amaurymartiny 2018-09-16 12:00:08

Typedoc, and they don't document prettily const f = ... syntax. If I put doc comments on f1, then f1 shows in the docs. Though getting kind of off-topic.

@Matt McCutchen 2018-09-18 02:05:19

Might you have read the answer backwards? I suggested to use const f1 = ... and then function f() { ... }.

@amaurymartiny 2018-09-18 08:31:27

Ah right, you're totally correct, my bad! Upvoted your answer, though I still prefer @Jonas's.

@Jonas Wilms 2018-09-14 15:45:10

just append the line:

 function f() {
   /* ... */
 }
 // @ts-ignore
 f = memoize(f);

@amaurymartiny 2018-09-14 15:46:21

[ts] Cannot assign to 'f' because it is not a variable.

@slebetman 2018-09-14 15:47:33

@amaurymartiny You didn't mention you're using typescript

@amaurymartiny 2018-09-14 15:48:44

Right, sorry about that. Added.

@Bergi 2018-09-14 15:47:27

Like @Jonas said, you can simply overwrite the declared variable afterwards (f = memoise(f)). A modern alternative would be to use the proposed and experimental decorators together with your favourite transpiler, and write

@memoise
function f() {
  /* ... */
}

@amaurymartiny 2018-09-14 16:00:45

Would be a syntax I am looking for. However, tc39 doesn't address decorators for functions. github.com/tc39/proposal-decorators/issues/40

@Bergi 2018-09-14 16:37:00

@amaurymartiny Oh dammit, you're of course right. I suppose decorators used in Typescript have the same limitation?

Related Questions

Sponsored Content

4 Answered Questions

[SOLVED] Are strongly-typed functions as parameters possible in TypeScript?

  • 2013-02-01 02:56:27
  • vcsjones
  • 148657 View
  • 350 Score
  • 4 Answer
  • Tags:   typescript

32 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

5 Answered Questions

[SOLVED] TypeScript function overloading

3 Answered Questions

[SOLVED] What is the syntax for Typescript arrow functions with generics?

  • 2015-08-31 09:46:38
  • Andreas Frische
  • 10065 View
  • 30 Score
  • 3 Answer
  • Tags:   typescript

3 Answered Questions

[SOLVED] ECMAScript6 arrow function that returns an object

7 Answered Questions

4 Answered Questions

[SOLVED] Reactjs setState arrow function syntax

7 Answered Questions

[SOLVED] How do I write a named arrow function in ES2015?

2 Answered Questions

2 Answered Questions

[SOLVED] Can a memoize function use a ES6 Map vs a hashtable?

Sponsored Content