For a challenge, I recently submitted the JavaScript solution
[].push
To be clear, this isn't x=>[].push
. The entry itself is a function: it's the function identified by the JavaScript expression [].push
.
The critical complication is that it wouldn't work to place this function in a variable. this would not work:
f=[].push;
f(...);
This is because push
needs to be accessed as a method (i.e., a property accessed from some parent value) in the lexical invocation expression where it is called in order for it to push into the correct array. A correct invocation must be [].push(...)
(or, to separate the answer from the input, ([].push)(...)
would be fine, too). The "bare" invocation f(...)
does not include property access, so the push
function is called but does not work correctly.
If this is allowed, are more complex answers of the same form allowed, e.g.:
{
x: 5,
f: function() { return Math.dothing(this.x) }
}.f
where you define an object with properties and then conclude with .f
so that the whole expression returns that single function?
f=[].push; f();
doesn't work makes me hesitant, and also makes me wonder what the challenge was. Can you post a link to that challenge/answer? \$\endgroup\$[].push(...)
any number of times with correct results, at least to my cursory testing \$\endgroup\$([].push)(1,2,3); ([].push)(4,5,6,7);
give correct results for each run. \$\endgroup\$