Elegant way to generate string from tagged template?

Glen Huang curvedmark at gmail.com
Tue Dec 23 17:49:40 PST 2014


On second thought, make it could be just as simple as this?

function tag(templateObj, ..args) {
	return String.raw({raw: templateObj}, …args);
}

> On Dec 24, 2014, at 7:39 AM, Brendan Eich <brendan at mozilla.org> wrote:
> 
> 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
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list