views:

497

answers:

3

I have recently picked up golfing as a pastime. I've been trying to golf with JavaScript, and the amount of time I have spent with JavaScript 1.8 is about zero.

Are there any nifty shortcuts I might be missing out on in JavaScript between 1.5 and 1.8 that I can rely on to reduce my golf score?

+6  A: 

Expression closures:

JavaScript 1.7 and older:

function(x) { return x * x; }  

JavaScript 1.8:

function(x) x * x  

This addition is nothing more than a shorthand for writing simple functions, giving the language something similar to a typical Lambda notation. (Source and further reading)


reduce():

JavaScript 1.8 also introduces the reduce() method on Arrays. This may help you score some good golf points:

var total = [0, 1, 2, 3].reduce(function(a, b){ return a + b; });  
// total == 6 

Destructuring assignment:

In JavaScript 1.7, you can use the destructuring assignment, for example, to swap values avoiding temporary variables (Source):

var a = 1;  
var b = 3;  

[a, b] = [b, a]; 
Daniel Vassallo
+17  A: 

Expression closures (-8 to 10 chars)

function(x){return x*x;} // 1.7
function(x)x*x           // 1.8
//                     1
//           0....5....0

for each

Assuming no one extends Array.prototype,

var S=0,a=[1,2,3,4,5];for(i=0;i<a.length;++i)S+=a[i]*a[i];  // 1.5    
var S=0,a=[1,2,3,4,5];for(i in a)S+=a[i]*a[i];              // 1.5
var S=0;for each(y in[1,2,3,4,5])S+=y*y;                    // 1.6
var S=[1,2,3,4,5].reduce(function(x,y)x+y*y);                 // 1.8
//                                               1    1  1
//                                     0....56...0....5..8

Array comprehensions (-lots of chars)

var I=[0,2,-5,6,-72,44];

var O=[];for(i=0;i<I.length;++i)if(I[i]>0)O.push(I[i]); // 1.5
var O=[];for(i in I)if(I[i]>0)O.push(I[i]);             // 1.5
var O=[];for each(x in I)if(x>0)O.push(x);              // 1.6
var O=[x for each(x in I)if(x>0)];                      // 1.7
//                                         1    1    22
//                               0....5..890....5....01

Destructuring assignment

 t=A;A=B;B=t;   // 1.5
 [A,B]=[B,A];   // 1.7

 m=/<a\s+href=(['"])(.+?)\1>/.exec(src);s=m[0],l=m[2]; // 1.5
 [s,,l]=/<a\s+href=(['"])(.+?)\1>/.exec(src);          // 1.7 '
 //                                         0....5...9

Misc

  • Many functions once available only to String or Array is now usable with all objects in 1.6 via the syntax String.method(object, args).
  • Array.some, Array.every, Array.forEach, Array.indexOf and Array.lastIndexOf.

Ref:

KennyTM
+1 for adding code-rulers!!! although realisticically, some numbers are inflated by using `input` as a variable, try `z` its much more golfy ;)
gnarf
@gnarf: I've renamed `input` and `output` to `I` and `O` and hopefully this will reduce the impact of these variables appearing multiple times.
KennyTM
Maybe I'll try javasript instead of F# next round of golf.
gradbot
+2  A: 

JS 1.7 Destructuring assignment

You can do the following to shorten variable assignment:

var {x} = ...; // some object with an 'x' property

It also works in function arguments:

function a({b, reallyLongParameter:c}) b + c * c;
a({b:5, reallyLongParameter:6}) === 41;

JS 1.9.3 / ES5 Object.keys()

Safe property iteration:

for (var prop in obj) {
  if (Object.prototype.hasOwnProperty.call(obj, prop)) { // obj.hasOwnProperty isn't safe
    // do stuff
  }
}

JS 1.9.3 / ES5 equivalent:

Object.keys(obj).forEach(function (prop) {
  // do stuff
});
Eli Grey