Although I am not familiar with Immutable.js, the problem is that T is a template that should be defined in your documentation. See that your function really returns, this is a list from a list of numbers. So T allowed by List<Number> , and your fixed documentation will look something like this:
And you can just get rid of * and List<any> as possible return types, since your function explicitly always returns a list of lists of numbers.
What is it.
About algorithmic complexity
Note that you wrote a function whose processing time increases quadratically with parameter n . If you often call a function that passes the same value, consider memoizing its return value:
const memoizedLists = new Map(); /** * @param {Number} n * @return {List<List<Number>>} */ function getList(n) { // try to find a previous run for this same value let result = memoizedLists.get(n); if (!result) { // compute it otherwise result = Immutable.List(); for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { result.push(Immutable.List.of(i, j)); } } // memoize it for a future invocation memoizedLists.set(n, result); } return result; }
In addition, not only time increases quadratically, but also memory use. Depending on how you use it, you probably want to make your function a generator function, which will “magically” make it use constant space, i.e. No matter how large n , your function will continue to use just the same amount of memory. Here, your function has turned into a generator function:
function *getList(n) { for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { yield Immutable.List.of(i, j); } } }
To use it as a generator, you need to call him on demand. For example, if you print these pairs of numbers to some output:
for (const pair of getList(4)) { console.info(`...and here comes another pair: [${pair}]`); }
source share