<?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/"
	>

<channel>
	<title>Paolo Donadeo -- LifeLOG &#187; Objective Caml</title>
	<atom:link href="http://www.donadeo.net/category/programming-languages/objective-caml/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.donadeo.net</link>
	<description>All about my life, job and thoughts</description>
	<pubDate>Sun, 17 May 2009 21:51:56 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Information technology disgrace</title>
		<link>http://www.donadeo.net/2009/03/php-disgrace/</link>
		<comments>http://www.donadeo.net/2009/03/php-disgrace/#comments</comments>
		<pubDate>Tue, 03 Mar 2009 00:15:12 +0000</pubDate>
		<dc:creator>Paolo</dc:creator>
		
		<category><![CDATA[Article]]></category>

		<category><![CDATA[Computer programming]]></category>

		<category><![CDATA[Digital life]]></category>

		<category><![CDATA[English]]></category>

		<category><![CDATA[Objective Caml]]></category>

		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://www.donadeo.net/?p=168</guid>
		<description><![CDATA[The world of information technology is made by men and, like any other activity in which human beings are involved in, mistakes happens. Sometimes huge mistakes. And huge mistakes turn out into disgraces. One of these disgraces has a name: PHP.
Here is the story: some days ago wordpress.org released the latest version and I decided [...]]]></description>
			<content:encoded><![CDATA[<p>The world of information technology is made by men and, like any other activity in which human beings are involved in, mistakes happens. Sometimes huge mistakes. And huge mistakes turn out into disgraces. One of these disgraces has a name: PHP.</p>
<p>Here is the story: some days ago <a title="WordPress &#8250; Blog Tool and Publishing Platform" href="http://wordpress.org/" onclick="javascript:pageTracker._trackPageview('/outbound/article/wordpress.org');">wordpress.org</a> released the latest version and I decided to upgrade. No db changes, everything seems ok. But, wait a moment&#8230; the sidebar is broken! To be honest, the page itself seems to be broken. I did nothing strange. Ok, let&#8217;s rationalize this: <kbd style="font-size: 133%;">ssh</kbd> on the server, <kbd style="font-size: 133%;">cd</kbd> to the wordpress directory and issue:</p>
<pre>$ php index.php
Segmentation fault</pre>
<p>What the frack?!? I had no time to investigate more, and I decided to install the most recent working backup (I use <a title="Git - Fast Version Control System" href="http://git-scm.com/" onclick="javascript:pageTracker._trackPageview('/outbound/article/git-scm.com');">GIT</a> to track everything, including db backups) and forgot it for some days. Tonight I decided to solve the problem. I installed everything on my PC and, by hacking the DB, I was able to remove all the sidebar widgets, among which I suspected the guilty should be. Than, from Wordpress admin I added exactly the same widgets, in the very same order and with the same configuration.</p>
<p>Result?<em> No more segfault</em>.</p>
<p>Now, how can a so popular application be so fragile? Are Wordpress guys stupid, or what? No, this time the problem is with the technology. One word suffices: PHP.</p>
<p>Yes, I know, I perfectly know I shouldn&#8217;t focus myself on this or that technology, I now Facebook is made with PHP, I know everything, but&#8230; as an engineer I simply can&#8217;t ignore how poor this language is!</p>
<p>Hey, if I considered the language choice unimportant I should work in the marketing <img src='http://www.donadeo.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>End of this rant: I started my own blog, I&#8217;ll write it in a real programming languages, <a title="Objective Caml home page" href="http://caml.inria.fr/ocaml/index.en.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/caml.inria.fr');">Objective Caml</a>, I&#8217;ll  never be rich but at least I&#8217;ll never ever spend 3 hours of my life debugging a PHP buggy blog.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donadeo.net/2009/03/php-disgrace/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Draw something on the screen&#8230; and interact with it!</title>
		<link>http://www.donadeo.net/2008/09/draw-something-on-the-screen-and-interact-with-it/</link>
		<comments>http://www.donadeo.net/2008/09/draw-something-on-the-screen-and-interact-with-it/#comments</comments>
		<pubDate>Mon, 01 Sep 2008 11:30:01 +0000</pubDate>
		<dc:creator>Paolo</dc:creator>
		
		<category><![CDATA[Article]]></category>

		<category><![CDATA[Computer programming]]></category>

		<category><![CDATA[English]]></category>

		<category><![CDATA[Objective Caml]]></category>

		<category><![CDATA[Spare time]]></category>

		<guid isPermaLink="false">http://www.donadeo.net/?p=90</guid>
		<description><![CDATA[Summary of the previous episodes: 10 days ago Richard Jones complained about the difficulties to achieve simple tasks (drawing a function graph on the screen) on modern computers with modern programming languages; the day after Erik de Castro Lopo replied with a post in which he used GTK and Cairo (better: the OCaml bindings) to [...]]]></description>
			<content:encoded><![CDATA[<p>Summary of the previous episodes: 10 days ago <a title="Just draw something on the f-ing screen" href="http://camltastic.blogspot.com/2008/08/just-draw-something-on-f-ing-screen.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/camltastic.blogspot.com');">Richard Jones complained</a> about the difficulties to achieve simple tasks (drawing a function graph on the screen) on modern computers with modern programming languages; the day after <a title="Just Drawing Stuff on the Screen." href="http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/stuff_on_screen.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.mega-nerd.com');">Erik de Castro Lopo replied</a> with a post in which he used GTK and Cairo (better: the OCaml bindings) to achieve the result to draw a simple function on the screen. Yesterday Matias Giovannini added some pepper to this argument using SDL to <a href="http://alaska-kamtchatka.blogspot.com/2008/08/just-draw-something-on-screen.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/alaska-kamtchatka.blogspot.com');">draw the Newton fractal</a>.</p>
<p>So, what can be added to all this? With a perfect graphic toy you can draw on a window with simple commands, of course, but you also want to interact with the objects you drew. So I elaborated Erik example to add some keyboard and mouse interaction with the graphics on the screen.<span id="more-90"></span></p>
<h3>Downloading and compiling</h3>
<p>First of all, download the <a href="http://www.donadeo.net/wp-content/uploads/2008/09/cairo_toy.tgz" onclick="javascript:pageTracker._trackPageview('/downloads/wp-content/uploads/2008/09/cairo_toy.tgz');">source code</a> or, if you want the latest version, clone my GIT repository:</p>
<p class="note">$ git clone http://www.ex-nunc.org/projects/pdonadeo/cairo_toy.git cairo_toy.git</p>
<p>To compile the program you need:</p>
<ul>
<li><a title="Latest Objective Caml release" href="http://caml.inria.fr/ocaml/release.en.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/caml.inria.fr');">OCaml</a> (I have version 3.10.2, but probably 3.10.0 or 3.10.1 are ok);</li>
<li><a title="LablGTK" href="http://wwwfun.kurims.kyoto-u.ac.jp/soft/lsl/lablgtk.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/wwwfun.kurims.kyoto-u.ac.jp');">Lablgtk2</a>, the OCaml binding to GTK2;</li>
<li>the <a title="Objective Caml bindings" href="http://www.cairographics.org/cairo-ocaml/" onclick="javascript:pageTracker._trackPageview('/outbound/article/www.cairographics.org');">OCaml binding</a> to libcairo.</li>
</ul>
<p>All these packages are available in any recent Linux distribution; on Debian/Ubuntu:</p>
<p class="note">$ aptitude install ocaml liblablgtk2-ocaml-dev libcairo-ocaml-dev</p>
<p>To compile instruct this command inside the program directory:</p>
<p class="note">$ ocamlbuild demo_toy.native</p>
<h3>The code</h3>
<p>The program is very simple and is essentially derived from Erik&#8217;s code: the core is the functor <strong>Toy_maker.Make</strong> which accepts, as input, a module with the following signature <strong>INTERACTOR</strong>:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p90code2'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p902"><td class="code" id="p90code2"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">module</span> <span style="color: #06c; font-weight: bold;">type</span> INTERACTOR <span style="color: #a52a2a;">=</span>
<span style="color: #06c; font-weight: bold;">sig</span>
  <span style="color: #06c; font-weight: bold;">type</span> state
  <span style="color: #06c; font-weight: bold;">val</span> init_state <span style="color: #a52a2a;">:</span> state
  <span style="color: #06c; font-weight: bold;">val</span> win_title <span style="color: #a52a2a;">:</span> string
  <span style="color: #06c; font-weight: bold;">val</span> init_width <span style="color: #a52a2a;">:</span> int
  <span style="color: #06c; font-weight: bold;">val</span> init_height <span style="color: #a52a2a;">:</span> int
  <span style="color: #06c; font-weight: bold;">val</span> cmd_line_handler <span style="color: #a52a2a;">:</span> state <span style="color: #a52a2a;">-&gt;</span> string array <span style="color: #a52a2a;">-&gt;</span> state
  <span style="color: #06c; font-weight: bold;">val</span> keyboard_callback <span style="color: #a52a2a;">:</span> state <span style="color: #a52a2a;">-&gt;</span> GdkEvent<span style="color: #a52a2a;">.</span><span style="color: #060;">Key</span><span style="color: #a52a2a;">.</span><span style="color: #060;">t</span> <span style="color: #a52a2a;">-&gt;</span> state <span style="color: #a52a2a;">*</span> bool
  <span style="color: #06c; font-weight: bold;">val</span> pointer_buttons_callback <span style="color: #a52a2a;">:</span> state <span style="color: #a52a2a;">-&gt;</span> GdkEvent<span style="color: #a52a2a;">.</span><span style="color: #060;">Button</span><span style="color: #a52a2a;">.</span><span style="color: #060;">t</span> <span style="color: #a52a2a;">-&gt;</span> state <span style="color: #a52a2a;">*</span> bool
  <span style="color: #06c; font-weight: bold;">val</span> pointer_motion_callback <span style="color: #a52a2a;">:</span> state <span style="color: #a52a2a;">-&gt;</span> GdkEvent<span style="color: #a52a2a;">.</span><span style="color: #060;">Motion</span><span style="color: #a52a2a;">.</span><span style="color: #060;">t</span> <span style="color: #a52a2a;">-&gt;</span> state <span style="color: #a52a2a;">*</span> bool
  <span style="color: #06c; font-weight: bold;">val</span> pointer_scroll_callback <span style="color: #a52a2a;">:</span> state <span style="color: #a52a2a;">-&gt;</span> GdkEvent<span style="color: #a52a2a;">.</span><span style="color: #060;">Scroll</span><span style="color: #a52a2a;">.</span><span style="color: #060;">t</span> <span style="color: #a52a2a;">-&gt;</span> state <span style="color: #a52a2a;">*</span> bool
  <span style="color: #06c; font-weight: bold;">val</span> repaint <span style="color: #a52a2a;">:</span> state <span style="color: #a52a2a;">-&gt;</span> Cairo<span style="color: #a52a2a;">.</span><span style="color: #060;">t</span> <span style="color: #a52a2a;">-&gt;</span> int <span style="color: #a52a2a;">-&gt;</span> int <span style="color: #a52a2a;">-&gt;</span> state <span style="color: #a52a2a;">*</span> bool
<span style="color: #06c; font-weight: bold;">end</span></pre></td></tr></table></div>

<p>In this module the user must provide a <strong>type state</strong>, which contains the application state, some initialization values, a command line handler (in case you need) and 4 event handlers for the following events:</p>
<ul>
<li>keyboard;</li>
<li>mouse motion;</li>
<li>mouse buttons;</li>
<li>mouse wheel event (scroll events in GTK).</li>
</ul>
<p>The user also provides a <strong>repaint</strong> function, which takes care of repainting the Cairo context.</p>
<p>As a demo I wrote a simple <strong>My_interactor</strong> module implementing the following simple features:</p>
<ul>
<li>left click on the gray background creates a new circle;</li>
<li>left click inside an existing circle moves it around;</li>
<li>right click inside a circle deletes it;</li>
<li>the mouse wheel zooms (in and out);</li>
<li>middle click is used to pan;</li>
</ul>
<p>Here is the result.</p>
<p style="text-align: center;"><a href="http://www.donadeo.net/wp-content/uploads/2008/09/cairo_demo.png" ><img class="aligncenter size-medium wp-image-116" title="cairo_demo" src="http://www.donadeo.net/wp-content/uploads/2008/09/cairo_demo-300x231.png" alt="" width="300" height="231" /></a></p>
<p>Yes, it&#8217;s somewhat dull, but it does its job. Have fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.donadeo.net/2008/09/draw-something-on-the-screen-and-interact-with-it/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Sending emails via Gmail with Objective Caml</title>
		<link>http://www.donadeo.net/2008/04/sending-emails-via-gmail-with-objective-caml/</link>
		<comments>http://www.donadeo.net/2008/04/sending-emails-via-gmail-with-objective-caml/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 21:00:53 +0000</pubDate>
		<dc:creator>Paolo</dc:creator>
		
		<category><![CDATA[Article]]></category>

		<category><![CDATA[Computer programming]]></category>

		<category><![CDATA[English]]></category>

		<category><![CDATA[Objective Caml]]></category>

		<category><![CDATA[Python]]></category>

		<category><![CDATA[Spare time]]></category>

		<guid isPermaLink="false">http://www.donadeo.net/?p=78</guid>
		<description><![CDATA[
Motivation
Last week I was writing a Python script to make an automatic backup, and I decided to send me an email in case of scp failure. I decided to use Python to send the email, possibly via GMail and I found this interesting blog post: Sending emails via Gmail with Python. I like Python, it&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p style="float: left; margin-right: 1em; margin-bottom: 1em"><img title="Gmail logo with the caml" src="http://www.donadeo.net/wp-content/uploads/2008/04/gmail_and_ocaml.png" border="2" alt="Gmail logo with the caml" width="256" height="158" /></p>
<h3>Motivation</h3>
<p>Last week I was writing a Python script to make an automatic backup, and I decided to send me an email in case of scp failure. I decided to use Python to send the email, possibly via GMail and I found this interesting blog post: <a title="Kutuma's Ramblings: Sending emails via Gmail with Python" href="http://kutuma.blogspot.com/2007/08/sending-emails-via-gmail-with-python.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/kutuma.blogspot.com');">Sending emails via Gmail with Python</a>. I like Python, it&#8217;s a good programming language, but my heart (as a developer!) beats for the <a title="Objective Caml home page" href="http://caml.inria.fr/ocaml/index.en.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/caml.inria.fr');">Objective Caml</a> programming language.</p>
<p>So I decided to port the script presented in the post in OCaml. The result is this <a href="http://www.donadeo.net/wp-content/uploads/2008/04/sendmail.ml" >sendmail.ml</a>.</p>
<h3>Compiling the script</h3>
<p>To compile the script you need four software components:</p>
<ol>
<li>the Objective Caml environment. You can download it from the <a href="http://caml.inria.fr/ocaml/release.en.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/caml.inria.fr');">INRIA site</a>;</li>
<li><a href="http://projects.camlcity.org/projects/findlib.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/projects.camlcity.org');">Findlib</a>, to make compiling very simple;</li>
<li>Ocamlnet: here is the <a href="http://projects.camlcity.org/projects/ocamlnet.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/projects.camlcity.org');">home page of the project</a>;</li>
<li><a href="http://savonet.sourceforge.net/wiki/OCamlLibs" onclick="javascript:pageTracker._trackPageview('/outbound/article/savonet.sourceforge.net');">OCaml binding</a> to the SSL library.</li>
</ol>
<p>You can of course compile all this stuff, but every decent Linux distributions has all packaged. In Debian you have to run the following command:</p>
<pre># aptitude install ocaml libocamlnet-ocaml-dev \
  libssl-ocaml-dev ocaml-findlib</pre>
<p>Now, to compile the script, issue the command:</p>
<pre>$ ocamlfind ocamlopt -linkpkg -package \
  netstring,smtp,ssl,str sendmail.ml -o sendmail</pre>
<p>Before using it, remember to customize your name, email address, GMail user and password.</p>
<h3>Code comparison</h3>
<p>The first difference that jumps out at everyone confronting the two scripts is the number of lines: 41 lines for Python against 163 of my OCaml version. The difference is justified by the fact that the Python standard library comes with an almost full featured SMTP client, with ESMTP and TLS capability. On the other side Objective Caml has a very concise standard library, which includes essential modules and data structures, but no &#8220;batteries&#8221; are provided out of the box. This is a precise design decision by INRIA and, in some ways, I agree with them. Luckily the OCaml community is a source of excellent libraries and bindings, like <a href="http://projects.camlcity.org/projects/ocamlnet.html" onclick="javascript:pageTracker._trackPageview('/outbound/article/projects.camlcity.org');">Ocamlnet</a> by Gerd Stolpmann and the <a href="http://savonet.sourceforge.net/wiki/OCamlLibs" onclick="javascript:pageTracker._trackPageview('/outbound/article/savonet.sourceforge.net');">SSL library binding</a>, written by Samuel Mimram. The first one is in particular the Swiss Army Knife for network oriented battles.</p>
<p>Since the SMTP client provided by Ocamlnet doesn&#8217;t include TLS capability I decided to stole the source code and adapt it to my needs, to have a more comfortable and high level interface resembling the one offered by the Python standard library.</p>
<p>So the different length is easily explained: 109 lines of code are devoted to the smtp_client class, and the actual script is 54 lines long.</p>
<h3>The forward pipe operator</h3>
<p>All Turing complete computer languages are equivalent, but everyone knows this is only the theory and everyone have a programming language of choice. Here are two examples of what you can do in OCaml.</p>
<p>The first is the <em>pipe</em> operator:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code11'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7811"><td class="code" id="p78code11"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">|&gt;</span><span style="color: #6c6;">&#41;</span> x f <span style="color: #a52a2a;">=</span> f x</pre></td></tr></table></div>

<p>Here we define a (very common in <a href="http://en.wikipedia.org/wiki/Functional_programming" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');">FP</a>) infix operator which simply inverts the order of its operands. What the <a href="http://en.wiktionary.org/wiki/frack" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wiktionary.org');">frack</a> is this? Very simple, we use it to invert the order of a function with its last parameter so, if we want to compute the 3rd Fibonacci number we can write:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code12'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7812"><td class="code" id="p78code12"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> fib3 <span style="color: #a52a2a;">=</span> fibonacci <span style="color: #c6c;">3</span></pre></td></tr></table></div>

<p>but also:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code13'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7813"><td class="code" id="p78code13"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> fib3 <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">3</span> <span style="color: #a52a2a;">|&gt;</span> fibonacci</pre></td></tr></table></div>

<p>This is not a style issue, we can define a simple infix operator that feeds a function with a value; we can of course connect several functions together, like in a shell script with the Unix pipe operator, transforming an ugly and difficult to be read call:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code14'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7814"><td class="code" id="p78code14"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> result <span style="color: #a52a2a;">=</span> func1<span style="color: #6c6;">&#40;</span>func2 <span style="color: #6c6;">&#40;</span>func3<span style="color: #6c6;">&#40;</span>x<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span></pre></td></tr></table></div>

<p>into:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code15'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7815"><td class="code" id="p78code15"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> result <span style="color: #a52a2a;">=</span> x <span style="color: #a52a2a;">|&gt;</span> func3 <span style="color: #a52a2a;">|&gt;</span> func2 <span style="color: #a52a2a;">|&gt;</span> func1</pre></td></tr></table></div>

<p>In the sendmail.ml script, line 127, we read:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code16'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7816"><td class="code" id="p78code16"><pre class="ocaml" style="font-family:monospace;">email_string <span style="color: #a52a2a;">|&gt;</span>
  <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html"><span style="color: #06c; font-weight: bold;">Str</span></a><span style="color: #a52a2a;">.</span><span style="color: #060;">global_replace</span> new_line_regexp <span style="color: #3cb371;">&quot;\r\n&quot;</span> <span style="color: #a52a2a;">|&gt;</span>
    <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html"><span style="color: #06c; font-weight: bold;">Str</span></a><span style="color: #a52a2a;">.</span><span style="color: #060;">split</span> crlf_regexp <span style="color: #a52a2a;">|&gt;</span>
      <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/List.html"><span style="color: #06c; font-weight: bold;">List</span></a><span style="color: #a52a2a;">.</span><span style="color: #060;">iter</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> s <span style="color: #a52a2a;">-&gt;</span>
        self<span style="color: #a52a2a;">#</span>output_string <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">if</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/String.html"><span style="color: #06c; font-weight: bold;">String</span></a><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span> s <span style="color: #a52a2a;">&gt;</span> <span style="color: #c6c;">0</span> <span style="color: #a52a2a;">&amp;&amp;</span> s<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span><span style="color: #c6c;">0</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">=</span> <span style="color: #a52a2a;">'.'</span> <span style="color: #06c; font-weight: bold;">then</span>
                              <span style="color: #6c6;">&#40;</span><span style="color: #3cb371;">&quot;.&quot;</span> <span style="color: #a52a2a;">^</span> s <span style="color: #a52a2a;">^</span> <span style="color: #3cb371;">&quot;\r\n&quot;</span><span style="color: #6c6;">&#41;</span>
                            <span style="color: #06c; font-weight: bold;">else</span> s<span style="color: #a52a2a;">^</span><span style="color: #3cb371;">&quot;\r\n&quot;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">;</span></pre></td></tr></table></div>

<p>Here we take the string containing the email, we replace all new lines with the sequence &#8220;\r\n&#8221;, split the stream into lines and in the end send each line to the SMTP server, taking care of quoting each line starting with a period. In 6 lines of code.</p>
<h3>Algebraic data type</h3>
<p><a href="http://en.wikipedia.org/wiki/Algebraic_data_type" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');">Algebraic data type</a> are a very interesting aspect of functional programming. We can <em>easily</em> wrap two heterogeneous data types into a single one with two line of code:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code17'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7817"><td class="code" id="p78code17"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> socket <span style="color: #a52a2a;">=</span>
  <span style="color: #a52a2a;">|</span> Unix_socket <span style="color: #06c; font-weight: bold;">of</span> Unix<span style="color: #a52a2a;">.</span><span style="color: #060;">file_descr</span>
  <span style="color: #a52a2a;">|</span> SSL_socket <span style="color: #06c; font-weight: bold;">of</span> Ssl<span style="color: #a52a2a;">.</span><span style="color: #060;">socket</span></pre></td></tr></table></div>

<p>The smtp_client class contains a reference to the connection handle used for communicating with the server which is a plain file descriptor or an SSL socket, which one depends on the state of the communication. I <strong>do not want</strong> to create a virtual class or an interface and two implementing class as I should do in horrible languages like Java, spending half an hour deciding which methods to put in the public interface, and so on; after all, it&#8217;s only a file descriptor!</p>
<p>Now I have a new type which is a <a href="http://en.wikipedia.org/wiki/Disjoint_union" onclick="javascript:pageTracker._trackPageview('/outbound/article/en.wikipedia.org');">disjoint union</a> of the two original types and I can write code like this (line 54):</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p78code18'); return false;">View Code</a> OCAML</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table width="100%" ><tr id="p7818"><td class="code" id="p78code18"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">let</span> <a href="http://caml.inria.fr/pub/docs/manual-ocaml/libref/Pervasives.html#VALinput"><span style="color: #06c; font-weight: bold;">input</span></a> <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">match</span> channel <span style="color: #06c; font-weight: bold;">with</span>
  <span style="color: #a52a2a;">|</span> Unix_socket s <span style="color: #a52a2a;">-&gt;</span> Unix<span style="color: #a52a2a;">.</span><span style="color: #060;">read</span> s
  <span style="color: #a52a2a;">|</span> SSL_socket s <span style="color: #a52a2a;">-&gt;</span> Ssl<span style="color: #a52a2a;">.</span><span style="color: #060;">read</span> s <span style="color: #06c; font-weight: bold;">in</span></pre></td></tr></table></div>

<p>Here we say: if channel is actually a Unix file descriptor, let&#8217;s define a new function &#8220;input&#8221; which is the standard function &#8220;read&#8221;, from Unix module, otherwise, if channel is an SSL socket, let&#8217;s define &#8220;input&#8221; as the Ssl.read function, which works only in ciphered sockets. From now on I&#8217;ll use input instead of one of the two original functions.</p>
<p>Ok, it&#8217;s time to stop the waffle. Enjoy the script if you need, it&#8217;s completely free, like in free beer, in free speech and even in free sex! <img src='http://www.donadeo.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.donadeo.net/2008/04/sending-emails-via-gmail-with-objective-caml/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
