<?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>Juozas devBlog &#187; anonymous function</title>
	<atom:link href="http://dev.juokaz.com/tag/anonymous-function/feed" rel="self" type="application/rss+xml" />
	<link>http://dev.juokaz.com</link>
	<description>Random ideas, scripts and facts</description>
	<lastBuildDate>Mon, 22 Mar 2010 10:48:42 +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>Lambda functions are coming to PHP</title>
		<link>http://dev.juokaz.com/php/lambda-function-coming-to-php</link>
		<comments>http://dev.juokaz.com/php/lambda-function-coming-to-php#comments</comments>
		<pubDate>Thu, 26 Mar 2009 19:56:34 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[anonymous function]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[functions]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[php 5.3]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=434</guid>
		<description><![CDATA[Only some days ago PHP.net introduced 5.3.0RC1 version, but future features have been known for quite a while. Namespaces and lambda functions (+ closures) are most anticipated, because they&#8217;ll increase flexibility and good-looks of code a lot. Today I&#8217;m going to try to prove why lambda functions are so useful.
Because of &#8220;Functional Programming&#8221; course I [...]]]></description>
			<content:encoded><![CDATA[<p>Only some days ago PHP.net introduced <a href="http://www.php.net/archive/2009.php#id2009-03-24-1">5.3.0RC1</a> version, but future features have been known for quite a while. Namespaces and <a href="http://en.wikipedia.org/wiki/Lambda_function">lambda functions</a> (+ closures) are most anticipated, because they&#8217;ll increase flexibility and good-looks of code a lot. Today I&#8217;m going to try to prove why lambda functions are so useful.</p>
<p>Because of &#8220;Functional Programming&#8221; course I had in <a href="http://www.ed.ac.uk">university</a>, I love lambda functions. At first they seemed a little bit strange, but hey &#8211; in <a href="http://www.haskell.org/">Haskell</a> you write:</p>

<div class="wp_syntax"><div class="code"><pre class="haskell" style="font-family:monospace;">Input: <span style="font-weight: bold;">map</span> <span style="color: green;">&#40;</span>\x <span style="color: #339933; font-weight: bold;">=&gt;</span> x<span style="color: #339933; font-weight: bold;">*</span><span style="color: red;">3</span><span style="color: green;">&#41;</span> <span style="color: green;">&#91;</span><span style="color: red;">1</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">2</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">4</span><span style="color: green;">&#93;</span>
Output: <span style="color: green;">&#91;</span><span style="color: red;">3</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">6</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">9</span><span style="color: #339933; font-weight: bold;">,</span><span style="color: red;">12</span><span style="color: green;">&#93;</span></pre></div></div>

<p>No need to create for loops (which doesn&#8217;t exists in Haskell at all) and code is much more flexible. Functional languages allows creating functions which accepts functions as arguments, not only integers or floats. That&#8217;s why most functions are very short, but are very easily connectable together. </p>
<p>I think one the best examples of lambda functions usefulness is sorting. Image this scenario: <em>$books</em> <a href="http://docs.php.net/manual/en/class.arrayobject.php">collection</a> holds books information and you need to sort it by title; author; publish year and author; publish city and title. How you are going to do it? It&#8217;s easy and you probably would write something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Sort by title</span>
<span style="color: #000088;">$books</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sortByTitle</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ASC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Sort by publisher year, author</span>
<span style="color: #000088;">$books</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sortByPublishYear</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'DESC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sortByAuthor</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'ASC'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>However, it looks good and does work just fine, but it&#8217;s not very practical &#8211; you need to create functions for all keywords (title, author, publish year, etc.). You may argue, that __call() can be used and that&#8217;s absolutely true, but it&#8217;s even harder to maintain and code it, still usable though. </p>
<p>Nevertheless, 5.3 version of <a href="http://dev.juokaz.com/category/php">PHP</a> introduces lambda functions (also called <a href="http://docs.php.net/functions.anonymous">anonymous functions</a>), which can simplify your work a lot (I haven&#8217;t tried running this code):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Sort by book title</span>
<span style="color: #000088;">$books</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sort</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$book1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$book2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$book1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$book2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</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>
&nbsp;
<span style="color: #666666; font-style: italic;">// Sort by publisher year, author</span>
<span style="color: #000088;">$books</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sort</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$book1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$book2</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$book1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish_year</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$book2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish_year</span><span style="color: #009900;">&#41;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$book1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">author</span><span style="color: #339933;">,</span> <span style="color: #000088;">$book2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">author</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">strcmp</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$book2</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish_year</span><span style="color: #339933;">,</span> <span style="color: #000088;">$book1</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">publish_year</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>It&#8217;s a little bit harder to read and maybe understand, but basically you can pass whatever comparison function you&#8217;d like. Sort function can use simple for-loop-inside-for-loop or <a href="http://uk.php.net/manual/en/function.usort.php">usort</a> and compare all elements with your function</p>
<p>Lambda functions are not replacement for __cal() and should be used cleverly, search and filtering with lambda functions is only one example of how useful they are. In short, lambda functions allows you to create small dynamic functions which can be used to expand and modify normal functions behaviour. </p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/lambda-function-coming-to-php/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
