<?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; partial evaluation</title>
	<atom:link href="http://blog.higher-order.net/category/partial-evaluation/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>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>
	</channel>
</rss>
