Tuesday, 15 March 2011

for loop with range in CoffeeScript -



for loop with range in CoffeeScript -

noob question. trying write loop range. example, want produce in javascript:

var i, a, j, b, len = arr.length; (i = 0; < len - 1; i++) { = arr[i]; (j = + 1; < len; j++) { b = arr[j]; dosomething(a, b); } }

the closest i've come far following, but

it generates unnecessary , expensive piece calls accesses array length within inner loop

coffeescript:

for a, in a[0...a.length-1] b, j in a[i+1...a.length] dosomething a, b

generated code:

var a, b, i, j, _i, _j, _len, _len1, _ref, _ref1; _ref = a.slice(0, a.length - 1); (i = _i = 0, _len = _ref.length; _i < _len; = ++_i) { = _ref[i]; _ref1 = a.slice(i + 1, a.length); (j = _j = 0, _len1 = _ref1.length; _j < _len1; j = ++_j) { b = _ref1[j]; dosomething(a, b); } }

(how) can expressed in coffeescript?

basically, transcribing first js code cs:

len = arr.length in [0...len - 1] 1 = arr[i] j in [i + 1...len] 1 b = arr[j] dosomething a, b

coffeescript

No comments:

Post a Comment