<?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>Nicollet.Net</title>
	<atom:link href="http://www.nicollet.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.nicollet.net</link>
	<description>Everyone Loves Me</description>
	<lastBuildDate>Mon, 23 Jan 2012 16:55:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>OCaml Submodule Pattern</title>
		<link>http://www.nicollet.net/2012/01/ocaml-submodule-pattern/</link>
		<comments>http://www.nicollet.net/2012/01/ocaml-submodule-pattern/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 16:55:59 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Functional]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Objective Caml]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2660</guid>
		<description><![CDATA[My code is quite large for an OCaml project. The main RunOrg repository alone contains 46212 lines of OCaml code (plus an additional 5631 lines of OCaml mli files) — and then, there&#8217;s the web framework code and the independent plugins code. It&#8217;s is Better™ to have many short files than a few long ones. [...]]]></description>
			<content:encoded><![CDATA[<p>My code is quite large for an OCaml project. The main RunOrg repository alone contains 46212 lines of OCaml code (plus an additional 5631 lines of OCaml mli files) — and then, there&#8217;s the web framework code and the independent plugins code.</p>
<p>It&#8217;s is Better™ to have many short files than a few long ones. One reason is incremental compiling with <em>ocamlbuild</em> : that the smaller your files are, the smaller the percentage of code to be compiled when you make a small change. Another reason is that files provide a natural delineation of code that makes it slightly easier to reason about.</p>
<p>The very process of splitting a large file into smaller files is also an excellent way to clean up the code. Every split is an opportunity to move some code to a more generic location — why have a <code>CMember_importParser</code> module when all of its functionality could fit into an <code>OzCsv</code> plugin module ? Even when no such generic solution exists, cutting through the jungle that a 2000-line module contains helps clean up dependencies, identify shared functionality and imagine better ways to design code.</p>
<p>Still, when cutting up code this way, the problem of encapsulation remains. If code that relates to pictures (an upload module, a transform module, a download module, an access rights module) is split across several files, it is desirable to let each file access functions and values from other values that would not otherwise be shown to modules not related to picture processing. For instance, a <code>get_download_link</code> function should be available throughout all picture-related modules, but the rest of the application should use the <code>get_download_link_for_user</code> function that checks whether the user is allowed to download the file.</p>
<p>In order to achieve several nested levels of encapsulation required to work with modules this way, I have come up with a convention :</p>
<ul>
<li>A module name (and thus, a file name) is composed of segments written in camelCase and separated by underscores. For instance, <code>CEntity_view_grid</code> is a module name containing segments <code>CEntity,</code> <code>view</code> and <code>grid</code>.</li>
<li>Modules with only one segment are public. Any other module may include, open or otherwise reference them with no limitations beyond what the module signature says. So, <code>CEntity</code> may access <code>MGroup</code> freely.</li>
<li>Modules with N &gt; 1 segments are private. They may only be accessed by modules which share the first N-1 segments. So, <code>CEntity_view</code> is available to modules <code>CEntity</code> and <code>CEntity_edit</code> but not <code>CPicture</code>.</li>
<li>A module with N segments may export any module with N+1 segments it can access, possibly under a more restrictive signature. For instance, <code>CEntity_view</code> is available to all other modules as <code>CEntity.View</code>.</li>
</ul>
<p>To make these rules easier to respect, private module dependencies are made explicit by adding a list of module aliases at the top of each file. The top of my <code>cEntity_view.ml</code> file starts with :</p>
<pre style="padding-left: 30px;"><code>module Sidebar     = CEntity_sidebar
module Unavailable = CEntity_unavailable
module Edit        = CEntity_edit
module Info        = CEntity_view_info
module Directory   = CEntity_view_directory
module Grid        = CEntity_view_grid
module Wall        = CEntity_view_wall
</code></pre>
<p>It is forbidden to use a private module without going through such an alias, and it is forbidden to define such an alias anywhere except at the top of the file. This makes it extremely easy to determine whether private access rules are respected.</p>
<p>The rule of thumb for splitting files (in my particular coding style) is :</p>
<ul>
<li>Code for separate layers (model, view, controller&#8230;) go into separate public modules.</li>
<li>For complex code (such as complex rules in model or controller code), consider splitting files larger than 200 lines.</li>
<li>For simple code (such as HTML template or JSON serialization definitions), there is no splitting limit except for factoring out common behavior.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2012/01/ocaml-submodule-pattern/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>freedom.txt</title>
		<link>http://www.nicollet.net/2012/01/freedom-txt/</link>
		<comments>http://www.nicollet.net/2012/01/freedom-txt/#comments</comments>
		<pubDate>Mon, 23 Jan 2012 13:21:26 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Freedom]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PIPA]]></category>
		<category><![CDATA[SOPA]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2658</guid>
		<description><![CDATA[Francis suggested the freedom.txt idea in early January. It&#8217;s catching on. I think this is a good idea, although I do not agree with the wording of the message, so here&#8217;s mine. You might not understand what I am rambling about in the two sentences above. What is going on? Until recently, humans were fairly [...]]]></description>
			<content:encoded><![CDATA[<p>Francis suggested the <a href="http://fr.anc.is/2012/01/01/freedom.txt/" target="_blank">freedom.txt</a> idea in early January. <a href="https://github.com/mvanveen/freedom.txt" target="_blank">It&#8217;s catching on</a>. I think this is a good idea, although I do not agree with the wording of the message, so <a href="http://www.nicollet.net/freedom.txt" target="_blank">here&#8217;s mine</a>.</p>
<p>You might not understand what I am rambling about in the two sentences above. <em>What is going on?</em></p>
<p><strong></strong>Until recently, humans were fairly similar to each other in terms of capabilities. If an individual decided to annoy, harass or harm others, their impact would be limited to what they could do on their own before being stopped, or they would have to convince enough people to help them. When you were a large organized group of people, you only had to care about other large organized groups of people.</p>
<p>Even with the advent of modern technology up to the 1980s, when two people in a stealth bomber could mash tens of thousands to a pulp by pressing a button, this was still the consequence of an industrial infrastructure allowed by a large organized group that willingly granted those two people such power.</p>
<p>Guns are a special case. An individual with a gun can do more damage than average, faster and without retaliation. What happened next is hardly surprising: some people decided to fight fire with fire and buy their own guns as a deterrent, while others decided that gun ownership — or rather, the increase in destructive power provided by guns — should be heavily regulated.</p>
<p>And then, <strong>computing and the internet</strong> happened. Owning an internet-connected general-purpose device is quite affordable these days.</p>
<p>And owning an internet-connected computer increases the capabilities of individuals by several orders of magnitude, as far as processing data and communicating with others is concerned. While computers are not as lethal as guns (tech support calls excluded), the increase in lethality provided by guns is much smaller than the increase in processing power and communication reach provided by computers. This is literally the first time that we, as a species, have to deal with some individuals having enough power to harm, disrupt or topple large organized groups.</p>
<p>A handful of people can harness the power of computing to <a href="http://en.wikipedia.org/wiki/Botnet#Types_of_attacks" target="_blank">send billions of spam messages</a>, <a href="http://en.wikipedia.org/wiki/Denial-of-service_attack" target="_blank">bring down entire web sites</a> or <a href="http://en.wikipedia.org/wiki/Melissa_%28computer_virus%29" target="_blank">networks</a> or <a href="http://www.theaustralian.com.au/news/world/computer-virus-used-to-sabotage-irans-nuclear-plans-built-by-us-and-israel/story-e6frg6so-1225989304785" target="_blank">nuclear plants</a>, <a href="http://wikileaks.org/" target="_blank">make elite KGB spies green with envy</a>, <a href="https://www.facebook.com/" target="_blank">collect personal information about thousands of people</a>, <a href="http://www.huffingtonpost.com/2012/01/18/sopa-blackout-internet-censorship_n_1211905.html" target="_blank">tell millions of people about a new law that mainstream media are silent about</a>, or <a href="http://en.wikipedia.org/wiki/Amanuensis" target="_blank">illegally distribute content without compensating its creators</a>.</p>
<p>Is it any surprise that corporations, organizations and countries are fighting to regulate such incredible power?</p>
<p>You may not run software on iWhatever devices without consent from Apple. In France, you are legally responsible for when third parties commit crimes using your IP address. Various laws around the world including SOPA, PIPA and ACTA aim to provide counter-measures to illegal distribution of content. Germany and the UK are getting anal-retentive about what cookies you are allowed to send to your users.</p>
<p>And yet, there are immense benefits still to be reaped from a free, open, uncensored internet, and I am certain they far outweigh most of the costs involved. To willingly throw away those benefits in order to maintain existing business models and political habits strikes me as a very bad idea indeed, and one we should fight against.</p>
<p>Another alarming development is that the general public, lawmakers included, are woefully incompetent when it comes to computers and the internet. They do not understand why it works, and so they do not understand why actions they take might prevent it from working or what negative consequences. Laws are drafted and voted on without asking any experts for input, even when experts are quite outspoken against them.</p>
<p>And we, the experts, are the cause. We are dealing with people who thrive on communicating with the public — we are fairly good when it comes to communication, but only with each other. Is it any wonder that no one listens to us? We are so familiar with the details, nooks and crannies of our high-tech world that we fail to explain, in simple terms, why non-experts should care about these issues.</p>
<p>Why did it take something as critical as SOPA and PIPA to get us moving? Shouldn&#8217;t we be the ones <em>leading</em> the conversation on computers and the internet, instead of mumbling in our collective niche beards when clueless members of our parliaments speak of «series of tubes»? Could it be that we are so used to wielding awesome individual powers of communication, that we have forgotten how to team up to make our voices heard?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2012/01/freedom-txt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Frameworks, Libraries, Conventions</title>
		<link>http://www.nicollet.net/2012/01/frameworks-libraries-conventions/</link>
		<comments>http://www.nicollet.net/2012/01/frameworks-libraries-conventions/#comments</comments>
		<pubDate>Thu, 05 Jan 2012 19:12:25 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Dynamic]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Zend]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2649</guid>
		<description><![CDATA[Funkatron came up with the MicroPHP Manifesto : I am a PHP developer I am not a Zend Framework or Symfony or CakePHP developer I think PHP is complicated enough I like building small things I like building small things with simple purposes I like to make things that solve problems I like building small [...]]]></description>
			<content:encoded><![CDATA[<p>Funkatron came up with the <a href="http://funkatron.com/posts/the-microphp-manifesto.html" target="_blank">MicroPHP Manifesto</a> :</p>
<blockquote><p><strong>I am a PHP developer</strong></p>
<ul>
<li>I am not a Zend Framework or Symfony or CakePHP developer</li>
<li>I think PHP is complicated enough</li>
</ul>
<p><strong>I like building small things</strong></p>
<ul>
<li>I like building small things with simple purposes</li>
<li>I like to make things that solve problems</li>
<li>I like building small things that work together to solve larger problems</li>
</ul>
<p><strong>I want less code, not more</strong></p>
<ul>
<li>I want to write less code, not more</li>
<li>I want to manage less code, not more</li>
<li>I want to support less code, not more</li>
<li>I need to justify every piece of code I add to a project</li>
</ul>
<p><strong>I like simple, readable code</strong></p>
<ul>
<li>I want to write code that is easily understood</li>
<li>I want code that is easily verifiable</li>
</ul>
</blockquote>
<p>Without surprise, a large swath of the community did not take it well, for similar reasons to <a href="http://www.nicollet.net/2010/03/why-i-gave-up-on-the-zend-framework/" target="_blank">my earlier piece against Zend Framework</a> — deviation from the commonly accepted norm.</p>
<p>I have come a long way since I wrote that article, and I must have been walking in circles, because I actually ended up where I originally begun : why do we call these things <em>frameworks</em> ?</p>
<p>Zend, Symfony, CakePHP — as well as Node.js, Rails, Django, Ocsigen &#8230; — actually contribute three different things to projects that use them.</p>
<h4>Libraries</h4>
<p>A library provides <em>functionality</em> used for solving <em>general problems</em> in a flexible, <em>standalone</em> manner. <code>Zend_Mail</code> is a classic example of the library aspect of Zend Framework: you can plug it into your application and start sending e-mail. The interface you would use is uncluttered by details that are not directly related to sending e-mail.</p>
<p>The core qualities of a library are its power (how many different aspects of a problem does it let me solve — attachments, rich text, bouncing, MIME handling&#8230;) and the clarity of its interface. <strong>What problems can you solve, and how fast can you solve them?</strong></p>
<h4>Conventions</h4>
<p>When you hear «conventions» you immediately think of opening brace positions and variable naming rules. It&#8217;s about more than that.</p>
<p>The Model-View-Controller separation is an example of convention: it has been decided that under no circumstances should HTML rendering occur in Model code, no HTTP or session handling should happen in View code, and no SQL queries happen in Controller code.</p>
<p>Good conventions are designed to let the developers assume interesting properties about the code without having to actually read it. A convention like «no global variables» means I never have to care about global state in my code, ever. A convention like «view code must respect the law of Demeter» means all the data used by the view is right where it is being initialized.</p>
<p>They are also designed to make reuse and interoperability easier by reducing the number of ways in which a possible interface can be implemented. A convention could say the values are passed by assigning them to members post-construction and <strong>not</strong> as constructor arguments, so you have one less point of contention between the object that is initialized and the object that does the initialization.</p>
<p>Last but not least, conventions are usually based on experience of things that could go wrong if certain behavior is allowed. A typical example is the requirement to escape all strings as they are being output — eliminating any ambiguities as to whether the string has already been escaped elsewhere and should be output as-is: it has not.</p>
<p>Zend comes with a variety of useful conventions enforced both through the interface of its tools — <em>this</em> is how you use a view, <em>this</em> is how you define a view helper that should be available from within any view, <em>this</em> is how you bind a piece of code to an URL, and so on. I happen to disagree with many of those conventions myself — because I believe they solve the wrong problems — but they are certainly better than a project with no conventions.</p>
<p>For the reference, my PHP conventions are described in <a href="http://www.nicollet.net/ohm-least-resistance/" target="_blank">the user manual for Ohm</a>.</p>
<h4>Framework</h4>
<p>A framework is actually going a step further than mere conventions. They are super-conventions designed to be respected by plugin authors. The point is that if plugin A and plugin B respect the set of conventions provided by the framework, then they can be used together in the same application.</p>
<p>Consider a practical example : a plugin that implements a CAPTCHA field in a form, and a plugin that displays and submits a form through AJAX. On a bad day, it goes like this :</p>
<ol>
<li>When an error occurs, the server-side AJAX-form plugin sends out a small piece of JSON containing the fields that have errors, along with the error messages. A small client-side script applies these.</li>
<li>However, the CAPTCHA plugin expected the image to be reloaded when an error occurs.  It may either keep the same image and target word — defeating the purpose of a CAPTCHA — or change the target word without knowing that the image could not be changed.</li>
<li>You then need to post on StackOverflow hoping for a solution, search online for a patch to either plugin that could make it work as expected, or try to read the code to either in order to create the patch yourself.</li>
</ol>
<p>Had the framework provided a clean notion of « this field must be refreshed on every attempt » as part of their form interface, both plugins would have used it — the CAPTCHA plugin would have marked its field as such, and the AJAX plugin would have implemented a special case for such fields.</p>
<p>As such, the purpose of a framework is to provide a clean, unambigous and extensive <strong>vocabulary</strong> that all the plugins should be able to speak, and that is designed to cover as much real-world situations as possible.</p>
<p>Zend Framework and Symfony in particular do an absolutely great job of this. When you can have a pager component push its data to the page through a progressive enhancement component, and log its performance to FirePHP when an user authentication component  determines that the viewing user is a developer, and all of it works by plugging square pegs into square holes, you know there has been a lot of great work going on below the hood.</p>
<h4>Back to the point</h4>
<p>Using a framework is all fun and games until you need to disagree with it. You need to plug out what does not work, and plug your own implementation in its place. The more complex the vocabulary, and the harder it will be to write new code — frameworks make it easy to connect existing components, at the cost of having to deal with more concepts when actually implementing new things.</p>
<p>What it boils down to, in the end, is whether you expect to be reusing a lot of third party components, or to write a lot of your own code. In the latter case, MicroPHP — and lean environments that do not have a heavy framework side to them — is actually an improvement over trying to fit a six-inch wooden square peg into a mini-USB port.</p>
<p>The exception to this is, of course, being so familiar with a particular framework that you immediately know what changes you need to do without fighting against third party code.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2012/01/frameworks-libraries-conventions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Consensus and Compromise</title>
		<link>http://www.nicollet.net/2011/12/consensus-and-compromise/</link>
		<comments>http://www.nicollet.net/2011/12/consensus-and-compromise/#comments</comments>
		<pubDate>Fri, 30 Dec 2011 16:54:29 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Discussion]]></category>
		<category><![CDATA[Management]]></category>
		<category><![CDATA[Psychology]]></category>
		<category><![CDATA[Start-Up]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2645</guid>
		<description><![CDATA[Working on a start-up involves many decisions — how features should work, how pages should look like, how advertising should be written&#8230; and making decisions is a difficult process when working as a team. Even in the tightest-knit team of two, disagreements happen. Sometimes, on the less important decisions, you might get out of it [...]]]></description>
			<content:encoded><![CDATA[<p>Working on a start-up involves many decisions — how features should work, how pages should look like, how advertising should be written&#8230; and making decisions is a difficult process when working as a team. Even in the tightest-knit team of two, disagreements happen.</p>
<p>Sometimes, on the less important decisions, you might get out of it by default — A disagrees with B, but doesn&#8217;t care enough about the topic to actually do something about it, so B proceeds anyway.</p>
<p>Sometimes, decisions will be dictated by competence. If A disagrees with a proposed solution because of objective technical or legal implications, well, that&#8217;s it.</p>
<p>Quite often, though, there is no such solution. This is where one must be aware of the dangerous tendency of <em>consensus</em>-based teams to devolve into <em>compromise</em>-based discussions.</p>
<p><strong>Consensus</strong>-based discussions feel like this :</p>
<blockquote><p>A: I&#8217;m going to frobnicate the thingamajig.<br />
B: If you frobnicate the foobar instead, you would get a 10% increase in floogum output.<br />
A: You&#8217;re right. Let&#8217;s frobnicate the foobar, then.</p></blockquote>
<p>That is, initially differing opinions evolve through an <strong>argumentation</strong> process, so that everyone agrees that the final outcome is the best one.</p>
<p><strong>Compromise</strong>-based discussions feel like this :</p>
<blockquote><p>A: I&#8217;m going to frobnicate the thingamajig.<br />
B: I would rather that we frobnicate the bazquux instead. Might get better results.<br />
A: Actually, I have a hunch that the thingamajig is the better option.<br />
B: Tell you what, let&#8217;s frobnicate 50% of the thingamajig and 50% of the bazquux. This way we have everything covered.<br />
A: All right.</p></blockquote>
<p>Here, initially differing opinions are settled through a <strong>negotiation</strong> process, so that everyone&#8217;s input is respected, but no one agrees the final outcome is really the best.</p>
<p>Compromise is a bad idea for several reasons.</p>
<ul>
<li>Quite often, going 100% with any reasonable solution is better than investing only 50% into two different solutions. In more general terms, lack of commitment to a single strategy or objective is a dangerous thing to do, and often less effective than commiting to any strategy or objective one might come up with.</li>
<li>Compromise is not agreement, it&#8217;s negotiation : you give something and get something else in return until everyone agrees that it&#8217;s an even trade. People who are good negotiators tend to dictate their terms in such circumstances, and others might feel helpless and useless after a few unbalanced trades. It also leads to « I agreed with your idea yesterday so agree with mine today » or « You&#8217;ve already changed my idea enough, stop asking me to change it again, » both of which are content-free sentences that aim to make a decision based on interpersonal history instead of objective analysis.</li>
<li>The inability to come to a logical conclusion sometimes happens because there is not enough data available to decide. The first priority in such circumstances should be to actually search for the data (possibly accepting one of the proposed solutions as temporary until the data is collected), not settling for an arbitrary compromise.</li>
<li>Building consensus is hard when people have trouble putting their insight into words, no matter how convincing or true that insight might be. I&#8217;m objectively right, but I cannot seem to explain to you the reason for it. This can mean going for a compromise today instead of a consensus tomorrow — hasty decisions are seldom good. Always make sure everyone has had enough time to think the decision through, even if it means adjourning it until later.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/12/consensus-and-compromise/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Two-way bindings</title>
		<link>http://www.nicollet.net/2011/12/two-way-bindings/</link>
		<comments>http://www.nicollet.net/2011/12/two-way-bindings/#comments</comments>
		<pubDate>Thu, 29 Dec 2011 18:42:05 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Functional]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Objective Caml]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2640</guid>
		<description><![CDATA[Quick : how would you design a function that opens a file handle that auto-closes whenever execution leaves a certain scope, even if an exception happens ? The C++ solution is quite straightforward : have a destructor that closes the file handle, and create the handle as an auto variable: std::ofstream out = std::ofstream("out.txt"); out [...]]]></description>
			<content:encoded><![CDATA[<p>Quick : how would you design a function that opens a file handle that auto-closes whenever execution leaves a certain scope, even if an exception happens ?</p>
<p>The C++ solution is quite straightforward : have a <em>destructor</em> that closes the file handle, and create the handle as an auto variable:</p>
<pre style="padding-left: 30px;">std::ofstream out = std::ofstream("out.txt");
out &lt;&lt; whatever();
// file handle is always closed by the language</pre>
<p>On the other hand, OCaml does not have destructors, but can simulate the RAII spirit <a href="http://thelema.github.com/AAA-batteries/hdoc/BatFile.html#VALwith_file_in" target="_blank">using a closure</a>: you provide a function that will be called with the file handle as its argument, and the file handle will be destroyed when the function returns or raises an exception.</p>
<pre style="padding-left: 30px;">BatFile.with_file_out "out.txt" begin fun out -&gt;
  BatIO.nwrite out (whatever ())
  (* the file handle is always closed by the language *)
end</pre>
<p>This is a special case of a more general principle.</p>
<h4>Two-way binding</h4>
<p>The standard <code>let</code> keyword performs a one-way binding: bind value to variable <em>then</em> evaluate expression. Two-way binding adds a post-processing step : when you&#8217;re done with the expression, do something else. Such a behavior has important consequences for writing concise and readable code.</p>
<p>In my OCaml code, two-way binding is performed with keyword let! that is preprocessed as follows :</p>
<pre style="padding-left: 30px;">let! pattern = value in expression
(* Is translated to *)
value (fun pattern -&gt; expression)</pre>
<p>For instance, the above file manipulation script would be written as:</p>
<pre style="padding-left: 30px;">let! out = BatFile.with_file_out "out.txt" in
BatIo.nwrite out (whatever ())</pre>
<p>This syntax expresses the actual intent of the code better than the anonymous callback syntax did: <em>bind the file handle to this variable, but don&#8217;t forget the post-processing steps</em>.</p>
<p>Here are a few more examples of situations that may be improved by this syntax :</p>
<h4>Events and reactive programming</h4>
<p>Reactive programs can be constructed either using the typical &#8220;<em>register this function to be called whenever this value changes or this event happens</em>&#8221; semantics, or  by using binding semantics instead:</p>
<pre style="padding-left: 30px;">let () =
  let! user = User.on_change (#last_login) in
  if user # notify_login then
    Mail.send (user # email)
      ("Someone has logged in to your account at " ^ datetime (user # last_login))</pre>
<p>The underlying signature of <code>User.on_change</code> (which registers a listener callback and returns unit) remains the same.</p>
<h4>Retry semantics</h4>
<p>CouchDB implements transactions with retry semantics: you read a document, compute some changes and try saving them back, and  if the document was changed by someone else in the mean time, you will have to try again. It makes sense for the code inside the transaction to be 1° idempotent and 2° wrapped away in a function that 3° takes the latest version of the document as an argument :</p>
<pre style="padding-left: 30px;">let set_title article_id new_title =
  let! article = Database.transaction article_id in
  Database.write article_id { article with title = new_title }</pre>
<p>In such a design, the write function would throw a specific exception if a collision occurs, and the transaction function would intercept that exception and try again until the transaction succeeded or a maximum number of retries happened.</p>
<h4>Monads</h4>
<p>Value binding in monads benefits from having a syntax that actually looks like binding.With the option monad, one can turn this :</p>
<pre style="padding-left: 30px;">match Files.get file_id with None -&gt; None | Some file -&gt;
  match file # owner with None -&gt; None | Some user_id -&gt;
    match Users.get user_id with None -&gt; None | Some user -&gt;
      Some (user # name)</pre>
<p>Into a more straightforward version :</p>
<pre style="padding-left: 30px;">let  open BatOption.Monad in
let! file    = bind $ Files.get file_id in
let! user_id = bind $ file # owner in
let! user    = bind $ Users.get user_id in
return (user # name)</pre>
<p>Also, one can deal with Lwt threads almost as well as the Lwt-specific syntax extension:</p>
<pre style="padding-left: 30px;">open Lwt
open Lwt_io

let process_lines channel process =
  let loop () =
    let! line_opt = bind $ read_line_opt channel in
    match line_opt with
      | None -&gt; return ()
      | Some line -&gt; loop () &lt;&amp;&gt; process line
  in
  loop ()</pre>
<h4>Being Silly</h4>
<pre style="padding-left: 30px;">let fold init list f = List.fold_left (fun acc x -&gt; f (acc,x)) init list
let map list f       = List.map f list

let probabilities odds =
  let sum =
    let! accumulator, odd = fold 0. odds in
    accumulator +. float_of_int odd
  in
  let! odd = map odds in
  float_of_int odd /. sum</pre>
<h4>The Syntax Extension</h4>
<p>In case you don&#8217;t know how to create it, this is the preprocessor file for this syntax extension :</p>
<pre style="padding-left: 30px;">open Camlp4.PreCast
open Syntax

EXTEND Gram
 GLOBAL: expr;

 expr: LEVEL "top"
 [
   [ "let"; "!"; p = patt ; "=" ; e = expr ; "in" ; e' = expr -&gt;
     &lt;:expr&lt; (($e$) (fun $p$ -&gt; $e'$)) &gt;&gt; ]
 ] ;

END;</pre>
<p>By the way, I find that this extension has a significant advantage over the Lwt extension &#8211; it is readily compatible with syntax highlighting in most editors.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/12/two-way-bindings/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>How to Annoy People</title>
		<link>http://www.nicollet.net/2011/12/how-to-annoy-people/</link>
		<comments>http://www.nicollet.net/2011/12/how-to-annoy-people/#comments</comments>
		<pubDate>Tue, 06 Dec 2011 14:48:51 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[RunOrg]]></category>
		<category><![CDATA[Start-Up]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2637</guid>
		<description><![CDATA[Here is a hypothetical situation : The kingdom is in trouble, and the King must enact a new law. Of course, such a law will make many people happy for years, but some people will be annoyed by it for a few days. He has two possible choices : Law A will make 20% of the [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2638" title="crown" src="http://www.nicollet.net/wp-content/uploads/2011/12/crown.png" alt="" width="675" height="100" /></p>
<p>Here is a hypothetical situation :</p>
<blockquote><p>The kingdom is in trouble, and the King must enact a new law. Of course, such a law will make many people happy <em>for years</em>, but some people will be annoyed by it <em>for a few days</em>. He has two possible choices :</p>
<p>Law A will make <strong>20%</strong> of the people happy at the cost of annoying <strong>1%</strong>.</p>
<p>Law B will make <strong>90%</strong> of the people happy at the cost of annoying <strong>10%</strong>.</p>
<p>Which law should the monarch enact ?</p></blockquote>
<p>This is not the kind of problem that engineer types like me enjoy — no matter how much we try to abstract away the details and build a sound foundation for that decision, some <em>ethics</em> will inevitably seep in.  Is it right to annoy an additional 9% of the people so that 70% more people become happier? Where do we draw the line in term of percentage, and in terms of annoyance — a few days might be fine, but what about a few months or years?</p>
<p>As you might expect, this is not an entirely hypothetical exercise. It is, in fact, the very core of the opt-in versus opt-out debate. Above, law A is <strong>opt-in</strong> : only 21% of the population knows about its effects, but at least there will be very few complaints ; law B is <strong>opt-out</strong> : it ensures that 100% of the population knows about it, but it will annoy the 20% who will have to manually opt out.</p>
<p>My start-up hosts discussion forums for associations. Every one of our customers has faced the same decision : should they send an e-mail to their association members telling them « please come and sign up on our forums » or should they import their member directory and let naysayers unsubscribe ?</p>
<p>With the opt-in approach — please come and sign up — the initial e-mail is followed by a small number of sign ups, usually from the more active or dedicated members, and will be ignored by everyone else. If the number of initial adopters does not reach a critical mass, the forums will simply die off and be forgotten by everyone.</p>
<p>With the opt-out approach — import all the members — everyone will be notified every time a new message is posted, as would happen on a mailing-list. A forum message from a friend is a lot more interesting than a « please come and sign up » e-mail and drives more members to connect and participate. The critical mass is reached far easier (and faster!) and the forum becomes an essential part of the community. However, those who did not wish to participate will receive e-mail that is <em>literally</em> unsolicited, and they will complain about it — while the number of active users increases significantly, the number of complaints and unsubscription requests increases even faster. The delicious irony of it all is that the number of complaints is driven up <em>because</em> the communication tool helps those annoyed members find each other and speak up in unison.</p>
<p><strong>Before continuing, let me fend off two possible problems.</strong></p>
<p>First, the opt-out approach is <em>not</em> intended to be a sneaky trick — we always strongly advise customers to send a preliminary e-mail to all their members in advance, telling them about the plan to move to a new discussion system. Not only does it keep things civil and honest, but people who absolutely hate receiving messages from their community can ask to opt out before it is too late. Online communities have trouble blooming when 10% of the messages are complaints about the very existence of the community, so it is in everyone&#8217;s interests to keep naysayers out.</p>
<p>And if anything else fails, we can wipe out members from our system on demand.</p>
<p>The second problem will be familiar to readers of Seth Godin — <strong>permission</strong>. Through the eyes of a permission marketer, to import all  the members without their prior explicit opt-in consent is absolute heresy.</p>
<p>I find this view a bit too extreme. Well, it does make sense when trying to sell things that 99.99% of the people will not care about, such as viagra or cheap hotels in Bangkok. But members actually <em>care</em> about what is going on in their association, and — based on our experience — only a minority of members ever asks to be completely removed from the forums. Most members only unsubscribe from individual discussions that they do not care about, and choose to remain available on the forums as a whole.</p>
<p>It feels sad that so many people would miss out on a great experience just so a handful of curmudgeons can spare the effort of clicking an unsubscribe link.</p>
<p><strong>Back to the point</strong>.</p>
<p>As far as I can tell, the approach that helps most members get involved in an online community is :</p>
<ol>
<li>In advance, send them an e-mail telling them that the association is about to move to another online community system — in our situation, it could be described as a mix between a forum (for those who wish to be very active) and a mailing-list (for the occasional participants) — and that each of them will receive those communications <strong>on an opt-out basis</strong>.</li>
<li>You will receive messages saying « this is a bad idea » or « I don&#8217;t want to receive those communications » and you should take steps to make sure that no person who opted out at this stage is <em>ever</em> imported into the forums. Unless they ask for it later, anyway.</li>
<li>After enough time has passed — at least 48 hours for large associations — import everyone into the forums, and write a welcome message there explicitly asking them to say hello when they reach it for the first time.</li>
<li>If any other people request to be completely unsubscribed, you may simply remove them from your forums and they will stop receiving messages. If you need to absolutely make sure that all their data has been wiped from our database, drop us a line and we&#8217;ll take care of it for you.</li>
</ol>
<p>We have had several customers build a thriving online community for their association with this approach, and have even seen a few « get me out of here » naysayers change their minds and come back online, once they understood everyone else was using it.</p>
<p>Where do <em>you</em> stand on the matter ?</p>
<p><small>Article image © Carlo Piana — <a href="http://www.flickr.com/photos/27561445@N04/4821130136/">Flickr</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/12/how-to-annoy-people/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Basic Patterns for Everyday Programming</title>
		<link>http://www.nicollet.net/2011/11/basic-patterns-for-everyday-programming/</link>
		<comments>http://www.nicollet.net/2011/11/basic-patterns-for-everyday-programming/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 15:48:19 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Functional]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Objective Caml]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2624</guid>
		<description><![CDATA[Lakshen Perera provides a list of basic patterns for everyday programming, illustrated in Javascript and Ruby. I thought it would be interesting to provide an OCaml illustration as well, and perhaps a handful of additional patterns as well. Verify object&#8217;s availability before calling its methods or properties In many languages, there is a possibility for [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2635" title="pattern" src="http://www.nicollet.net/wp-content/uploads/2011/11/pattern.png" alt="" width="675" height="100" /></p>
<p><a href="http://laktek.com/2011/11/23/basic-patterns-for-everyday-programming/" target="_blank">Lakshen Perera</a> provides a list of basic patterns for everyday programming, illustrated in Javascript and Ruby. I thought it would be interesting to provide an OCaml illustration as well, and perhaps a handful of additional patterns as well.</p>
<h4>Verify object&#8217;s availability before calling its methods or properties</h4>
<p>In many languages, there is a possibility for objects to be missing — whether this is represented as <code>NULL</code>, <code>null</code>, <code>nil</code> or <code>None</code>. Regardless of the language, it is important to keep in mind at all times whether a given value is optional or not. If it is not optional, then you may assert so using a type declaration if your language supports it :</p>
<pre style="padding-left: 30px;"><strong>method</strong> name  : string <span style="color: #008000;">(* not optional *)</span>
<strong>method</strong> email : string option <span style="color: #008000;">(* optional *)</span></pre>
<p>In other languages, a runtime assert will do, for instance in PHP :</p>
<pre style="padding-left: 30px;"><strong>assert</strong> (isset($this-&gt;name))</pre>
<p>OCaml will not require you to explicitly declare all values as optional or mandatory. Instead, it will deduce that information from the way you use each value. For instance, since <code>json_of_string</code> expects a non-optional string argument, you can simply write :</p>
<pre style="padding-left: 30px;"><strong>let</strong> parsed_content = json_of_string json <span style="color: #008000;">(* json is not optional *)</span></pre>
<p>If json is an optional string, this will create a compilation error unless you explicitly define what should happen when the string is missing. The most common approach is to decide that the result should be missing too :</p>
<pre style="padding-left: 30px;"><strong>let</strong> parsed_content = <strong>match</strong> json <strong>with</strong>
  | <span style="color: #008080;">None</span>   -&gt; <span style="color: #008080;">None</span>
  | <span style="color: #008080;">Some</span> s -&gt; <span style="color: #008080;">Some</span> (json_of_string s) <span style="color: #008000;">(* by definition, s is not optional *)</span></pre>
<p>If using the Batteries library (which, by the way, you should), you can express this more easily :</p>
<pre style="padding-left: 30px;"><strong>let</strong> parsed_content = <span style="color: #008080;">BatOption</span>.map json_of_string json</pre>
<h4>Set a default value with assignments</h4>
<p>This advice is almost identical to the previous one : if you need to construct a non-optional value, but only have access to an optional value, you will have to provide a default value. Using standard OCaml :</p>
<pre style="padding-left: 30px;"><strong>let</strong> role = <strong>match</strong> person.role <strong>with</strong>
  | <span style="color: #008080;">None</span>      -&gt; <span style="color: #008080;">`Guest</span>
  | <span style="color: #008080;">Some</span> role -&gt; role</pre>
<p>Using Batteries, there is an almost equivalent version :</p>
<pre style="padding-left: 30px;"><strong>let</strong> role = <span style="color: #008080;">BatOption</span>.default <span style="color: #008080;">`Guest</span> person.role</pre>
<p>This version is almost equivalent, because it will evaluate the default value even if it is not required. Let&#8217;s assume that the default value is itself provided by a complex computation, such as a database access :</p>
<pre style="padding-left: 30px;"><strong>let</strong> role = <strong>match</strong> person.role <strong>with</strong>
  | <span style="color: #008080;">None</span>      -&gt; readfrom database <span style="color: #008000;">(* only executed if person.role is missing *)</span>
  | <span style="color: #008080;">Some</span> role -&gt; role 

<strong>let</strong> role = <span style="color: #008080;">BatOption</span>.default
  (readfrom database) <span style="color: #008000;">(* Always executed, even if unnecessary *)</span>
  role</pre>
<p>The Batteries library provides an alternate function for that :</p>
<pre style="padding-left: 30px;"><strong>let</strong> role = <span style="color: #008080;">BatOption</span>.map_default
  readfrom database <span style="color: #008000;">(* only executed if person.role is missing *)</span>
  role</pre>
<h4>Checking whether a variable equals to any of the given values</h4>
<p>If the values have a legitimate reason to be strings, integers or other types with unlimited numbers of values, then « is in list » predicates are the preferred choice :</p>
<pre style="padding-left: 30px;"><strong>if </strong><span style="color: #008080;">List</span>.mem current_day [<span style="color: #ff0000;">"Monday"</span>;<span style="color: #ff0000;">"Wednesday"</span>;<span style="color: #ff0000;">"Friday"</span>] <strong>then</strong>
  <span style="color: #008000;">(* do something *) </span></pre>
<p>Days are a quite bad example, because these are better represented as a variant (which then type-checks whether you have written<em> Mornday</em> instead of <em>Monday</em>). If you have already defined the type of weekdays, then :</p>
<pre style="padding-left: 30px;"><strong>if</strong> <span style="color: #008080;">List</span>.mem (current_day : weekday) [<span style="color: #008080;">`Monday</span>;<span style="color: #008080;">`Wednesday</span>;<span style="color: #008080;">`Friday</span>] <strong>then</strong>
  <span style="color: #008000;">(* do something *)</span></pre>
<p>If this is a one-shot check, or if you would rather not define a weekday type yet, you should instead go for an exhaustive pattern-matching :</p>
<pre style="padding-left: 30px;"> <strong>match</strong> current_day <strong>with</strong>
  | <span style="color: #008080;">`Monday</span> | <span style="color: #008080;">`Wednesday</span> | <span style="color: #008080;">`Friday</span> -&gt;
    <span style="color: #008000;">(* do something *)</span>
  | <span style="color: #008080;">`Tuesday</span> | <span style="color: #008080;">`Thursday</span> | <span style="color: #008080;">`Saturday</span> | <span style="color: #008080;">`Sunday</span> -&gt; ()</pre>
<h4>Extract complex or repeated logic into functions</h4>
<p>This is a fairly fundamental concept — but it consists in two distinct parts. There is a separation and naming part (you pull out a piece of code and give it a name, which helps understand what it does and how it relates to the rest of the program) and there is an extraction and reuse part (the piece of code is pulled out into a more globally accessible location and parametrized, so that it may be used in other places).</p>
<p>With the above example, simple separation-and-naming would be :</p>
<pre style="padding-left: 30px;"><strong>let </strong>is_discount_day = <strong>match </strong>current_day <strong>with</strong>
 | <span style="color: #008080;">`Monday</span> | <span style="color: #008080;">`Wednesday</span> | <span style="color: #008080;">`Friday</span> -&gt; current_date &gt; <span style="color: #ff0000;">20</span>
 | <span style="color: #008080;">`Tuesday</span> | <span style="color: #008080;">`Thursday</span> | <span style="color: #008080;">`Saturday</span> | <span style="color: #008080;">`Sunday</span> -&gt; <span style="color: #ff0000;">false</span>
<strong>in</strong>

<strong>if </strong>is_discount_day <strong>then </strong>
  <span style="color: #008000;">(* do something *)</span></pre>
<p>The variable is defined in the same scope it is use in, and it assumes that <code>current_day</code> and <code>current_date</code> values have been defined previously in that scope. Extraction-and-reuse would go further :</p>
<pre style="padding-left: 30px;"><strong>type </strong>weekday =
  [ <span style="color: #008080;">`Monday</span> | <span style="color: #008080;">`Tuesday</span> | <span style="color: #008080;">`Wednesday</span>
  | <span style="color: #008080;">`Thursday</span> | <span style="color: #008080;">`Friday</span> | <span style="color: #008080;">`Saturday</span> | <span style="color: #008080;">`Sunday</span> ]
<strong>
let </strong>is_discount_day (day:weekday) date =
<span style="color: #008080;">  List</span>.mem day [<span style="color: #008080;">`Monday</span>;<span style="color: #008080;">`Wednesday</span>;<span style="color: #008080;">`Friday</span>] <strong>&amp;&amp;</strong> date &gt; <span style="color: #ff0000;">20</span>

...

  <strong>if </strong>is_discount_day current_day current_date <strong>then </strong>
    <span style="color: #008000;">(* do something *)</span></pre>
<p>Now, is-discount-day is a global function available from everywhere in the code, and it uses the provided parameters to determine whether this is indeed a discount day.</p>
<h4>Memoize the results of repeated function calls</h4>
<p>OCaml has several ways to perform memoization. One of them is lazy evaluation :</p>
<pre style="padding-left: 30px;"><strong>val</strong> discount_day = <strong>lazy</strong> (is_discount_day current_day current_date)
<strong>method</strong> discount_day = <span style="color: #008080;">Lazy</span>.force discount_day</pre>
<p>The lazy expression will only be evaluated the first time the <code>Lazy.force</code> function is called on it.</p>
<p>Note that if the current day or current date can change, then the memoization actually <em>breaks</em> things !</p>
<p>Memoization is also helpful when dealing with a function that requires arguments, in which case a different result will be provided for each argument set. A common solution is to use a hash table to store these :</p>
<pre style="padding-left: 30px;"><strong>let</strong> fibonacci =
  <strong>let</strong> memo = <span style="color: #008080;">Hashtbl</span>.create <span style="color: #ff0000;">100</span> <strong>in</strong>
  <strong>let rec</strong> fib n =
    <strong>try</strong> <span style="color: #008080;">Hashtbl</span>.find memo n <strong>with </strong><span style="color: #008080;">Not_found</span> -&gt;
      <strong>let</strong> result = fib (n-<span style="color: #ff0000;">1</span>) + fib (n-<span style="color: #ff0000;">2</span>) <strong>in</strong>
      <span style="color: #008080;">Hashtbl</span>.add memo n result ; result
  <strong>in</strong> fib</pre>
<p>This works fine for short-lived functions — don&#8217;t do this for global functions that might stick around for a long time, because the memoization hash table will grow and its contents will never be garbage-collected. If you really have to, use a <em>weak</em> hash table, such as Batteries&#8217; <code>BatInnerWeaktbl</code>, so that the garbage collector may reclaim the memoized values when it runs out of memory.</p>
<p>Also don&#8217; t overdo memoization — it only works when arguments are reliably passed more than once <em>and</em> the time to compute the value is significantly larger than the time to retrieve and store it <em>and</em> it is worth the memory usage <em>and </em>the function has no side-effects.</p>
<h4>Use the seven list manipulation primitives</h4>
<p>Almost any processing on collections of items can be expressed in terms of seven fundamental patterns. Recognizing those patterns can help improve the clarity of both the code and the underlying algorithm.</p>
<p><strong>1. Map</strong> transforms a list into another, item by item, in linear time. Use a map operation when all you need is a one-to-one transformation. The line below extracts three recipes from the database using their identifier.</p>
<pre style="padding-left: 30px;"><strong>let</strong> recipes = <span style="color: #008080;">List</span>.map from_database [ <span style="color: #ff0000;">"omelet"</span> ; <span style="color: #ff0000;">"cheeseburger"</span> ; <span style="color: #ff0000;">"risotto"</span> ]</pre>
<p><strong>2. Reduce</strong> transforms a list of values into a single value by repeatedly applying a function that combines together two values into one. The typical example is a fold, which uses a function to combine each list element, in turn, with an accumulator. It can be used to extract the sum of values in a list, for example :</p>
<pre style="padding-left: 30px;"><strong>let</strong> total = <span style="color: #008080;">List</span>.fold_left (+) <span style="color: #ff0000;">0</span> [ <span style="color: #ff0000;">5</span> ; <span style="color: #ff0000;">6</span> ; <span style="color: #ff0000;">3</span> ; <span style="color: #ff0000;">8</span> ; <span style="color: #ff0000;">9</span> ; <span style="color: #ff0000;">0</span> ; <span style="color: #ff0000;">7</span> ; <span style="color: #ff0000;">6</span> ]</pre>
<p>This transform allows a preliminary map step which transform the values inside the list into values that can be combined. For instance, to find the age of the oldest person in a list of people :</p>
<pre style="padding-left: 30px;"><strong>let</strong> oldest = <span style="color: #008080;">List</span>.fold_left (<strong>fun</strong> acc person -&gt; max age person.age) <span style="color: #ff0000;">0</span> people</pre>
<p><strong>3. Extract </strong>works like a map, but the transformation function returns zero, one or more results for each call. All the results are included in the final list. The most elementary implementation is literally to have a map (that transforms a list into a list of lists) followed by a concatenation (that transforms a list of lists into a list). For instance, to get all the ingredients involved in a list of recipes :</p>
<pre style="padding-left: 30px;"><strong>let </strong>ingredients =
  <span style="color: #008080;">List</span>.concat (<span style="color: #008080;">List</span>.map (<strong>fun</strong> recipe -&gt; recipe.ingredients) recipes)</pre>
<p><strong>4. Filter</strong> is a subset of Extract where the transform may not return more than one result — but it may still return none, so its result is simply an optional type. For instance, to extract the list of all recipes that have a wine recommandation along with their recommended wine :</p>
<pre style="padding-left: 30px;"><strong>let</strong> wines = <span style="color: #008080;">BatList</span>.filter_map
  (<strong>fun</strong> recipe -&gt; <span style="color: #008080;">BatOption</span>.map (<strong>fun</strong> wine -&gt; recipe,wine) recipe.wine) recipes</pre>
<p>The OCaml language also provides a standard <code>List.filter</code> function which keeps values for which a property is true. For instance, to get the list of recipes that have a wine recommendation :</p>
<pre style="padding-left: 30px;"><strong>let</strong> have_wines = <span style="color: #008080;">List</span>.filter (<strong>fun</strong> recipe -&gt; recipe.wine &lt;&gt; <span style="color: #008080;">None</span>) recipes</pre>
<p>This approach is weaker — the wines in the resulting list are still treated as optional by the type system, so you will need bogus pattern matching for a case that never happens (no wine) to extract the actual wine values. A <code>filter_map</code> lets you encode the filter property in the type of the result, which makes using the filtered list easier.</p>
<p><strong>5. Sort</strong> unsurprisingly sorts the list. The canonical sort — using the canonical order relationship — is a theoretical curiosity, and in practice most sorts use a <em>projection function</em> p such that A &lt; B iff p(A) &lt; p(B). This is best illustrated in SQL, in the form of the ORDER BY &lt;projection&gt; statement. Two useful helper functions :</p>
<pre style="padding-left: 30px;"><strong>let</strong> project compare p a b = compare (p a) (p b)
<strong>let</strong> descending compare a b = compare b a</pre>
<p>For instance, to sort the list of recipes based on how long each of them takes :</p>
<pre style="padding-left: 30px;"><strong>let</strong> by_duration =
  <span style="color: #008080;">List</span>.sort (project compare (<strong>fun</strong> recipe -&gt; recipe.duration)) recipes</pre>
<p><strong>6. Group</strong> works like Sort, but further regroups « equal » items together by returning a list of lists. It works using a comparison function that is usually based on a projection function. For instance, to get three lists containing one-star, two-star and three-star recipes :</p>
<pre style="padding-left: 30px;"><strong>let</strong> by_stars =
  <span style="color: #008080;">BatList</span>.group (project compare (<strong>fun</strong> recipe -&gt; recipe.stars)) recipes</pre>
<p>There is a special case when the projection function returns booleans, which is known as a <em>partition</em>. For instance, to extract recipes that are desserts and recipes that are not :</p>
<pre style="padding-left: 30px;"><strong>let</strong> desserts, non_desserts =
<span style="color: #008080;">  BatList</span>.partition (<strong>fun</strong> recipe -&gt; recipe.is_dessert) recipes</pre>
<p><strong>7. Search</strong> extracts one element from the list (if possible) based on a certain condition or property. Elementary searches are « first element » and « last element. » More complex searches : finding a value by key in a list of key-value pairs using <code>List.assoc</code> or using a predicate using <code>List.find</code>. The heavy-duty search tool is <code>BatList.find_map</code>, used below to find a recipe that is recommended by at least one person, and the recommending person :</p>
<pre style="padding-left: 30px;"><strong>let </strong>recipe, recommender = <span style="color: #008080;">BatList</span>.find_map
  (<strong>fun</strong> recipe -&gt; <span style="color: #008080;">BatOption</span>.map (<strong>fun </strong>person -&gt; recipe, person) recipe.recommended)
  recipes</pre>
<p>These seven patterns can be used in conjunction to perform almost any algorithm on collections, sequences or lists. For a more complex example, assume we need the ten ingredients that appear the most often in desserts. We would filter recipes by desserts, extract their ingredients, group them by name, sort the sub-lists by list length and take the name of the first element of the first ten sub-lists :</p>
<pre style="padding-left: 30px;"><strong>open </strong><span style="color: #008080;">BatPervasives</span> <span style="color: #008000;">(* for operator |&gt; *)</span>
<strong>open </strong><span style="color: #008080;">BatList
</span>
<strong>let</strong> ten_best_ingredients recipes = recipes
  |&gt; filter (<strong>fun</strong> r -&gt; r.dessert)
  |&gt; map (<strong>fun</strong> r -&gt; r.ingredients) |&gt; concat
  |&gt; group (project compare (<strong>fun</strong> i -&gt; i.name))
  |&gt; sort (descending (project compare length))
  |&gt; take <span style="color: #ff0000;">10</span>
  |&gt; map hd
  |&gt; map (<strong>fun</strong> i -&gt; i.name)</pre>
<p><small>Article &copy; brewbooks &mdash; <a href="http://www.flickr.com/photos/brewbooks/3203211847/">Flickr</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/11/basic-patterns-for-everyday-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Comment Branches</title>
		<link>http://www.nicollet.net/2011/11/comment-branches/</link>
		<comments>http://www.nicollet.net/2011/11/comment-branches/#comments</comments>
		<pubDate>Thu, 17 Nov 2011 18:42:36 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Functional]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Design Patterns]]></category>
		<category><![CDATA[Productivity]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2619</guid>
		<description><![CDATA[Your development job is making changes in your software. Writing, testing and debugging those changes takes some time. If your job is anywhere as hectic as mine, you will have to fix and deploy urgent patches, even when your application code is in a half-written, half-debugged state because of the feature of the month. This is [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2620" title="branches" src="http://www.nicollet.net/wp-content/uploads/2011/11/branches.png" alt="" width="675" height="100" />Your development job is making changes in your software. Writing, testing and debugging those changes takes some time.</p>
<p>If your job is anywhere as hectic as mine, you will have to fix and deploy urgent patches, even when your application code is in a half-written, half-debugged state because of <em>the feature of the month</em>.</p>
<p>This is what <em>branches</em> are for. You keep two versions of the code, one of which is called the <strong>trunk </strong>and is always ready for deployment, and another which holds the changes that you are working on.</p>
<p>When your feature is done, you <em>merge</em> the two versions together. You want to keep the merge operation painless. To do so, you have several kinds of branches available.</p>
<p>The <strong>repository branch</strong> is built into your SourceSafe/subversion/git/whatever. It creates two independent copies, and you need to migrate changes from the trunk to every branch out there as soon as possible, or the merge will make you wish for a sweet and merciful death.</p>
<p>By the way, changeset-oriented tools (like git or mercurial) make this easer, while revision-oriented tools (like subversion) make it harder.</p>
<p>The <strong>feature branch</strong> is done using programming logic. The code you deploy to production supports the new feature, but it is turned off for everyone except yourself. This technique is great for adding features, but inefficient when changing existing ones.</p>
<p>A side effect of the feature branch is that you can stress-test new code by rolling it out to increasing numbers of users progressively.</p>
<p>The <strong>comment branch</strong> is an odd gambit. It involves ripping out an entire module and replacing it with another that has a <em>different</em> interface. This will involve large amounts of re-wiring all over the code base, and these will take hours or days before they can be compiled, let alone <em>tested</em>.</p>
<p>Use a comment structure such as this one:</p>
<pre style="padding-left: 30px;"><span style="color: #008000;">/*[*/</span> old code <span style="color: #008000;">/*|* new code *]*/</span></pre>
<p>It is trivial to build a text-replacement macro that turns the above into the code below and back:</p>
<pre style="padding-left: 30px;"><span style="color: #008000;">/*[* old code *|*/</span> new code <span style="color: #008000;">/*]*/</span></pre>
<p>Use the macro to switch between development mode (when you write new code and desperately try to get it to compile) and fix mode (when you edit the old code and deploy it). For consistency, always commit the <em>old </em>version to the repository.</p>
<p>Why use <strong>comment branches</strong> instead of <strong>repository branches</strong> ? Maybe your source control tool sucks at branches. I use Subversion. Yes, I know. Legacy, pain and unlikely hopes of a brighter future.</p>
<p>When a trunk change occurs in a part that has been erased or reworked in the branch, that change <em>will</em> cause a conflict that <em>will</em> require manual intervention. Even with git or mercurial. For a large number of small changes sprinkled over a large codebase that is routinely involving many small updates, repository branches turn into a merge minefield.</p>
<p>Does your branch involve a small number of well-defined files ?</p>
<p>Then you should use <strong>repository branches</strong>, because conflicts will only happen in those files, and will usually be easy to fix.</p>
<p>Does your branch involve many changes in many files everywhere in the project ?</p>
<p>Then use <strong>comment branches</strong>.</p>
<p>Last and possibly least, there is the <strong>TODO-branch</strong>. This involves non-breaking, purely cosmetic changes. 25% of my project uses this syntax for historical reasons:</p>
<pre style="padding-left: 30px;">Table.get id |-&gt; function
   | None       -&gt; return 0
   | Some value -&gt; return value.count</pre>
<p>Then, a convention change happened, and this is used instead:</p>
<pre style="padding-left: 30px;">let! value_opt = breathe (Table.get id) in
match value_opt with  
   | None       -&gt; return 0
   | Some value -&gt; return value.count</pre>
<p>Then, another convention change happened, and this should be used instead</p>
<pre style="padding-left: 30px;">let! value = breathe_req_or (return 0) (Table.get id) in
return value.count</pre>
<p>And then, there&#8217;s the current version:</p>
<pre style="padding-left: 30px;">let! value = breathe_req_or (return 0) $ Table.get id in
return value.count</pre>
<p>Whenever I change coding conventions, I do not spend the time to reformat the tens of thousands of lines of code in my application. That would have been wasteful. Instead, every time a piece of code is refactored, it is refactored to the most recent style.</p>
<p>The same happens when using an old and a new version of a given API. My code uses two libraries for handling HTML forms, uses both Javascript and Coffeescript, and a variety of similar two-hammers-one-nail situations.</p>
<p>These are, for all practical purposes, branches. They are work that is being performed for long durations. The benefit of TODO-branches is that code in the middle of such changes is still compatible with the trunk. It all happens in the head of the developer, who remembers what changes should be done the next time a piece of code is rewritten.</p>
<p><small>Article Image &copy; Dominic Alves &mdash; <a href="http://www.flickr.com/photos/dominicspics/422131893/">Flickr</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/11/comment-branches/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The Three Components of Negotiation</title>
		<link>http://www.nicollet.net/2011/11/the-three-components-of-negotiation/</link>
		<comments>http://www.nicollet.net/2011/11/the-three-components-of-negotiation/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 13:33:46 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Strategy]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[Psychology]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2611</guid>
		<description><![CDATA[We negotiate several times a day, even if we do not recognize those occurences as such — « accept or walk away » is a fairly instinctive, if somewhat inefficient, negotiation strategy, and it goes hand in hand with the modern « take it or leave it » strategy employed by most mass consumer shops. [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2616" title="bazaar" src="http://www.nicollet.net/wp-content/uploads/2011/11/bazaar.png" alt="" width="675" height="100" /></p>
<p>We negotiate several times a day, even if we do not recognize those occurences as such — « accept or walk away » is a fairly instinctive, if somewhat inefficient, negotiation strategy, and it goes hand in hand with the modern « take it or leave it » strategy employed by most mass consumer shops.</p>
<p>What is negotiation anyway, and why does it happen?</p>
<p>It happens because when humans cooperate, they get a cake that is bigger than the sum of its parts, and they need to decide how to split it. For instance, if I grow potatoes and you breed cattle, we could decide to put together my delicious Désirée tubers and your tasty Charolais steaks, which is a big improvement over otherwise potato- or beef-only diets. And then, we would argue over how much of the final mix each of us would get:</p>
<blockquote><p>ME: We both contributed equally, so let&#8217;s get 50% each.<br />
YOU: Breeding cattle is harder than watching potatoes grow, so I want 65%.<br />
ME: Suit yourself, I know a guy who only asks for 55%.<br />
YOU: Fine, I&#8217;ll go as low as 60% because I like your blog.<br />
ME: I just told you I know a guy who&#8230;<br />
YOU: &#8230;and that would save you the effort of finding that guy before lunch.<br />
ME: Right. 60% it is, then.</p></blockquote>
<p>You were a good negotiator, so you managed to squeeze 10% out of this. What happened? Let&#8217;s break this down.</p>
<h4>Irrational Arguments</h4>
<p>« We both contributed equally » might be true. Maybe I spent as much time growing my potatoes than you spent working in your ranch. But that is not my argument. What I have written between the lines is that it is <em>fair</em> for equal efforts to receive equal rewards.</p>
<p>And <strong>fairness</strong> is not a rational argument. It is a subtle way to circumvent rational thinking and aim for our unconscious ethical programming and, by definition, make decisions that are not in our best interest. In the eyes of a good negotiator, this is a loophole to be exploited.</p>
<p>There are many other such loopholes in the human mind. My favorites are:</p>
<p><a href="http://en.wikipedia.org/wiki/Reciprocity_(social_psychology)" target="_blank"><strong>Reciprocity</strong></a> is an atavistic impulse to give back whenever we receive something, even if that something is mostly unrelated to our objectives. In the example above, going from 65% to 60% is cleverly framed as a gift instead of a simple acceptance of my terms, which makes me unwilling to further haggle over whether it should be 57.5%.</p>
<p><strong>Imprinting</strong> is our tendency, when we have no idea how much something is worth, to go with the first guess, clue or hint that we find about it. By suggesting a 50-50 trade, I imprinted you on the idea that potatoes are about as valuable than meat, and this serves as a basis for your « reasonable » 35-65 counter-proposal instead an extreme trade like, say, 3-97.</p>
<p>Guess what the market rate for potatoes and Charolais steak is? 3-97. I would like to correct my previous sentence:</p>
<blockquote><p>You were a <span style="text-decoration: line-through;">good</span><strong> bad </strong>negotiator, so <span style="text-decoration: line-through;">you</span><strong> I</strong> managed to squeeze <span style="text-decoration: line-through;">10%</span> <strong>37%</strong> out of this.</p></blockquote>
<p>And then, there&#8217;s a slew of <strong>emotional manipulation</strong> that does not appear at all in my example above. You can feign disappointment to try and get more out of a deal — &#8220;I really hoped you, of all people, could get this done for me.&#8221; You can pretend to be offended, shocked or angry in order to cause a large shift in the terms being negotiated — when you haggle in a bazaar, quote a price that is too low and you will be told that it is <em>insulting</em>, and kicked out of the store.</p>
<p>There are many other examples. Go read <a href="http://conversionxl.com/17-lesser-known-ways-to-persuade-people/" target="_blank">this blog article</a> and read <a href="http://www.amazon.com/gp/product/B002C949KE" target="_blank">Predictably Irrational</a> by Dan Ariely.</p>
<h4>Rational arguments</h4>
<p>You might expect this section to be as rich as the previous one, but it is not. There are only two different rational arguments you can say in a negotiation: &#8220;I will  not accept this outcome because [credible reason]&#8221; and &#8220;You will accept this outcome because [credible reason]&#8220;. Let&#8217;s examine our conversation with this convention:</p>
<blockquote><p>ME: <strong>Suggests 50-50</strong><br />
YOU: <strong>No, because</strong> [breeding cattle is harder than watching potatoes grow], <strong>suggests 35-65</strong><br />
ME: <strong>No, because</strong> [I know a guy who only asks for 55%]<br />
YOU: <strong>Suggests 40-60</strong><br />
ME: <strong>No, because</strong> [I know a guy who only asks for 55%]<br />
YOU: <strong>Yes, because</strong> [that would save you the effort of finding that guy before lunch]<br />
ME: <strong>Accepts 40-60</strong></p></blockquote>
<p>This is the reason why the most fundamental principle in negotiations is <strong>be willing to walk away</strong>. If you cannot or will not walk away, and the other guy knows this, then you can no longer say « No, because » and this cripples your ability to negotiate properly.</p>
<p>Typical reasons why I will not accept an outcome:</p>
<ul>
<li>The costs are greater than what I get out of it. Maybe I&#8217;ll ask for more money, or non-financial benefits (exclusive rights, free advertising&#8230;)</li>
<li>It&#8217;s too risky for me. Maybe I&#8217;ll ask for insurance, or add terms to our contract that provide me with acceptable protection.</li>
<li>I can get a better offer elsewhere. You can either match that offer, give me something I cannot get elsewhere, or give up.</li>
</ul>
<p>Typical reasons why you should accept an outcome:</p>
<ul>
<li>There are additional benefits you did not take into consideration, such as not spending the time to find a better offer elsewhere, or my goodwill in terms of future trades, or my reputation as your client/provider.</li>
<li>If you do not accept now but change your mind later, I will not be able the same offer to you again in the future.</li>
</ul>
<p>These reasons do not have to be <em>true</em>, they only have to <em>believable</em>. I don&#8217;t know anyone who would accept a 45-55 split on potatoes and Charolais steak, but you don&#8217;t know that, so I can still bluff my way through our negotiation, bringing you down from 65% (which is still a 32% win for me) to 60%. If you knew that no sane person would accept a 45-55 split, you could certainly call my bluff and even bring me back up to 10-90 or so.</p>
<p>How you present those arguments is a matter of style. Here are four different ways of presenting the same argument (you will not get more than $30k for an <a href="http://en.wikipedia.org/wiki/APL_(programming_language)" target="_blank">APL</a> job) :</p>
<ol>
<li>No one else in town hires APL developers like yourself, so you either accept this $30k job or get out.</li>
<li>I am certainly interested in your APL skills, but I would be losing money if I paid you more than $30k.</li>
<li>We are looking to fill our APL development position, but I am not at liberty to offer you more than $30k.</li>
<li>We have interviewed several candidates with your skills, and most of them are below your wage requirements.</li>
</ol>
<p>Some aggressive types would prefer approach 1, while manipulator types would go with 2 (which is a lie), passive types would hide behind a third party authority using 3, and sneaky types would try to get an even lower salary quote by withholding the $30k figure in approach 4.</p>
<h4>Alternative Benefits</h4>
<p>This is a subtle but essential part of many successful negotiations. <strong>It&#8217;s not only about the money</strong>. In the above example, I traded 5% for the ability to get my food in time for lunch — a concept that had not been mentioned at all before. You decided that instead of matching my requirements as I expressed them, you would offer me something that was unexpected and which, to you, cost less than 5% but still, to me, was worth at least 5%.</p>
<p>Going down alternate routes is an excellent way to get a stuck negotiation moving.</p>
<p><small>Article Image &copy; William Neuheisel &mdash; <a href="http://www.flickr.com/photos/wneuheisel/6167306708/">Flickr</a></small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/11/the-three-components-of-negotiation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Annealing Constraints</title>
		<link>http://www.nicollet.net/2011/10/annealing-constraints/</link>
		<comments>http://www.nicollet.net/2011/10/annealing-constraints/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 17:46:28 +0000</pubDate>
		<dc:creator>Victor Nicollet</dc:creator>
				<category><![CDATA[Functional]]></category>
		<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[RunOrg]]></category>

		<guid isPermaLink="false">http://www.nicollet.net/?p=2598</guid>
		<description><![CDATA[Of all the existing constraint-solving algorithms out there, my favourite is simulated annealing. To use it, you need a mutation function that randomly and slightly alters a solution, and a fitness function that lets you compare two solutions. Based on these, the algorithm is simple : Start with an arbitrary solution. Mutate the solution : [...]]]></description>
			<content:encoded><![CDATA[<p><img class="aligncenter size-full wp-image-2599" title="annealing" src="http://www.nicollet.net/wp-content/uploads/2011/10/annealing.png" alt="" width="675" height="100" />Of all the existing constraint-solving algorithms out there, my favourite is simulated annealing. To use it, you need a mutation function that randomly and slightly alters a solution, and a fitness function that lets you compare two solutions. Based on these, the algorithm is simple :</p>
<ol>
<li>Start with an arbitrary solution.</li>
<li>Mutate the solution : if the mutated solution is better than the current one in terms of fitness, keep it and repeat this step.</li>
<li>If you discard too many mutated solutions in step 2, remember the current solution and mutate it.</li>
<li>When time runs out, return the best solution found so far.</li>
</ol>
<p>Step 2 is a fairly standard ascent towards a local maximum : try a random similar solution and keep it if it improves fitness. Step 3 detects that the algorithm is stuck in a local maximum, so it shakes the solution out of there and starts again from that new position.</p>
<p>What is interesting about this solution is that it is exceedingly easy to implement, easy to adapt to a problem (as long as mutation and fitness are possible), and is reasonably efficient for most small problems.</p>
<p>I used it today to solve an original problem on RunOrg. We have a history of recently connected users, and we wanted to display a simple metric along the lines of « 10 users connected in the last hour » or « 11 users connected this week » as a short summary on the administrators&#8217; dashboard. The problem, of course, is to pick a reasonable duration based on the current data ! If one user connected seven days ago, and ten users connected a few minutes ago, then both « 10 users connected in the last hour » and « 11 users connected this week » are correct descriptions of the situation, but the former is intuitively better than the latter. <em>Intuitively</em>, we should use the shortest duration with the largest amount of users.</p>
<p>And intuition is a bad thing, because it is hard to turn into an algorithm.</p>
<p>In the end, I settled for a handful of periods of time : the last 5 minutes, the last 1 to 6 hours, today, the last 2 or 3 days, and this week. Each of these periods of time contains a certain number of user connections (which is fairly easy to determine based on the connection history) and receives a score computed by multiplying the number of user connections with a magic constant that is tied to that period. So, if the magic constant for « the last hour » is 0.76 and that for « this week » is 0.03, then 10 × 0.76 = 7.6 and 11 × 0.03 = 0.33 meaning that the « 10 users connected in the last hour » version will be preferred.</p>
<p>Then, I came up with several examples of connection histories, and manually picked the <em>intuitive</em> duration that should be chosen to display them. This turned into the <strong>fitness</strong> function : any set of magic numbers would have to agree with as many of those examples as possible, the more the better. <strong>Mutation</strong>, on the other hand, consisted in changing one of the magic constants at random.</p>
<p>Within 2000 iterations and a few seconds, the annealing algorithm came up with a solution that satisfied 94% of my constraints — and I am not even certain whether 100% could be satisfied, since I might have left a few contradictory intuitions in there. The final output is:</p>
<pre style="padding-left: 30px;">== Fitness: 94.44%
5min : 1
1h : 0.761091036722064
2h : 0.42295812451487896
3h : 0.26899204507851066
4h : 0.20923702855563558
5h : 0.14858146007043194
6h : 0.14846461447836096
today : 0.1477566411417444
2days : 0.07381054881381345
3days : 0.04202811963812667
week : 0.033268828300871064</pre>
<p>And so, these are the magic numbers that our algorithm uses. If it makes wrong decisions, we can always run the annealing again with new constraints, and feed the magic numbers back in.</p>
<p><small>Article image © Detlef Schobert — <a href="http://www.flickr.com/photos/detlefschobert/2505032252/">Flickr</a> </small></p>
]]></content:encoded>
			<wfw:commentRss>http://www.nicollet.net/2011/10/annealing-constraints/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

