Array.prototype.map to map an array of functions
fns to their return values:
const fns = [() => 1, () => 2]; const result = fns.map( fn => fn() ); console.log(result); // => [1, 2]
The argument to
map above is
fn => fn(). The challenge is to rewrite this function using point-free style. Your solution should work when it replaces the third line of the above program:
const fns = [() => 1, () => 2]; const result = fns.map( /* your solution should work when inserted here */ ); console.log(result); // => [1, 2]
fn => fn() is not a valid solution because it defines a parameter named
fn. Writing a function in point-free style requires writing it without any variable or parameter names.
map will pass three arguments to your solution function:
currentValue: () => T,
index: number, and
array: Array<() => T>. The function for the solution must return the result of calling the
currentValue function, so hard-coding
2 will not help. The functions being mapped over are guaranteed to ignore their arguments and their
this value, so calling the function in any way will work.
fn => fn() is composed of five tokens:
However, I personally think that figuring out any answer is the fun part, so if this problem interests you, try to solve it yourself before scrolling down and seeing the answer.