<?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>pastbedti.me &#187; Currying</title>
	<atom:link href="http://www.pastbedti.me/tag/currying/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pastbedti.me</link>
	<description>About ruby, rails, postgresql and stuff....</description>
	<lastBuildDate>Fri, 02 Apr 2010 20:12:14 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Currying in JavaScript</title>
		<link>http://www.pastbedti.me/2009/11/currying-in-javascript/</link>
		<comments>http://www.pastbedti.me/2009/11/currying-in-javascript/#comments</comments>
		<pubDate>Sun, 08 Nov 2009 21:53:16 +0000</pubDate>
		<dc:creator>Patrik Hedman</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Currying]]></category>
		<category><![CDATA[Functional Programming]]></category>

		<guid isPermaLink="false">http://www.pastbedti.me/?p=587</guid>
		<description><![CDATA[Whilst being an object-oriented language, JavaScript still takes a lot of ideas from functional languages. One which it dosen&#8217;t implement though, is the idea of currying.
What&#8217;s currying then? You might ask, well, here&#8217;s the definition according to wikipedia:
In mathematics and computer science, currying, invented by Moses Schönfinkel and later re-invented by Haskell Curry,[1] is the [...]]]></description>
			<content:encoded><![CDATA[<p>Whilst being an object-oriented language, JavaScript still takes a lot of ideas from functional languages. One which it dosen&#8217;t implement though, is the idea of currying.<br />
What&#8217;s currying then? You might ask, well, here&#8217;s the definition according to wikipedia:</p>
<blockquote><p>In mathematics and computer science, currying, invented by Moses Schönfinkel and later re-invented by Haskell Curry,[1] is the technique of transforming a function that takes multiple arguments (or more accurately an n-tuple as its argument) in such a way that it can be called as a chain of functions each with a single argument.</p></blockquote>
<p>So basically, it let&#8217;s you fill in some arguments to a function before hand and return a new function which you will subsequently call instead, or default arguments on steroids if you will.<br />
Here&#8217;s a few examples of what we could do if JavaScript was capable of currying:</p>
<p>[cc lang="JavaScript"]<br />
// Array to csc<br />
Array.prototype.arrayToCsv = Array.prototype.join.curry(&#8220;; &#8220;);</p>
<p>["Patrik", "Hedman", "Stockholm"].arrayToCsv(); //=> &#8220;Patrik; Hedman; Stockholm&#8221;</p>
<p>// And back<br />
String.prototype.csvToArray = String.prototype.split.curry(/;\s*/);</p>
<p>(&#8220;Patrik; Hedman; Stockholm&#8221;).csvToArray(); //=> ["Patrik", "Hedman", "Stockholm"]</p>
<p>// The undefined parameter will be replaced with the<br />
// callback function passed to clicker();<br />
var clicker = document.body.addEventListener.curry(&#8220;click&#8221;, undefined, false);<br />
clicker(function(){<br />
    console.log( &#8220;Curryfied click event.&#8221; );<br />
});<br />
[/cc]</p>
<p>Pretty sweet, but it wont work since there is no curry() function in JavaScript, not a problem, implementing one is pretty straightforward:</p>
<p>[cc lang="JavaScript"]<br />
Function.prototype.curry = function() {<br />
	// Save references to this and the arguments so we can<br />
	// acces them from the closure<br />
	var fn   = this,<br />
	    args = Array.prototype.slice.call( arguments );</p>
<p>	return function() {<br />
		var arg = 0;<br />
		for ( var i = 0; i < args.length &#038;&#038; arg < arguments.length; i++ )<br />
			if ( args[i] === undefined )<br />
				// If the original argument is undefined<br />
				// then replace it with the new one<br />
				args[i] = arguments[arg++];</p>
<p>		// Call the original function with the current this as<br />
		// the context and the newly constructed argument list<br />
		return fn.apply(this, args);<br />
	};<br />
};<br />
[/cc]</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pastbedti.me/2009/11/currying-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
