var mk_tag = function(tag_name,clazz,contents) { return '<'+tag_name+' class="'+clazz+'">'+contents+'</'+tag_name+'>'; };If we are only interested in making 'div' tags with a class of 'green' then a more efficient version would be:
var mk_div_green = function(contents) { return '<div class="green">'+contents+'</div>'; };since it would require fewer string concatenations per call. We can think of 'mk_div_green' as a version of 'mk_tag' which is specialized for writing 'div' tags with a class of 'green'. A partial evaluator for JavaScript could automatically derive the 'mk_div_green' function from the 'mk_tag' function.
This is exactly what we can do with the evaluator in this posting.
//This code actually works ;-) Function.prototype.specialize = net.higherorder.jeene.Jeene.make(); var mk_tag = function(tag,clz,cont) { return "<"+tag+" class='"+clz+"'>"+cont+"</"+tag+">"; }; var mk_div_green = mk_tag.specialize({tag:'div', clz: 'green'}); mk_div_green("Pratt rocks!"); //result: <div class='green'>Pratt rocks!</div> mk_div_green.toSource ? mk_div_green.toSource() : mk_div_green.toString(); //result: //(function (cont) {return ("<div class='green'>" + cont) + "</div>";})This last line, shows that the output function is much more efficient than what is created by general JavaScript curriers which have been seen before: these functions merely wait evaluating the function until all parameters are supplied; instead, a partial evaluator will create specialized function taking advantage of the information given. The first goal is for the partial evaluator to process any function written in an extension of "simplified JavaScript" (a subset of full JavaScript corresponding to what Crockford calls the "good parts"). This partial evaluator will have a number of useful features: it
- can easily be extended to full JavaScript;
- is written in simplified JavaScript (think Futamura projections);
- is a fast extension of Douglas Crockford's Pratt parser;
- because functions implement the toString method, it can be run on dynamically generated functions (e.g., a specialized function can be further specialized);
- can be embedded in any full JavaScript program as long as it is only used to specialize functions which are syntactically in simplified JavaScript;