Perhaps the most elegant way is functional composition on arrays, not
string hacking just-so imperative coding: zip composed with flatten and
join. You could even use underscore.js:
http://underscorejs.org/#flatten
http://underscorejs.org/#zip
but JS's OO standard library style prefers method chaining. Array lacks
standard zip and flatten, although concat does one level of flattening:
js> Array.prototype.flatten1 = function () { return [].concat.apply([],
this); };
(function () { return [].concat.apply([], this); })
js> Array.prototype.zip = function (b) { var r = []; var n =
Math.min(this.length, b.length) ; for (var i = 0; i < n; i++)
r.push([this[i], b[i]]); return r; }
(function (b) { var r = []; var n = Math.min(this.length, b.length) ;
for (var i = 0; i < n; i++) r.push([this[i], b[i]]); return r; })
js> ['a', 'b', 'c'].zip([1, 2, 3]).flatten1().join('');
"a1b2c3"
This is just a quick sketch. A real standard library zip would be a bit
more involved, ditto any flatten or flatten1 ;-).
/be
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss