<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Higher-Order &#187; JavaScript</title>
	<atom:link href="http://blog.higher-order.net/category/general/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.higher-order.net</link>
	<description>topics: functional programming, concurrency, web-development, REST, dynamic languages</description>
	<lastBuildDate>Mon, 19 Jul 2010 06:45:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>VersionManager</title>
		<link>http://blog.higher-order.net/2010/01/22/version-manager/</link>
		<comments>http://blog.higher-order.net/2010/01/22/version-manager/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 15:10:59 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[namespacing]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/?p=450</guid>
		<description><![CDATA[[update: First, my apologies to jdalton, my post was not meant to derail Fusebox, merely to show a different approach to similar problems. ] I read a post on Ajaxian about Fusebox, a JavaScript library which is described as: [...] The problem is that frameworks / libraries / third-party scripts may overwrite native methods or [...]]]></description>
			<content:encoded><![CDATA[<p>[update: First, my apologies to jdalton, my post was not meant to derail Fusebox, merely to show a different approach to similar problems. ]</p>
<p>I read a <a href="http://ajaxian.com/archives/de-fusing-javascript-natives-with-the-fusebox">post on Ajaxian</a> about <a href="http://github.com/jdalton/fusebox">Fusebox</a>, a JavaScript library which is described as:</p>
<blockquote><p>[...] The problem is that frameworks / libraries / third-party scripts may overwrite native methods or each other&#8217;s custom methods resulting in unpredictable outcomes. Fusebox, a limited version of the sandboxing component found in FuseJS, avoids these issues by creating sandboxed natives which can be extended without affecting the document natives.</p></blockquote>
<p>I think Fusebox is a good project. The code in this blog post shows a different approach which in some situations may be more useful and in others, it may not.</p>
<p>With Fusebox script developers avoid polluting the globals by writing their scripts using FuseBox and its wrappers. E.g, you write code with a &#8220;Fusebox prefix&#8221; like this:<br />
<code>
<pre>
  var fb = Fusebox();
  fb.Array.prototype.hai = function() {
    return fb.String("Oh hai, we have " + this.length + " items.");
  };
  fb.Array(1,2,3).hai(); // "Oh hai, we have 3 items."
  typeof window.Array.prototype.hai; // undefined

// like the native Array constructor the sandboxed constructor will return [ , , ]
  var a = fb.Array(3);
  // equiv to square-bracket notation [3]
  var b = fb.Array.create(3);
  // converting a native array to a sandboxed array
  var c = fb.Array.fromArray([1, 2, 3]);
</pre>
<p></code></p>
<p>That immediately reminded be of some work I&#8217;ve have been doing with colleague Jimmy Junker at Trifork (<tt>jju at trifork com</tt>) motivated by the following problem:</p>
<blockquote><p> In portal environments where multiple portlets want to use different JavaScript libraries or different versions of the same JavaScript libraries, you are very likely to run in to problems since almost all libraries (except later YUI versions) are designed to live in the global namespace.</p></blockquote>
<p>After a brainstorming session we developed a simple <em>prototype</em> (no pun intended), uninspiringly named &#8220;VersionManager&#8221;, with which we succeeded in loading two different versions of the PrototypeJS library. With VersionManager you can write code like this:<br />
<code>
<pre>
with (VersionManager.version("1.6.1")) {
//"1.6.1" refers to prototype which must have been loaded
  console.log("abcdaba".gsub("a","42"));// console.logs '42bcd42b42'
  console.log(typeof Object.extend);// console.logs 'function'
  $A([1,2,3])._each(function(x){
    console.log(x);
  });// console.logs '1','2','3'
}
VersionManager.clear();//otherwise previous version is lingering...
try {
  console.log(typeof "".gsub);
//console.logs 'function' <= this is generic delegating proxy function
  console.log("abcdaba".gsub("a","42"));//throws error
} catch (e) {
  console.log(e);
  //TypeError: "attempt call to delegate with no version defined (see documentation)"
}
console.log(typeof Object.extend);// console.logs 'undefined'
console.log(typeof $A);// console.logs 'undefined'
</pre>
<p></code></p>
<p>So version manager lets you load several libraries on the page, and sandboxes changes made to globals. By telling VersionManager which version you want to use for a particular block you code, you can use each of those libraries, <em>even if they define the same global variables or properties on the prototypes of objects,</em> e.g., <tt>Array.prototype</tt>. </p>
<p> VersionManager also creates sandboxes, but as opposed to Fusebox, what is sandboxed is "changes made to" the built-ins, and global variables defined. Also, the goal is not to avoid extending the prototypes, but almost the opposite: to allow several scripts/libraries that would otherwise conflict to live in the same page without seeing each other, even if they define the same global names or want to extend built-in prototypes in different ways.</p>
<p>With VersionManager you simply write regular user code, but wrap it in a with statement. This means that existing code can easily be rewritten.</p>
<p>The loaded "sandboxed" libraries must adhere to a few syntactic and semantic constraints.<br />
For an example, see a sandboxed version of prototypejs here</p>
<p><a href="http://github.com/krukow/versionmanager/blob/master/test/assets/prototype.js">Transformed prototypejs</a></p>
<p>The transformation applied to prototypejs is very simple and can be done automatically (i.e., it is easy to write a program that performs this script-transformation for you). It is a bit technical, but it results in is adding a declaration with-statement "header" at the beginning and a "footer" and the end. In fact, we are writing a program that transforms any JavaScript program/library into an equivalent one that adheres to these constraints. </p>
<p>An alpha version is on github. It is a proof-of-concept that shows that the fundamental approach seems to work. </p>
<p>jdalton pointed out a couple of things :</p>
<ul>
<li>You may run into problems with minifiers given that your code is wrapped in 'with'. However, you can minify the code first, and then wrap it.</li>
<li>It doesn't track changes to event or elements.</li>
</ul>
<p>So far, we've tested it only in latest Firefox and Safari, but I see no reason why it couldn't work in other recent browsers.  We have sucessfully loaded prototypejs in a sandbox, and managed to run all prototypejs unit tests successfully inside the sandbox.</p>
<p>Github project: <a href="http://github.com/krukow/versionmanager">http://github.com/krukow/versionmanager</a></p>
<p>/Karl</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2010/01/22/version-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jeene: An automatic partial evaluator for JavaScript</title>
		<link>http://blog.higher-order.net/2008/09/14/jeene/</link>
		<comments>http://blog.higher-order.net/2008/09/14/jeene/#comments</comments>
		<pubDate>Sun, 14 Sep 2008 19:42:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[code specialization]]></category>
		<category><![CDATA[instanceof]]></category>
		<category><![CDATA[partial evaluation]]></category>
		<category><![CDATA[automatic code specialization]]></category>
		<category><![CDATA[simplified javascript]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/?p=110</guid>
		<description><![CDATA[The purpose of this posting is to show that is is possible to create an online partial evaluator for JavaScript, written also in JavaScript. As far as I know, this has been not been done before. This post is the first in a series describing the inner workings of Jeene. A what? A partial evaluator [...]]]></description>
			<content:encoded><![CDATA[<p>The purpose of this posting is to show that is is possible to create an online partial evaluator for JavaScript, written also in JavaScript. As far as I know, this has been not been done before. This post is the first in a series describing the inner workings of <a href="http://code.google.com/p/jeene/">Jeene</a>.</p>
<p><strong>A what?</strong> A partial evaluator (or program specializer) is a program which takes two inputs: another program and an environment mapping variables to values; it outputs a specialized (i.e., more efficient) version of the input program with respect to the environment. One can think of a partial evaluator as a mix between an interpreter and a compiler: it interprets the static parts of the program and emits code for the dynamic parts. </p>
<p>For a simple example of code specialization, consider the following function for creating a string corresponding to an HTML tag:</p>
<pre><tt><span class="keyword">var</span><span class="normal"> mk_tag </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">function</span><span class="symbol">(</span><span class="normal">tag_name</span><span class="symbol">,</span><span class="normal">clazz</span><span class="symbol">,</span><span class="normal">contents</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">   </span><span class="keyword">return</span><span class="normal"> </span><span class="string">'&lt;'</span><span class="symbol">+</span><span class="normal">tag_name</span><span class="symbol">+</span><span class="string">' class="'</span><span class="symbol">+</span><span class="normal">clazz</span><span class="symbol">+</span><span class="string">'"&gt;'</span><span class="symbol">+</span><span class="normal">contents</span><span class="symbol">+</span><span class="string">'&lt;/'</span><span class="symbol">+</span><span class="normal">tag_name</span><span class="symbol">+</span><span class="string">'&gt;'</span><span class="symbol">;</span>
<span class="cbracket">}</span><span class="symbol">;</span>
</tt></pre>
<p>If we are only interested in making &#8216;div&#8217; tags with a class of &#8216;green&#8217; then a more efficient version would be:</p>
<pre><tt><span class="keyword">var</span><span class="normal"> mk_div_green </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">function</span><span class="symbol">(</span><span class="normal">contents</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">   </span><span class="keyword">return</span><span class="normal"> </span><span class="string">'&lt;div class="green"&gt;'</span><span class="symbol">+</span><span class="normal">contents</span><span class="symbol">+</span><span class="string">'&lt;/div&gt;'</span><span class="symbol">;</span>
<span class="cbracket">}</span><span class="symbol">;</span>
</tt></pre>
<p>since it would require fewer string concatenations per call. We can think of &#8216;mk_div_green&#8217; as a version of &#8216;mk_tag&#8217; which is specialized for writing &#8216;div&#8217; tags with a class of &#8216;green&#8217;. A partial evaluator for JavaScript could automatically derive the &#8216;mk_div_green&#8217; function from the &#8216;mk_tag&#8217; function.</p>
<p>This is exactly what we can do with the evaluator in this posting.</p>
<pre><tt><span class="comment">//This code actually works <img src='http://blog.higher-order.net/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </span>
<span class="normal">Function</span><span class="symbol">.</span><span class="keyword">prototype</span><span class="symbol">.</span><span class="normal">specialize </span><span class="symbol">=</span><span class="normal"> net</span><span class="symbol">.</span><span class="normal">higherorder</span><span class="symbol">.</span><span class="normal">jeene</span><span class="symbol">.</span><span class="normal">Jeene</span><span class="symbol">.</span><span class="function">make</span><span class="symbol">();</span>

<span class="keyword">var</span><span class="normal"> mk_tag </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">function</span><span class="symbol">(</span><span class="normal">tag</span><span class="symbol">,</span><span class="normal">clz</span><span class="symbol">,</span><span class="normal">cont</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">   </span><span class="keyword">return</span><span class="normal"> </span><span class="string">"&lt;"</span><span class="symbol">+</span><span class="normal">tag</span><span class="symbol">+</span><span class="string">" class='"</span><span class="symbol">+</span><span class="normal">clz</span><span class="symbol">+</span><span class="string">"'&gt;"</span><span class="symbol">+</span><span class="normal">cont</span><span class="symbol">+</span><span class="string">"&lt;/"</span><span class="symbol">+</span><span class="normal">tag</span><span class="symbol">+</span><span class="string">"&gt;"</span><span class="symbol">;</span>
<span class="cbracket">}</span><span class="symbol">;</span>

<span class="keyword">var</span><span class="normal"> mk_div_green </span><span class="symbol">=</span><span class="normal"> mk_tag</span><span class="symbol">.</span><span class="function">specialize</span><span class="symbol">(</span><span class="cbracket">{</span><span class="normal">tag</span><span class="symbol">:</span><span class="string">'div'</span><span class="symbol">,</span><span class="normal"> clz</span><span class="symbol">:</span><span class="normal"> </span><span class="string">'green'</span><span class="cbracket">}</span><span class="symbol">);</span>

<span class="function">mk_div_green</span><span class="symbol">(</span><span class="string">"Pratt rocks!"</span><span class="symbol">);</span>
<span class="comment">//result: &lt;div class='green'&gt;Pratt rocks!&lt;/div&gt;</span>

<span class="normal">mk_div_green</span><span class="symbol">.</span><span class="normal">toSource </span><span class="symbol">?</span><span class="normal"> mk_div_green</span><span class="symbol">.</span><span class="function">toSource</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">:</span><span class="normal"> mk_div_green</span><span class="symbol">.</span><span class="function">toString</span><span class="symbol">();</span>
<span class="comment">//result:</span>
<span class="comment">//(function (cont) {return ("&lt;div class='green'&gt;" + cont) + "&lt;/div&gt;";})</span></tt></pre>
<p>This last line, shows that the output function is much more efficient than what is created by general JavaScript curriers <a href="http://ajaxian.com/archives/currying-in-javascript">which have been seen before</a>: 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.</p>
<p>The first goal is for the partial evaluator to process any function written in an extension of &#8220;simplified JavaScript&#8221; (a subset of full JavaScript corresponding to what Crockford calls the &#8220;<a href="http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742">good parts</a>&#8220;). This partial evaluator will have a number of useful features: it</p>
<ul>
<li>can easily be extended to full JavaScript;</li>
<li> is written in simplified JavaScript (think <a href="http://en.wikipedia.org/wiki/Partial_evaluation">Futamura projections</a>);</li>
<li> is a fast extension of Douglas <a href="http://javascript.crockford.com/tdop/tdop.html">Crockford&#8217;s Pratt parser</a>;</li>
<li> because functions implement the toString method, it can be run on dynamically generated functions (e.g., a specialized function can be further specialized);</li>
<li> can be embedded in any full JavaScript program as long as it is only used to specialize functions which are syntactically in simplified JavaScript;</li>
</ul>
<p>I have started a new open source project, <a href="http://code.google.com/p/jeene/">Jeene</a>, which aims to create an efficient partial evaluator for full JavaScript that works in any ECMAScript 3 compliant implementation (e.g., all major browsers, Rhino, TraceMonkey, V8 etc). Right now the project is at a very early stage; a proof of concept, for example it cannot specialize itself. Let me know if you are interested in contributing: I will use a 1 patch threshold like Rubinius: if you submit one patch which is accepted, you get commit rights.</p>
<p>Stay tuned <a href="http://blog.higher-order.net">here</a> for more information about how Jeene is designed and implemented.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/09/14/jeene/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Manual code specialization:a poor-mans partial evaluation in JavaScript</title>
		<link>http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/</link>
		<comments>http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 17:46:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[code specialization]]></category>
		<category><![CDATA[partial evaluation]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[sharing]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/</guid>
		<description><![CDATA[Recall the object function that Douglas Crockford is promoting in his work on prototypal inheritance in JavaScript: function object(p) { function F(){} F.prototype = p; return new F(); } The object function creates a new object which has the input object (p) as it&#8217;s prototype. On the comp.lang.javascript newsgroup Richard Cornford showed a functionally equivalent [...]]]></description>
			<content:encoded><![CDATA[<p>Recall the object function that Douglas Crockford is promoting in his work on prototypal inheritance in JavaScript:</p>
<pre><tt><strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">object</span></strong><span style="color: #990000;">(</span>p<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
   <strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{}</span>
   F<span style="color: #990000;">.</span><strong><span style="color: #0000ff;">prototype</span></strong> <span style="color: #990000;">=</span> p<span style="color: #990000;">;</span>
   <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">();</span>
<span style="color: #ff0000;">}</span>
</tt></pre>
<p>The object function creates a new object which has the input object (p) as it&#8217;s prototype.</p>
<p>On the <a href="http://groups.google.com/group/comp.lang.javascript/topics">comp.lang.javascript newsgroup</a> Richard Cornford showed a <a href="http://groups.google.com/group/comp.lang.javascript/msg/e04726a66face2a2">functionally equivalent version</a> which has better performance in general:</p>
<pre><tt><tt><strong><span style="color: #0000ff;">var</span></strong> object <span style="color: #990000;">=</span> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{</span>
    <strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{}</span>
    <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>p<span style="color: #990000;">)</span><span style="color: #ff0000;">{</span>
        F<span style="color: #990000;">.</span><strong><span style="color: #0000ff;">prototype</span></strong> <span style="color: #990000;">=</span> p<span style="color: #990000;">;</span>
        <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">F</span></strong><span style="color: #990000;">();</span>
    <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">)();</span></tt></tt></pre>
<p>In this version, all invocations of object share the same F which has its prototype mutated with each call. Cornford argues:</p>
<blockquote><p><tt>[The first version of 'object']... is an example of the process that clearly expresses what is being done, but is not particularly efficient as it creates a new - F - function each time it is executed, but all of those - F - functions are essentially identical. If this is to be done often then a more efficient approach would be to only create a single - F - function and put it where it could not be modified by external code.</tt></p></blockquote>
<p>Now, it is important to notice that in general one has to be careful when applying this technique to recursive functions as a variable mutation in one level of recursion may affect others. Also, if we were to put threads into JavaScript, this code would go from being thread-safe in the original form to non-thread safe in the optimized form. However, for now, this technique can be applied in performance-critical functions.</p>
<p>In fact, there is a general technique here that one might call code specialization via higher-order functions (which can be seen as a poor-mans form of partial evaluation). Here is a simple example of that general technique: The &#8216;mk_tag&#8217; function creates the string for an html tag with a class attribute and a text-contents.</p>
<pre><tt><strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">mk_tag</span></strong><span style="color: #990000;">(</span>tag_name<span style="color: #990000;">,</span>clazz<span style="color: #990000;">,</span>contents<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
   <strong><span style="color: #0000ff;">return</span></strong> <span style="color: #ff0000;">'&lt;'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">' class="'</span><span style="color: #990000;">+</span>clazz<span style="color: #990000;">+</span><span style="color: #ff0000;">'"&gt;'</span><span style="color: #990000;">+</span>contents<span style="color: #990000;">+</span><span style="color: #ff0000;">'&lt;/'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">'&gt;'</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span>
</tt></pre>
<p>Using code specialization via higher-order functions (by currying), we can make specialized functions for writing e.g. &#8216;div&#8217; tags, and specialized (faster) functions for making &#8216;div&#8217; tags with certain classes. The trick is to compute as much as is possible with the given inputs before returning the specialized function:</p>
<pre><tt><em><span style="color: #9a1900;">//a curried version which specializes to it's input</span></em>
<strong><span style="color: #0000ff;">function</span></strong> <strong><span style="color: #000000;">curried_mk_tag</span></strong><span style="color: #990000;">(</span>tag_name<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
  <strong><span style="color: #0000ff;">var</span></strong> tag_hd <span style="color: #990000;">=</span> <span style="color: #ff0000;">'&lt;'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">' class="'</span><span style="color: #990000;">,</span>
      tag_tail <span style="color: #990000;">=</span> <span style="color: #ff0000;">'&lt;/'</span><span style="color: #990000;">+</span>tag_name<span style="color: #990000;">+</span><span style="color: #ff0000;">'&gt;'</span><span style="color: #990000;">;</span>
  <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>clazz<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
     <strong><span style="color: #0000ff;">var</span></strong> head <span style="color: #990000;">=</span> tag_hd<span style="color: #990000;">+</span>clazz<span style="color: #990000;">+</span><span style="color: #ff0000;">'"&gt;'</span><span style="color: #990000;">;</span>
     <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>contents<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
         <strong><span style="color: #0000ff;">return</span></strong> head<span style="color: #990000;">+</span>contents<span style="color: #990000;">+</span>tag_tail<span style="color: #990000;">;</span>
     <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
  <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span>

<strong><span style="color: #0000ff;">var</span></strong> mk_div <span style="color: #990000;">=</span> <strong><span style="color: #000000;">curried_mk_tag</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"div"</span><span style="color: #990000;">);</span>
<strong><span style="color: #0000ff;">var</span></strong> mk_div_green <span style="color: #990000;">=</span> <strong><span style="color: #000000;">mk_div</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"green"</span><span style="color: #990000;">);</span>
<strong><span style="color: #0000ff;">var</span></strong> mk_div_blue <span style="color: #990000;">=</span> <strong><span style="color: #000000;">mk_div</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"blue"</span><span style="color: #990000;">);</span>
<strong><span style="color: #000000;">mk_div_green</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"karl"</span><span style="color: #990000;">)</span><em><span style="color: #9a1900;">//&lt;-- "&lt;div class="green"&gt;karl&lt;/div&gt;"</span></em>
<strong><span style="color: #000000;">mk_div_blue</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">"karl"</span><span style="color: #990000;">)</span><em><span style="color: #9a1900;">//&lt;-- "&lt;div class="blue"&gt;karl&lt;/div&gt;"</span></em>
</tt></pre>
<p>This is elegant as functions can be reused, e.g., &#8216;mk_div_green(&#8220;karl&#8221;);mk_div_green(&#8220;krukow&#8221;)&#8217;. But notice that it is more efficient than simply using a general currier (e.g., <a href="http://www.dustindiaz.com/javascript-curry/">Diaz</a>); essentially it is a form of manual partial evaluation.</p>
<p>I&#8217;ll post some performance measurements in a later posting, but initial results show that we can reduce execution time by roughly 40% by using the sharing form of the object function.</p>
<h2>More Examples</h2>
<p>I&#8217;m not sure how many JavaScript programmers are familiar with this type of optimization. Here are a bunch of real-world examples where it can be applied:</p>
<p><em>Prototype &#8211; Ajax.request function</em></p>
<pre><tt><strong><span style="color: #0000ff;">var</span></strong> Ajax <span style="color: #990000;">=</span> <span style="color: #ff0000;">{</span><em><span style="color: #9a1900;">//original</span></em>
  getTransport<span style="color: #990000;">:</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span>
    <strong><span style="color: #0000ff;">return</span></strong> Try<span style="color: #990000;">.</span><strong><span style="color: #000000;">these</span></strong><span style="color: #990000;">(</span>
      <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">XMLHttpRequest</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
      <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Msxml2.XMLHTTP'</span><span style="color: #990000;">)</span><span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
      <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Microsoft.XMLHTTP'</span><span style="color: #990000;">)</span><span style="color: #ff0000;">}</span>
    <span style="color: #990000;">)</span> <span style="color: #990000;">||</span> <strong><span style="color: #0000ff;">false</span></strong><span style="color: #990000;">;</span>  <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>

  activeRequestCount<span style="color: #990000;">:</span> <span style="color: #993399;">0</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
</tt></pre>
<p>The thing to notice here is that every time getTransport is called prototype will recompute which XMLHttp transport to use. However, the result of Try.these will always  be the same in a particular run of Prototype, i.e., the showing of a page in one browser. So we might as well precompute which object is the correct one:</p>
<pre><tt><strong><span style="color: #0000ff;">var</span></strong> Ajax <span style="color: #990000;">=</span> <span style="color: #ff0000;">{</span><em><span style="color: #9a1900;">//modified form</span></em>
  getTransport<span style="color: #990000;">:</span> Try<span style="color: #990000;">.</span><strong><span style="color: #000000;">these</span></strong><span style="color: #990000;">(</span>
         <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">XMLHttpRequest</span></strong><span style="color: #990000;">();</span> <em><span style="color: #9a1900;">//test if it exists</span></em>
            <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">XMLHttpRequest</span></strong><span style="color: #990000;">();</span><span style="color: #ff0000;">}</span>
         <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
         <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Msxml2.XMLHTTP'</span><span style="color: #990000;">);</span> <em><span style="color: #9a1900;">//test</span></em>
            <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Msxml2.XMLHTTP'</span><span style="color: #990000;">);</span> <span style="color: #ff0000;">}</span>
         <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
         <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Microsoft.XMLHTTP'</span><span style="color: #990000;">);</span>
            <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span> <span style="color: #ff0000;">{</span><strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">new</span></strong> <strong><span style="color: #000000;">ActiveXObject</span></strong><span style="color: #990000;">(</span><span style="color: #ff0000;">'Microsoft.XMLHTTP'</span><span style="color: #990000;">);</span> <span style="color: #ff0000;">}</span>
         <span style="color: #ff0000;">}</span><span style="color: #990000;">),</span>

  activeRequestCount<span style="color: #990000;">:</span> <span style="color: #993399;">0</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">;</span></tt></pre>
<p><tt><em>jQuery - attr function</em></tt></p>
<pre><tt><tt>attr<span style="color: #990000;">:</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span> name<span style="color: #990000;">,</span> value<span style="color: #990000;">,</span> type <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
 <strong><span style="color: #0000ff;">var</span></strong> options <span style="color: #990000;">=</span> name<span style="color: #990000;">;</span>

 <em><span style="color: #9a1900;">// Look for the case where we're accessing a style value</span></em>
 <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> name<span style="color: #990000;">.</span>constructor <span style="color: #990000;">==</span> String <span style="color: #990000;">)</span>
  <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> value <span style="color: #990000;">===</span> undefined <span style="color: #990000;">)</span>
   <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">]</span> <span style="color: #990000;">&amp;</span>amp<span style="color: #990000;">;&amp;</span>amp<span style="color: #990000;">;</span> jQuery<span style="color: #990000;">[</span> type <span style="color: #990000;">||</span> <span style="color: #ff0000;">"attr"</span> <span style="color: #990000;">](</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">],</span> name <span style="color: #990000;">);</span>

  <strong><span style="color: #0000ff;">else</span></strong> <span style="color: #ff0000;">{</span>
   options <span style="color: #990000;">=</span> <span style="color: #ff0000;">{}</span><span style="color: #990000;">;</span>
   options<span style="color: #990000;">[</span> name <span style="color: #990000;">]</span> <span style="color: #990000;">=</span> value<span style="color: #990000;">;</span>
  <span style="color: #ff0000;">}</span>

  <em><span style="color: #9a1900;">// Check to see if we're setting style values</span></em>
 <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span><strong><span style="color: #000000;">each</span></strong><span style="color: #990000;">(</span><strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>i<span style="color: #990000;">)</span><span style="color: #ff0000;">{</span>
  <em><span style="color: #9a1900;">// Set all the styles</span></em>
  <strong><span style="color: #0000ff;">for</span></strong> <span style="color: #990000;">(</span> name <strong><span style="color: #0000ff;">in</span></strong> options <span style="color: #990000;">)</span>
   jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">attr</span></strong><span style="color: #990000;">(</span>    type <span style="color: #990000;">?</span>  <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span>style <span style="color: #990000;">:</span>     <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span>
    name<span style="color: #990000;">,</span> jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">prop</span></strong><span style="color: #990000;">(</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span> options<span style="color: #990000;">[</span> name <span style="color: #990000;">],</span> type<span style="color: #990000;">,</span> i<span style="color: #990000;">,</span> name <span style="color: #990000;">)</span>   <span style="color: #990000;">);</span>
 <span style="color: #ff0000;">}</span><span style="color: #990000;">);</span>
<span style="color: #ff0000;">}</span></tt></tt></pre>
<p>With jQuery we can&#8217;t write a curried form as that would break compatability. However, we can still perform optimizations like what we had with the &#8216;object&#8217; function. Notice that the function supplied to &#8216;each&#8217; is created with each invocation of &#8216;attr&#8217;, you can also see a for-loop where a check to &#8216;type&#8217; is made with each iteration. In our optimized version, attr chooses which inner function to give to &#8216;each&#8217; by checking type first.</p>
<pre><tt><tt>jQuery<span style="color: #990000;">.</span>fn<span style="color: #990000;">.</span>attr <span style="color: #990000;">=</span> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">()</span><span style="color: #ff0000;">{</span>
    <strong><span style="color: #0000ff;">var</span></strong> type<span style="color: #990000;">,</span>
        options<span style="color: #990000;">,</span>
        inner_type <span style="color: #990000;">=</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>i<span style="color: #990000;">)</span><span style="color: #ff0000;">{</span>
          <em><span style="color: #9a1900;">// Set all the styles</span></em>
          <strong><span style="color: #0000ff;">var</span></strong> t  <span style="color: #990000;">=</span> type<span style="color: #990000;">,</span>
              s  <span style="color: #990000;">=</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span>style<span style="color: #990000;">;</span>
          <strong><span style="color: #0000ff;">for</span></strong> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">var</span></strong> name <strong><span style="color: #0000ff;">in</span></strong> options <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
              jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">attr</span></strong><span style="color: #990000;">(</span>s<span style="color: #990000;">,</span>
                          name<span style="color: #990000;">,</span>
                           jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">prop</span></strong><span style="color: #990000;">(</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span> options<span style="color: #990000;">[</span> name <span style="color: #990000;">],</span> t<span style="color: #990000;">,</span> i<span style="color: #990000;">,</span> name <span style="color: #990000;">)</span>
              <span style="color: #990000;">);</span>
          <span style="color: #ff0000;">}</span>
        <span style="color: #ff0000;">}</span><span style="color: #990000;">,</span>
        inner_no_type <span style="color: #990000;">=</span> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span>i<span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
          <strong><span style="color: #0000ff;">for</span></strong> <span style="color: #990000;">(</span><strong><span style="color: #0000ff;">var</span></strong> name <strong><span style="color: #0000ff;">in</span></strong> options <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
              jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">attr</span></strong><span style="color: #990000;">(</span><strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span>
                          name<span style="color: #990000;">,</span>
                           jQuery<span style="color: #990000;">.</span><strong><span style="color: #000000;">prop</span></strong><span style="color: #990000;">(</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">,</span> options<span style="color: #990000;">[</span> name <span style="color: #990000;">],</span> <strong><span style="color: #0000ff;">null</span></strong><span style="color: #990000;">,</span> i<span style="color: #990000;">,</span> name <span style="color: #990000;">)</span>
              <span style="color: #990000;">);</span>
          <span style="color: #ff0000;">}</span>
        <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>

    <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">function</span></strong><span style="color: #990000;">(</span> name<span style="color: #990000;">,</span> value<span style="color: #990000;">,</span> t <span style="color: #990000;">)</span> <span style="color: #ff0000;">{</span>
                type <span style="color: #990000;">=</span> t<span style="color: #990000;">;</span>
                options <span style="color: #990000;">=</span> name<span style="color: #990000;">;</span>
  <em><span style="color: #9a1900;">// Look for the case where we're accessing a style value</span></em>
  <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> name<span style="color: #990000;">.</span>constructor <span style="color: #990000;">==</span> String <span style="color: #990000;">)</span>
   <strong><span style="color: #0000ff;">if</span></strong> <span style="color: #990000;">(</span> value <span style="color: #990000;">===</span> undefined <span style="color: #990000;">)</span>
    <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">]</span> <span style="color: #990000;">&amp;</span>amp<span style="color: #990000;">;&amp;</span>amp<span style="color: #990000;">;</span> jQuery<span style="color: #990000;">[</span> type <span style="color: #990000;">||</span> <span style="color: #ff0000;">"attr"</span> <span style="color: #990000;">](</span> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">[</span><span style="color: #993399;">0</span><span style="color: #990000;">],</span> name <span style="color: #990000;">);</span>

   <strong><span style="color: #0000ff;">else</span></strong> <span style="color: #ff0000;">{</span>
    options <span style="color: #990000;">=</span> <span style="color: #ff0000;">{}</span><span style="color: #990000;">;</span>
    options<span style="color: #990000;">[</span> name <span style="color: #990000;">]</span> <span style="color: #990000;">=</span> value<span style="color: #990000;">;</span>
   <span style="color: #ff0000;">}</span>

  <em><span style="color: #9a1900;">// Check to see if we're setting style values</span></em>
  <strong><span style="color: #0000ff;">return</span></strong> <strong><span style="color: #0000ff;">this</span></strong><span style="color: #990000;">.</span><strong><span style="color: #000000;">each</span></strong><span style="color: #990000;">(</span>t <span style="color: #990000;">?</span> inner_type <span style="color: #990000;">:</span> inner_no_type<span style="color: #990000;">);</span>
 <span style="color: #ff0000;">}</span><span style="color: #990000;">;</span>
<span style="color: #ff0000;">}</span><span style="color: #990000;">)();</span></tt></tt></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/07/04/manual-code-specializationa-poor-mans-partial-evaluation-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>fun with with</title>
		<link>http://blog.higher-order.net/2008/06/12/fun-with-with/</link>
		<comments>http://blog.higher-order.net/2008/06/12/fun-with-with/#comments</comments>
		<pubDate>Thu, 12 Jun 2008 18:43:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[namespacing]]></category>
		<category><![CDATA[scope]]></category>
		<category><![CDATA[using]]></category>
		<category><![CDATA[with]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/06/12/fun-with-with/</guid>
		<description><![CDATA[Read this on the old blog. Here.]]></description>
			<content:encoded><![CDATA[<p>Read this on the old blog. <a href="http://higher-order.blogspot.com/2008/06/fun-with-with.html">Here.</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/06/12/fun-with-with/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML-free Web-applications with ExtJS [Designing client/server web-apps, Part I, Section III]</title>
		<link>http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/</link>
		<comments>http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/#comments</comments>
		<pubDate>Sat, 19 Apr 2008 19:41:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ext]]></category>
		<category><![CDATA[Extjs]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[model view controller]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/</guid>
		<description><![CDATA[HTML-free Web-applications with ExtJS]]></description>
			<content:encoded><![CDATA[<p><a href="http://higher-order.blogspot.com/2008/03/html-free-web-applications-with-extjs.html">HTML-free Web-applications with ExtJS</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/04/19/html-free-web-applications-with-extjs-designing-clientserver-web-apps-part-i-section-iii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Client/Server Web-apps &#8212; the model (Part I, Section II)</title>
		<link>http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/</link>
		<comments>http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/#comments</comments>
		<pubDate>Sat, 08 Mar 2008 19:00:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Ext]]></category>
		<category><![CDATA[Extjs]]></category>
		<category><![CDATA[HTML free web-applications]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[model view controller]]></category>

		<guid isPermaLink="false">http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/</guid>
		<description><![CDATA[Client/Server Web-apps &#8212; the model]]></description>
			<content:encoded><![CDATA[<p><a href="http://higher-order.blogspot.com/2008/03/clientserver-web-apps-model-part-i.html">Client/Server Web-apps &#8212; the model</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.higher-order.net/2008/03/08/clientserver-web-apps-the-model-part-i-section-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
