<?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; Haskell</title>
	<atom:link href="http://dev.juokaz.com/tag/haskell/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>
		<item>
		<title>Parallel processes in PHP</title>
		<link>http://dev.juokaz.com/php/parallel-processes-in-php</link>
		<comments>http://dev.juokaz.com/php/parallel-processes-in-php#comments</comments>
		<pubDate>Thu, 12 Feb 2009 23:36:18 +0000</pubDate>
		<dc:creator>Juozas</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[computing]]></category>
		<category><![CDATA[cron]]></category>
		<category><![CDATA[graphics]]></category>
		<category><![CDATA[Haskell]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[parallel]]></category>
		<category><![CDATA[rayracer]]></category>
		<category><![CDATA[university]]></category>

		<guid isPermaLink="false">http://dev.juokaz.com/?p=108</guid>
		<description><![CDATA[When I was coding Ray-Tracer project for my Computer Science studies in university, I ran into using Haskell parallel map function (map calls function for all list elements). Ray-Tracer runs reflections, shadows, ray-casts, etc. detection for every single pixel in scene and since everything is mathematical calculations, it&#8217;s paralleling is almost trivial.
Parallel map functions does [...]]]></description>
			<content:encoded><![CDATA[<p>When I was coding Ray-Tracer project for my Computer Science studies in university, I ran into using <a href="http://www.haskell.org/ghc/docs/latest/html/libraries/parallel/Control-Parallel-Strategies.html">Haskell parallel map function</a> (map calls function for all list elements). <a href="http://en.wikipedia.org/wiki/Ray_tracing_(graphics)">Ray-Tracer</a> runs reflections, shadows, ray-casts, etc. detection for every single pixel in scene and since everything is mathematical calculations, it&#8217;s paralleling is almost trivial.</p>
<p>Parallel map functions does all work for you &#8211; you don&#8217;t even need to know when it&#8217;s right time to fork another thread. Implementing parallel computation in Haskell was very easy and it almost gave theoretical decrease in processing time by N times (where N is processor cores count). Today I will talk a little bit about possible parallelism with PHP&#8217;s internal <a href="http://uk2.php.net/manual/en/ref.posix.php">POSIX</a> functions.</p>
<p>PHP is neither a functional language, nor is made with threading support (correct me if I&#8217;m wrong). Still, it&#8217;s possible to create parallel processes by using <a href="http://uk2.php.net/manual/en/ref.pcntl.php">pcntl_*</a> functions family. Some days ago I tried just to get something working, what uses parallel processes and can possible be extended to deal with time-consuming mathematical algorithms.</p>
<p>Download my sample code <a href="http://dev.juokaz.com/examples/parallel/run.phps" target="_blank">here</a> (rename extension to <em>php</em>) and run it in terminal (it probably wont run with Apache). This code will try to compute x^3 for 0-1M integers two times, parallel runs both in one call, normal behaviour runs one after another. Output should look similar to this:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">juokaz<span style="color: #000000; font-weight: bold;">@</span>thinkpad:~<span style="color: #000000; font-weight: bold;">/</span>Desktop<span style="color: #000000; font-weight: bold;">/</span>php<span style="color: #000000; font-weight: bold;">/</span>parallel$ php run.php
We start
I am the child, pid = <span style="color: #000000;">0</span>
I am the parent, pid = <span style="color: #000000;">2004</span>
yep, finished, I have <span style="color: #000000;">2004</span> ID
yep, finished, I have <span style="color: #000000;">0</span> ID
Ran parallel <span style="color: #000000;">5.81009602547</span> seconds
yep, finished, I have <span style="color: #000000;">1</span> ID
yep, finished, I have <span style="color: #000000;">2</span> ID
Ran normal <span style="color: #000000;">9.76671719551</span> seconds</pre></div></div>

<p>As you can see, parallel computation on dual core processor ran almost 2x faster. And yes, it does use both cores simultaneously.</p>
<p>If you want to look more deeply, there is great article called <a href="http://www.van-steenbeek.net/?q=php_pcntl_fork">Thorough look at PHP&#8217;s pcntl_fork()</a>. You will quite quickly find that there are problems when using pcntl_fork, because it basically clones running process and then continues. Everything you define before forking child process will be accessible inside child &#8211; it&#8217;s not always what you really want.<a href="http://www.van-steenbeek.net/?q=php_pcntl_fork"><br />
</a></p>
<p>Parallel processes can be easily simulated by using asynchronous calls (over HTTP, for example) from one script to others, but for something more calculations-based, using pcntl_fork() can be much more practical. But I will probably still chose asyncronous calls over parallelizing because they are much more flexible.</p>
]]></content:encoded>
			<wfw:commentRss>http://dev.juokaz.com/php/parallel-processes-in-php/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
