0
1
Introduction
I have some JavaScript code that uses 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]
Challenge
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.
Note that 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 1
and 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.
Scoring
The best answer is the one that is represented by the fewest tokens when lexed. The ANTLR 4 JavaScript lexer is the official lexer for the sake of objectivity, but if you know how a lexer works you should be able to estimate how many tokens some JavaScript needs without having to read that link. For example, fn => fn()
is composed of five tokens: fn
=>
fn
(
)
.
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.