<?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>Artful Code &#187; javascript</title>
	<atom:link href="http://www.artfulcode.net/tags/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Updated Json module for newLISP</title>
		<link>http://www.artfulcode.net/articles/updated-json-module-for-newlisp/</link>
		<comments>http://www.artfulcode.net/articles/updated-json-module-for-newlisp/#comments</comments>
		<pubDate>Fri, 22 May 2009 18:10:01 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>
		<category><![CDATA[newlisp]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/?p=643</guid>
		<description><![CDATA[Thanks to Andrew Pennebaker for pointing out where the decoder was broken. This update includes a complete rewrite of the JSON decoder. It is now slightly more forgiving and much more accurate. You can download it here or upgrade using nl-install. Submit article]]></description>
			<content:encoded><![CDATA[<p>Thanks to Andrew Pennebaker for pointing out where the decoder was broken. This update includes a complete rewrite of the JSON decoder. It is now slightly more forgiving and much more accurate. You can download it <a href="http://static.artfulcode.net/newlisp/json.lsp.html">here</a> or upgrade using <a href="http://static.artfulcode.net/nl-install/packages.lsp">nl-install</a>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Updated+Json+module+for+newLISP&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F&amp;title=Updated+Json+module+for+newLISP" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Updated+Json+module+for+newLISP+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fupdated-json-module-for-newlisp%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/updated-json-module-for-newlisp/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An alternative to large multi-selects</title>
		<link>http://www.artfulcode.net/articles/alternative-large-multi-selects/</link>
		<comments>http://www.artfulcode.net/articles/alternative-large-multi-selects/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 15:15:59 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[releases]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/alternative-large-multi-selects/</guid>
		<description><![CDATA[HTML has no useful, built-in form widget for selecting from a large number of options. Some libraries offer Javascript-powered alternatives, but few of these are built to deal with more than a few hundred options. The problem I recently worked on a project with a backend using the Django admin library. It is a wonderful [...]]]></description>
			<content:encoded><![CDATA[<p>HTML has no useful, built-in form widget for selecting from a large number of options.  Some libraries offer Javascript-powered alternatives, but few of these are built to deal with more than a few hundred options.<span id="more-12"></span></p>
<h4>The problem</h4>
<p>I recently worked on a project with a backend using the Django admin library.  It is a wonderful tool, but the <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/#filter-horizontal">filter interface</a> is not equipped to deal with the thousands of options that were being populated in this particular form entry (a necessary parameter of the project.)</p>
<p>The Django filter interface would take 30 seconds or more to load the multi-select&#8217;s data on each form load, even on speedy computers.  Moreover, the search filter breaks up queries at spaces, then performs a substring search for each token for every option in the select.  Suffice it to say that this locked up more than a few browsers in our testing.</p>
<h4>The solution</h4>
<p>My first thought was to create my own search using a ternary tree or trie to index the data, but building these structures turned out to be <em>extremely</em> slow implemented in Javascript, taking even more time than the Django filter to initialize.</p>
<p>I ended up trying what seemed like a foolish idea.  Create one large string to search by joining all of the options&#8217; labels together (and delimiting with pipes).  I assumed this would take a huge amount of memory, but in fact it took less than Javascript trees and was <em>much</em> faster than operating directly on DOM nodes.  By delimiting the strings, a bounded regular expression could be used to perform reasonably fast searches of the data.  There would be the need to identify the options to which the strings originated, for which I created a simple hash mapping option label to value.</p>
<p>This solution has two main problems &#8211; it assumes that all entries in the select box have <em>both</em> a unique label and a unique value, and no label can have a pipe in it.  However, assuming that these conditions are met, this solution seems to work pretty speedily, and without overtaxing the browser.</p>
<h4>The code</h4>
<p>This code is implemented as a <a href="http://www.jquery.com">jQuery</a> plugin.  It hides the select and changes made to the widget automatically update the select.  A search box and two lists are inserted into the document; the user enters a partial match in the search box and hits return.  The first list is populated with the search results.  Double-clicking on a result copies it into the second list.</p>
<p>Items that are already selected are automatically put into the second list.  Regular expressions are permitted, and must be surrounded by forward slashes.</p>
<p>This module has been lightly tested in Firefox 2 and 3, Safari (latest), Opera (latest), and IE 7.  A few concessions in favor of efficiency have been made at the expense of clarity (mainly for IE, which seems to have the slowest Javascript engine), primarily in the SelectMap constructor.  jQuery&#8217;s each method and string concatenation proved to be too much for IE, so they were replaced with a for loop over the select node&#8217;s legacy <code>options</code> array.  Iterative string building was replaced with <code>Array.join</code>, which is faster (but more expensive in memory) in IE.</p>
<p>To use it:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> select <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> BigSelect<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'target_id'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// or...</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#target_id'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bigSelect</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The code is available <a title="Artful Code Projects" href="http://www.artfulcode.net/projects/">here</a>.  <span style="text-decoration: line-through;">You can see a live example here.</span></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F&amp;title=An+alternative+to+large+multi-selects" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F&amp;title=An+alternative+to+large+multi-selects" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=An+alternative+to+large+multi-selects&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F&amp;title=An+alternative+to+large+multi-selects" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F&amp;title=An+alternative+to+large+multi-selects" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F&amp;title=An+alternative+to+large+multi-selects" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+An+alternative+to+large+multi-selects+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Falternative-large-multi-selects%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/alternative-large-multi-selects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Partial application and currying</title>
		<link>http://www.artfulcode.net/articles/partial-application-and-currying/</link>
		<comments>http://www.artfulcode.net/articles/partial-application-and-currying/#comments</comments>
		<pubDate>Tue, 04 Dec 2007 18:33:00 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[functional]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/partial-application-and-currying/</guid>
		<description><![CDATA[Currying, known in Python land as partial application, is a technique in which a function taking multiple arguments composes a function that takes fewer arguments (in most languages, reducing to one, although this is not the case in Python) by partially applying it to given parameters. For example, a function, sum, might be used to [...]]]></description>
			<content:encoded><![CDATA[<p>Currying, known in Python land as partial application, is a technique in which a function taking multiple arguments composes a function that takes fewer arguments (in most languages, reducing to one, although this is not the case in Python) by partially applying it to given parameters.  For example, a function, sum, might be used to compose a new function called &#8220;plus_one&#8221; by currying it with the value of one.  The composed function is not evaluated; it is returned as a function object which may then be applied to other parameters.<span id="more-41"></span></p>
<p>Python&#8217;s partial() is contained in the <a href="http://docs.python.org/lib/module-functools.html">functools module</a> (included since Python 2.5, I believe).  The first argument passed must be the function to be curried, and the rest are positional or keyword arguments that will be used to curry the passed function.</p>
<p>A common case where partials are useful is in defining a compare function for a list.  It often happens that the list must be sorted according to rules defined at runtime.  A partial application can simplify the process, especially if there is a complex sort algorithm.  Assume a list of Items, items.  The list will be sorted according to get_sort_attribute(), which returns the name of the attribute of Item which will be used to perform the sort, and get_sort_direction, which returns either &#8220;desc&#8221; or &#8220;asc.&#8221;  Rather than using a long series of if/else statements and calling the sort() method in various ways, partial() can be used to progressively modify the sort.</p>
<p>To sort a list this way, we might have something like:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">def</span> compare<span style="color: black;">&#40;</span>a, b<span style="color: black;">&#41;</span>:
    attr = get_sort_attribute<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    <span style="color: #008000;">dir</span> = get_sort_direction<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    value_a = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>a, attr<span style="color: black;">&#41;</span>
    value_b = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>b, attr<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">dir</span> == <span style="color: #483d8b;">'desc'</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>value_b, value_a<span style="color: black;">&#41;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">cmp</span><span style="color: black;">&#40;</span>value_a, value_b<span style="color: black;">&#41;</span>
items.<span style="color: black;">sort</span><span style="color: black;">&#40;</span>compare<span style="color: black;">&#41;</span></pre></div></div>

<p>This can get pretty long-winded, especially if our function, compare, performs complex operations before performing the comparison.  Here is the same thing using partial applications to compose our function instead:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> functools <span style="color: #ff7700;font-weight:bold;">import</span> partial
&nbsp;
<span style="color: #808080; font-style: italic;"># Wrap getattr in a lambda so that can accept keyword arguments</span>
attribute_getter = partial<span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">lambda</span> <span style="color: #008000;">object</span>, name:
    <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">object</span>, name<span style="color: black;">&#41;</span>, name=get_sort_attribute<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
sort_fn = partial<span style="color: black;">&#40;</span>items.<span style="color: black;">sort</span>, key=attribute_getter<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> get_sort_direction<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> == <span style="color: #483d8b;">'desc'</span>:
    sort_fn = partial<span style="color: black;">&#40;</span>sort_fn, <span style="color: #008000;">reversed</span>=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
sort_fn<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This is certainly more concise.  I found a nice little currying function for Javascript <a href="http://www.dustindiaz.com/javascript-curry/">here</a> (<strong>edit:</strong> it was pointed out in a reader&#8217;s comment (here and at <a href="http://www.dzone.com/links/partial_application_and_currying.html">dzone</a>) that this version of curry does not work on previously curried functions; below it is a modified version which will function as expected):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
function curry(fn, scope) {
    var scope = scope || window;
    var args = [];
    for (var i=2, len = arguments.length; i &amp;lt; len; ++i) {
        args.push(arguments[i]);
    };
    return function() {
        /* one big problem here is that the following statement
        is not returning the applied function. */</span>
        fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">*/</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> curry<span style="color: #009900;">&#40;</span>fn<span style="color: #339933;">,</span> scope<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> scope <span style="color: #339933;">=</span> scope <span style="color: #339933;">||</span> window<span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> args <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// this takes care of the arguments problem</span>
        <span style="color: #003366; font-weight: bold;">var</span> fn_args <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> args.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fn_args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> arguments.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fn_args.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>arguments<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #006600; font-style: italic;">// this takes care of the null return problem</span>
        <span style="color: #000066; font-weight: bold;">return</span> fn.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>scope<span style="color: #339933;">,</span> fn_args<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I often use this in my Django web projects.  I will include a basic error function which inserts an error message into an element with a particular id on the base template.  Note that I use jQuery in the following examples.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> err<span style="color: #009900;">&#40;</span>target<span style="color: #339933;">,</span> msg<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> target<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>msg<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In templates that extend that template, I can then use currying to modify that for a particular location defined in this template:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> err <span style="color: #339933;">=</span> curry<span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> window<span style="color: #339933;">,</span> <span style="color: #3366CC;">'err_div_in_this_template'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I can further use this to create custom error callbacks for ajax functions:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> err404 <span style="color: #339933;">=</span> curry<span style="color: #009900;">&#40;</span>err<span style="color: #339933;">,</span> window<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;The server could not be contacted.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, err404 is the equivalent of:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> err404<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#'</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'err_div_in_this_template'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;The server could not be contacted.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Obviously, this is a pretty trivial example, but it does a good job of showing a real-world use for currying.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Partial+application+and+currying&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F&amp;title=Partial+application+and+currying" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Partial+application+and+currying+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fpartial-application-and-currying%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/partial-application-and-currying/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

