<?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>brunoreis.com/tech</title>
	<atom:link href="http://brunoreis.com/tech/feed/" rel="self" type="application/rss+xml" />
	<link>http://brunoreis.com/tech</link>
	<description>Maths, code, technology...</description>
	<lastBuildDate>Fri, 26 Aug 2011 19:15:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Intalling Java 7 on Ubuntu Natty (11.04)</title>
		<link>http://brunoreis.com/tech/intalling-java-ubuntu-natty/</link>
		<comments>http://brunoreis.com/tech/intalling-java-ubuntu-natty/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 18:45:29 +0000</pubDate>
		<dc:creator>Bruno França dos Reis</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://brunoreis.com/tech/?p=353</guid>
		<description><![CDATA[It&#8217;s been almost one month since the official release date (July 28th) of Java 7 and it is still not available in Ubuntu&#8217;s software repository for Natty (11.04). There&#8217;s one for Oneiric (11.10), sure, but cannot be installed into Natty. I&#8217;ve seen some guides proposing how to install Java 7 on Natty, but all of [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s been almost one month since the official release date (July 28th) of Java 7 and it is still not available in Ubuntu&#8217;s software repository for Natty (11.04). There&#8217;s one for Oneiric (11.10), sure, but cannot be installed into Natty.</p>

<p>I&#8217;ve seen some guides proposing how to install Java 7 on Natty, but all of them either do something wrong, or fail to explain what they are doing.</p>

<p>Here, I propose a way to install Java 7 (both OpenJDK and Sun) on Natty that won&#8217;t make a mess on your system.</p>

<h3>Java 7, OpenJDK</h3>

<p>Last week Damien Lecan created a PPA with a backport of Oneiric&#8217;s the .deb package and announced it in <a href="http://www.dlecan.com/index.php?url=archives/69-Installing-OpenJDK-7-on-Ubuntu-Natty-PPA-method.html">his blog</a>. To install the JRE</p>

<pre class="prettyprint"><code>$ sudo add-apt-repository ppa:dlecan/openjdk
$ sudo apt-get update
$ sudo apt-get install openjdk-7-jre
</code></pre>

<p>To install the JDK, replace the last line with <code>sudo apt-get install openjdk-7-jdk</code>.</p>

<h3>Java 7 , Sun/Oracle</h3>

<p>Some of you might prefer Sun/Oracle&#8217;s JVM. Unfortunately, I&#8217;ve not found any PPA distributing it, therefore it has to be installed manually.</p>

<p>First of all, <strong>download the JDK</strong> from <a href="http://www.oracle.com/technetwork/java/javase/downloads/java-se-jdk-7-download-432154.html">Oracle&#8217;s download page</a> to your home directory. You want <code>jdk-7-linux-i586.tar.gz</code> for 32-bit systems or <code>jdk-7-linux-x64.tar.gz</code> for 64-bit systems.</p>

<p>Then <strong>untar it</strong> with <code>$ tar xf jdk-7-linux-x64.tar.gz</code>.</p>

<p>The next step is to <strong>move the content of the tarball to the same directory where Ubuntu installs the JVMs</strong>, which is /usr/lib/jvm. You need sudo, because the destination directory is owned by root and has permissions 755: <code>sudo mv ~/jdk1.7.0 /usr/lib/jvm/</code>.</p>

<p>Now <strong>create a directory symlink</strong> called java-7-sun, pointing to jdk1.7.0, to follow Ubuntu&#8217;s conventions: <code>sudo ln -s /usr/lib/jvm/jdk1.7.0 /usr/lib/jvm/java-7-sun</code></p>

<p>We&#8217;re almost there. Now you must <strong>configure Ubuntu&#8217;s <em>alternative system</em></strong>, by telling it about the new version you just downloaded. This is done by issuing a bunch of <code>update-alternatives</code> commands, telling the system the location of every new file.</p>

<p>The alternative system works by creating symlinks (mostly) in /usr/bin to /etc/alternatives, and then updating the links in /etc/alternatives to point to the actual executable. For example, in my system, <code>/usr/bin/java</code> points to <code>/etc/alternatives/java</code>, which in turn points to <code>/usr/lib/jvm/java-7-sun/jre/bin/java</code>. By issuing the command <code>sudo update-alternatives --config java</code>, Ubuntu prompts the user to select another alternative for <code>/usr/bin/java</code>, and simply updates the symlink <code>/etc/alternatives/java</code> to point to the newly selected provider.</p>

<p>I&#8217;ve studied the installation of Sun&#8217;s Java 6 (packages sun-java6-bin, sun-java6-jdk and sun-java6-plugin), specifically the <code>postinst</code> scripts from each, merged and adapted the scripts, and produced a new one that would simply print into the screen the alternatives that have to be configured. Save the following content to a file (say, <code>~/java7config</code>), then run it as root (<code>sudo bash ~/java7config</code>):</p>

<p>(<strong>Note</strong>: if you downloaded the 32-bit version, you should replace <code>amd64</code> with <code>i386</code>! Thanks, satrapes!)</p>

<pre class="prettyprint"><code>update-alternatives --quiet --install /usr/lib/xulrunner-addons/plugins/libjavaplugin.so xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-7-sun/jre/lib/amd64/libnpjp2.so 63
update-alternatives --quiet --install /usr/lib/mozilla/plugins/libjavaplugin.so mozilla-javaplugin.so /usr/lib/jvm/java-7-sun/jre/lib/amd64/libnpjp2.so 63
update-alternatives --quiet --install /usr/bin/appletviewer appletviewer /usr/lib/jvm/java-7-sun/bin/appletviewer 63 --slave /usr/share/man/man1/appletviewer.1 appletviewer.1 /usr/lib/jvm/java-7-sun/man/man1/appletviewer.1
update-alternatives --quiet --install /usr/bin/apt apt /usr/lib/jvm/java-7-sun/bin/apt 63 --slave /usr/share/man/man1/apt.1 apt.1 /usr/lib/jvm/java-7-sun/man/man1/apt.1
update-alternatives --quiet --install /usr/bin/extcheck extcheck /usr/lib/jvm/java-7-sun/bin/extcheck 63 --slave /usr/share/man/man1/extcheck.1 extcheck.1 /usr/lib/jvm/java-7-sun/man/man1/extcheck.1
update-alternatives --quiet --install /usr/bin/idlj idlj /usr/lib/jvm/java-7-sun/bin/idlj 63 --slave /usr/share/man/man1/idlj.1 idlj.1 /usr/lib/jvm/java-7-sun/man/man1/idlj.1
update-alternatives --quiet --install /usr/bin/jar jar /usr/lib/jvm/java-7-sun/bin/jar 63 --slave /usr/share/man/man1/jar.1 jar.1 /usr/lib/jvm/java-7-sun/man/man1/jar.1
update-alternatives --quiet --install /usr/bin/jarsigner jarsigner /usr/lib/jvm/java-7-sun/bin/jarsigner 63 --slave /usr/share/man/man1/jarsigner.1 jarsigner.1 /usr/lib/jvm/java-7-sun/man/man1/jarsigner.1
update-alternatives --quiet --install /usr/bin/javac javac /usr/lib/jvm/java-7-sun/bin/javac 63 --slave /usr/share/man/man1/javac.1 javac.1 /usr/lib/jvm/java-7-sun/man/man1/javac.1
update-alternatives --quiet --install /usr/bin/javadoc javadoc /usr/lib/jvm/java-7-sun/bin/javadoc 63 --slave /usr/share/man/man1/javadoc.1 javadoc.1 /usr/lib/jvm/java-7-sun/man/man1/javadoc.1
update-alternatives --quiet --install /usr/bin/javah javah /usr/lib/jvm/java-7-sun/bin/javah 63 --slave /usr/share/man/man1/javah.1 javah.1 /usr/lib/jvm/java-7-sun/man/man1/javah.1
update-alternatives --quiet --install /usr/bin/javap javap /usr/lib/jvm/java-7-sun/bin/javap 63 --slave /usr/share/man/man1/javap.1 javap.1 /usr/lib/jvm/java-7-sun/man/man1/javap.1
update-alternatives --quiet --install /usr/bin/jconsole jconsole /usr/lib/jvm/java-7-sun/bin/jconsole 63 --slave /usr/share/man/man1/jconsole.1 jconsole.1 /usr/lib/jvm/java-7-sun/man/man1/jconsole.1
update-alternatives --quiet --install /usr/bin/jdb jdb /usr/lib/jvm/java-7-sun/bin/jdb 63 --slave /usr/share/man/man1/jdb.1 jdb.1 /usr/lib/jvm/java-7-sun/man/man1/jdb.1
update-alternatives --quiet --install /usr/bin/jhat jhat /usr/lib/jvm/java-7-sun/bin/jhat 63 --slave /usr/share/man/man1/jhat.1 jhat.1 /usr/lib/jvm/java-7-sun/man/man1/jhat.1
update-alternatives --quiet --install /usr/bin/jinfo jinfo /usr/lib/jvm/java-7-sun/bin/jinfo 63 --slave /usr/share/man/man1/jinfo.1 jinfo.1 /usr/lib/jvm/java-7-sun/man/man1/jinfo.1
update-alternatives --quiet --install /usr/bin/jmap jmap /usr/lib/jvm/java-7-sun/bin/jmap 63 --slave /usr/share/man/man1/jmap.1 jmap.1 /usr/lib/jvm/java-7-sun/man/man1/jmap.1
update-alternatives --quiet --install /usr/bin/jps jps /usr/lib/jvm/java-7-sun/bin/jps 63 --slave /usr/share/man/man1/jps.1 jps.1 /usr/lib/jvm/java-7-sun/man/man1/jps.1
update-alternatives --quiet --install /usr/bin/jrunscript jrunscript /usr/lib/jvm/java-7-sun/bin/jrunscript 63 --slave /usr/share/man/man1/jrunscript.1 jrunscript.1 /usr/lib/jvm/java-7-sun/man/man1/jrunscript.1
update-alternatives --quiet --install /usr/bin/jsadebugd jsadebugd /usr/lib/jvm/java-7-sun/bin/jsadebugd 63 --slave /usr/share/man/man1/jsadebugd.1 jsadebugd.1 /usr/lib/jvm/java-7-sun/man/man1/jsadebugd.1
update-alternatives --quiet --install /usr/bin/jstack jstack /usr/lib/jvm/java-7-sun/bin/jstack 63 --slave /usr/share/man/man1/jstack.1 jstack.1 /usr/lib/jvm/java-7-sun/man/man1/jstack.1
update-alternatives --quiet --install /usr/bin/jstat jstat /usr/lib/jvm/java-7-sun/bin/jstat 63 --slave /usr/share/man/man1/jstat.1 jstat.1 /usr/lib/jvm/java-7-sun/man/man1/jstat.1
update-alternatives --quiet --install /usr/bin/jstatd jstatd /usr/lib/jvm/java-7-sun/bin/jstatd 63 --slave /usr/share/man/man1/jstatd.1 jstatd.1 /usr/lib/jvm/java-7-sun/man/man1/jstatd.1
update-alternatives --quiet --install /usr/bin/native2ascii native2ascii /usr/lib/jvm/java-7-sun/bin/native2ascii 63 --slave /usr/share/man/man1/native2ascii.1 native2ascii.1 /usr/lib/jvm/java-7-sun/man/man1/native2ascii.1
update-alternatives --quiet --install /usr/bin/rmic rmic /usr/lib/jvm/java-7-sun/bin/rmic 63 --slave /usr/share/man/man1/rmic.1 rmic.1 /usr/lib/jvm/java-7-sun/man/man1/rmic.1
update-alternatives --quiet --install /usr/bin/schemagen schemagen /usr/lib/jvm/java-7-sun/bin/schemagen 63 --slave /usr/share/man/man1/schemagen.1 schemagen.1 /usr/lib/jvm/java-7-sun/man/man1/schemagen.1
update-alternatives --quiet --install /usr/bin/serialver serialver /usr/lib/jvm/java-7-sun/bin/serialver 63 --slave /usr/share/man/man1/serialver.1 serialver.1 /usr/lib/jvm/java-7-sun/man/man1/serialver.1
update-alternatives --quiet --install /usr/bin/wsgen wsgen /usr/lib/jvm/java-7-sun/bin/wsgen 63 --slave /usr/share/man/man1/wsgen.1 wsgen.1 /usr/lib/jvm/java-7-sun/man/man1/wsgen.1
update-alternatives --quiet --install /usr/bin/wsimport wsimport /usr/lib/jvm/java-7-sun/bin/wsimport 63 --slave /usr/share/man/man1/wsimport.1 wsimport.1 /usr/lib/jvm/java-7-sun/man/man1/wsimport.1
update-alternatives --quiet --install /usr/bin/xjc xjc /usr/lib/jvm/java-7-sun/bin/xjc 63 --slave /usr/share/man/man1/xjc.1 xjc.1 /usr/lib/jvm/java-7-sun/man/man1/xjc.1
update-alternatives --quiet --install /usr/bin/java-rmi.cgi java-rmi.cgi /usr/lib/jvm/java-7-sun/bin/java-rmi.cgi 63
update-alternatives --quiet --install /usr/bin/ControlPanel ControlPanel /usr/lib/jvm/java-7-sun/jre/bin/ControlPanel 63
update-alternatives --quiet --install /usr/bin/java java /usr/lib/jvm/java-7-sun/jre/bin/java 63
update-alternatives --quiet --install /usr/bin/java_vm java_vm /usr/lib/jvm/java-7-sun/jre/bin/java_vm 63
update-alternatives --quiet --install /usr/bin/javaws javaws /usr/lib/jvm/java-7-sun/jre/bin/javaws 63
update-alternatives --quiet --install /usr/bin/jcontrol jcontrol /usr/lib/jvm/java-7-sun/jre/bin/jcontrol 63
update-alternatives --quiet --install /usr/bin/keytool keytool /usr/lib/jvm/java-7-sun/jre/bin/keytool 63
update-alternatives --quiet --install /usr/bin/pack200 pack200 /usr/lib/jvm/java-7-sun/jre/bin/pack200 63
update-alternatives --quiet --install /usr/bin/policytool policytool /usr/lib/jvm/java-7-sun/jre/bin/policytool 63
update-alternatives --quiet --install /usr/bin/rmid rmid /usr/lib/jvm/java-7-sun/jre/bin/rmid 63
update-alternatives --quiet --install /usr/bin/rmiregistry rmiregistry /usr/lib/jvm/java-7-sun/jre/bin/rmiregistry 63
update-alternatives --quiet --install /usr/bin/unpack200 unpack200 /usr/lib/jvm/java-7-sun/jre/bin/unpack200 63
update-alternatives --quiet --install /usr/bin/orbd orbd /usr/lib/jvm/java-7-sun/jre/bin/orbd 63
update-alternatives --quiet --install /usr/bin/servertool servertool /usr/lib/jvm/java-7-sun/jre/bin/servertool 63
update-alternatives --quiet --install /usr/bin/tnameserv tnameserv /usr/lib/jvm/java-7-sun/jre/bin/tnameserv 63
update-alternatives --quiet --install /usr/bin/jexec jexec /usr/lib/jvm/java-7-sun/jre/lib/jexec 63
</code></pre>

<p>To conclude the installation, you must <strong>create (as root) the file <code>/usr/lib/jvm/.java-7-sun.jinfo</code></strong> and <strong>add the following content</strong>:</p>

<p>(<strong>Note</strong>: if you downloaded the 32-bit version, you should replace <code>amd64</code> with <code>i386</code>! Thanks, satrapes!)</p>

<pre class="prettyprint"><code>name=java-7-sun
alias=java-7-sun
priority=63
section=non-free

jre ControlPanel /usr/lib/jvm/java-7-sun/jre/bin/ControlPanel
jre java /usr/lib/jvm/java-7-sun/jre/bin/java
jre java_vm /usr/lib/jvm/java-7-sun/jre/bin/java_vm
jre javaws /usr/lib/jvm/java-7-sun/jre/bin/javaws
jre jcontrol /usr/lib/jvm/java-7-sun/jre/bin/jcontrol
jre keytool /usr/lib/jvm/java-7-sun/jre/bin/keytool
jre pack200 /usr/lib/jvm/java-7-sun/jre/bin/pack200
jre policytool /usr/lib/jvm/java-7-sun/jre/bin/policytool
jre rmid /usr/lib/jvm/java-7-sun/jre/bin/rmid
jre rmiregistry /usr/lib/jvm/java-7-sun/jre/bin/rmiregistry
jre unpack200 /usr/lib/jvm/java-7-sun/jre/bin/unpack200
jre orbd /usr/lib/jvm/java-7-sun/jre/bin/orbd
jre servertool /usr/lib/jvm/java-7-sun/jre/bin/servertool
jre tnameserv /usr/lib/jvm/java-7-sun/jre/bin/tnameserv
jre jexec /usr/lib/jvm/java-7-sun/jre/lib/jexec
jdk appletviewer /usr/lib/jvm/java-7-sun/bin/appletviewer
jdk apt /usr/lib/jvm/java-7-sun/bin/apt
jdk extcheck /usr/lib/jvm/java-7-sun/bin/extcheck
jdk idlj /usr/lib/jvm/java-7-sun/bin/idlj
jdk jar /usr/lib/jvm/java-7-sun/bin/jar
jdk jarsigner /usr/lib/jvm/java-7-sun/bin/jarsigner
jdk java-rmi.cgi /usr/lib/jvm/java-7-sun/bin/java-rmi.cgi
jdk javac /usr/lib/jvm/java-7-sun/bin/javac
jdk javadoc /usr/lib/jvm/java-7-sun/bin/javadoc
jdk javah /usr/lib/jvm/java-7-sun/bin/javah
jdk javap /usr/lib/jvm/java-7-sun/bin/javap
jdk jconsole /usr/lib/jvm/java-7-sun/bin/jconsole
jdk jdb /usr/lib/jvm/java-7-sun/bin/jdb
jdk jhat /usr/lib/jvm/java-7-sun/bin/jhat
jdk jinfo /usr/lib/jvm/java-7-sun/bin/jinfo
jdk jmap /usr/lib/jvm/java-7-sun/bin/jmap
jdk jps /usr/lib/jvm/java-7-sun/bin/jps
jdk jrunscript /usr/lib/jvm/java-7-sun/bin/jrunscript
jdk jsadebugd /usr/lib/jvm/java-7-sun/bin/jsadebugd
jdk jstack /usr/lib/jvm/java-7-sun/bin/jstack
jdk jstat /usr/lib/jvm/java-7-sun/bin/jstat
jdk jstatd /usr/lib/jvm/java-7-sun/bin/jstatd
jdk native2ascii /usr/lib/jvm/java-7-sun/bin/native2ascii
jdk rmic /usr/lib/jvm/java-7-sun/bin/rmic
jdk schemagen /usr/lib/jvm/java-7-sun/bin/schemagen
jdk serialver /usr/lib/jvm/java-7-sun/bin/serialver
jdk wsgen /usr/lib/jvm/java-7-sun/bin/wsgen
jdk wsimport /usr/lib/jvm/java-7-sun/bin/wsimport
jdk xjc /usr/lib/jvm/java-7-sun/bin/xjc
plugin xulrunner-1.9-javaplugin.so /usr/lib/jvm/java-7-sun/jre/lib/amd64/libnpjp2.so
plugin mozilla-javaplugin.so /usr/lib/jvm/java-7-sun/jre/lib/amd64/libnpjp2.so
</code></pre>

<p>This file is read by <code>update-java-alternatives</code>, which in turn will call <code>update-alternatives --config</code> for each one of the executables listed above. So, to finish, <strong>update your symlinks</strong>:</p>

<pre class="prettyprint"><code>sudo update-java-alternatives --set java-7-sun
</code></pre>

<p>Remember that some applications might read the environment variables <code>JAVA_HOME</code> or <code>JDK_HOME</code>, which you must update by yourself (by updating <code>~/.bash_rc</code>, or <code>~/.bash_profile</code>, or <code>~/.profile</code>, or <code>~/.pam_environment</code>, among many other alternatives).</p>

<p>Should you want to use another installed JVM, you can list the possibilities with:</p>

<pre class="prettyprint"><code>sudo update-java-alternatives --list
</code></pre>

<p>That&#8217;s all. The above procedure <em><a href="http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html">works on my machine</a></em>. If you have any problems with it, don&#8217;t panic, your system will probably not get broken. Leave a comment and we can try to fix it!</p>

<p><strong>Note</strong>: if you have no <code>update-java-alternatives</code>, you can install it with <code>sudo apt-get install java-common</code>. Thanks, Leandro!</p>

<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://brunoreis.com/tech/intalling-java-ubuntu-natty/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Fluent NHibernate &amp; HasOne(): how to implement a one-to-one relationship</title>
		<link>http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/</link>
		<comments>http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 20:20:00 +0000</pubDate>
		<dc:creator>Bruno França dos Reis</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[DDD]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://brunoreis.com/tech/?p=307</guid>
		<description><![CDATA[For a (very) long time I&#8217;ve been googling, binging, stackoverflowing, [your-search-engine]ing for a simple yet complete example showing how to implement a one-to-one relationship using Fluent NHibernate. Fluent NHibernate and lambda expressions as data If you don&#8217;t already know, Fluent NHibernate is a fantastic open-source library to be used in conjunction with NHibernate, a powerful [...]]]></description>
			<content:encoded><![CDATA[<p>For a (very) long time I&#8217;ve been googling, binging, stackoverflowing, [your-search-engine]ing for a simple yet complete example showing how to implement a one-to-one relationship using <strong><a href="http://fluentnhibernate.org/">Fluent NHibernate</a></strong>.</p>

<blockquote>
  <h4>Fluent NHibernate and lambda expressions as data</h4>
  
  <p>If you don&#8217;t already know, <em>Fluent NHibernate</em> is a fantastic open-source library to be used in conjunction with <a href="https://www.hibernate.org/343.html">NHibernate</a>, a powerful <abbr title="Object-Relational Mapping, a tool that tries to reduce the impedance mismatch between the Object Oriented model used in software and the Relational model used in relational databases. Usually such a tool will automatically generate one model based on the other, automatically load data and fill in the objects from the database, so that the developer does not need to care about that boilerplate code.">O/RM</abbr> library for .NET. NHibernate works by reading a (usually) manually created XML file that represents the mapping between the Object Oriented and Relational models. There are obvious annoying problems related to the fact of using this XML file, such as not being <em>refactoring-friendly</em>.</p>
  
  <p>Fluent NHibernate is based on the power and expressiviness of <em><a href="http://msdn.microsoft.com/en-us/library/bb397687.aspx">lambda expressions</a></em> to allow for a concise, strongly-type, refactoring-friendly mappings, that eliminates the need of hand-crafted XML files.</p>
  
  <p>It is a very nice example of a somewhat modern pattern, borrowed from functional programming, now possible in C#: <strong>using lambda expressions as data</strong>. For more details about such emerging patterns, I strongly recommend you <a href="http://msdn.microsoft.com/en-us/magazine/ee309512.aspx" title="Functional Programming for Everyday .NET Development, by Jeremy Miller">this excelent article</a> on MSDN Magazine.</p>
</blockquote>

<p>The problem is that I&#8217;ve not yet found an example that fits my needs, and Fluent NHibernate&#8217;s documentation is somewhat scarce (a big drawback of Fluent NHibernate, I&#8217;d say, specially when using it inside a company). What you will most certainly find is people telling you that <em>you might actually need a <strong>many-to-one</strong> relationship</em>. No, I was needing a one-to-one relationship, I swear, and couldn&#8217;t find a single example on how to correctly implement it with NHibernate and Fluent NHibernate. Note that I have almost no experience with NHibernate without Fluent NHibernate. I&#8217;ve never created an XML mapping that was more complex than <em>almost-trivial</em>. Had I had some more experience with it, I might had been able to solve this problem more easily.</p>

<p>So I present here a scenario, similar to the one I was facing, involving a <em>one-to-one</em> relationship and how I managed to implement it with Fluent NHibernate.</p>

<h2>The scenario</h2>

<p>Let&#8217;s say you have a project that deals with <em>Clients</em>. You normally would have a class to represent this entity, such as:</p>

<p><img src="/tech/images/fnhib121/model_clients.png" alt="Model" /></p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">public</span> <span class="kw4">class</span> Client <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">protected</span> <span class="kw1">virtual</span> <span class="kw4">int</span> Id <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">string</span> Name <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span></code></pre>

<p>(note that the <em>protected</em> id and the <em>virtual</em> modifiers are there to please NHibernate; it creates a proxy class around your model class, so it needs to be able to override getters and setters)</p>

<p>Using Fluent NHibernate, you need no XML file to manage the mappings, all you need is this:</p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">using</span> FluentNHibernate.<span class="me1">Mapping</span>;<br />
<span class="kw1">public</span> <span class="kw4">class</span> ClientMap <span class="sy0">:</span> ClassMap<span class="sy0">&lt;</span>Client<span class="sy0">&gt;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; Id<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">Id</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Map<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">Name</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span></code></pre>

<p>And that&#8217;s it! Should you need to refactor <em>Name</em> and call it, say, <em>FullName</em>, the mapping would be automatically updated accordingly (given that you use any minimally-competent refactoring tool).</p>

<p>Here is the corresponding database (the names are slightly modified, not following the standard Fluent NHibernate convention):</p>

<p><img src="/tech/images/fnhib121/db_clients.jpg" alt="Database" /></p>

<h3>Adding some relationships</h3>

<p>With NHibernate you are free to model your domain in the object-oriented world, with all the kinds of relationships you might want, and it will take care of the database for you.</p>

<p>One such relationship is a <strong>many-to-one</strong> relationship. Let&#8217;s say you want to possibly classify your clients according to some <em>Profile</em>s. A client may have one profile, or no profile at all (if you have not yet decided what is his profile). You would have something like this:</p>

<p><img src="/tech/images/fnhib121/model_profiles.png" alt="Model" /></p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">public</span> <span class="kw4">class</span> Client <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> Profile Profile <span class="br0">&#123;</span> get; <span class="kw1">private</span> set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> Profile <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">protected</span> <span class="kw1">virtual</span> <span class="kw4">int</span> Id <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">string</span> ProfileName <span class="br0">&#123;</span> get; <span class="kw1">private</span> set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span></code></pre>

<p>The mappings would be:</p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">public</span> <span class="kw4">class</span> ProfileMap <span class="sy0">:</span> ClassMap<span class="sy0">&lt;</span>Profile<span class="sy0">&gt;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; Id<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">Id</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Map<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">ProfileName</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> ClientMap <span class="sy0">:</span> ClassMap<span class="sy0">&lt;</span>Client<span class="sy0">&gt;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
&nbsp; &nbsp; References<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">Profile</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span></code></pre>

<p>And this is the database, as expected:</p>

<p><img src="/tech/images/fnhib121/db_profiles.jpg" alt="Database" /></p>

<p>And that&#8217;s all. Should you want, Fluent NHibernate can automatically generate the database for you as well, with one extra line of code on the configuration of the connection to the database.</p>

<p>Simple, huh?</p>

<h2>The problem &#8211; a one-to-one relationship</h2>

<p>Now suppose you have your system running, in production. Everything is fine, not too many bugs (and the ones found not <em>too</em> scary), the business (that partly relies on your software) is running OK.</p>

<p>One day, your boss calls you and (as you can expect) tells you he needs a <em>new feature</em>. Let&#8217;s say he needs to gather some more details about clients, about their alimentary habits.</p>

<p>You, as the developer, decide that you shall not modify the whole <code>Client</code> class, adding many alimentary properties to it. You&#8217;d rather create a new class <code>AlimentaryHabits</code>. However this data is still <em>part</em> of the client, you just decide to separate the classes not to burden the client.</p>

<p>The same argument applies to the database: you want to create a separate table not to burden the <em>Clients</em> table. Should you want to have two classes but only one table, you can create your mappings using <code>Component()</code>, but I won&#8217;t discuss this here since it is already well documented elsewhere.</p>

<p>What we want in the database is two tables, a <em>Clients</em> with its primary key <strong>Id</strong>, and a <em>AlimentaryHabits</em> table, with a primary key <strong>ClientId</strong> that is also a foreign key, referring to <em>Client</em>&#8216;s primary key:</p>

<p><img src="/tech/images/fnhib121/db_alimentary_habits.jpg" alt="Database" /></p>

<p>What you would like to have is a model that looks like this:</p>

<p><img src="/tech/images/fnhib121/model_alimentary_habits_1.png" alt="Model" /></p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">public</span> <span class="kw4">class</span> Client <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> AlimentaryHabits AlimentaryHabits<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span> get; <span class="kw1">private</span> set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> AlimentaryHabits <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">bool</span> LikesPasta <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">bool</span> LikesPizza <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">int</span> AverageDailyCalories <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></code></pre>

<p>This is what we <em>would like to have</em>. But it is not that easy. No. To please NHibernate and allow Fluent NHibernate to correctly create our mappings, we need to change things a bit. And <strong>this is my problem, I couldn&#8217;t find any example on how to implement this mapping</strong>.</p>

<h2>The solution</h2>

<p>To implement this model and the corresponding fluent mapping, we need to modify things a bit. This is the domain:</p>

<p><img src="/tech/images/fnhib121/model_alimentary_habits_2.png" alt="Model" /></p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">public</span> <span class="kw4">class</span> AlimentaryHabits <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> <span class="kw4">int</span> ClientId <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">private</span> Client Client <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; <span class="kw1">protected</span> AlimentaryHabits<span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; <span class="kw1">public</span> AlimentaryHabits<span class="br0">&#40;</span>Client client<span class="br0">&#41;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Client <span class="sy0">=</span> client;<br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">bool</span> LikesPasta <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">bool</span> LikesPizza <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">int</span> AverageDailyCalories <span class="br0">&#123;</span> get; set; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></code></pre>

<p>We need two things to satisfy our OR/M solution:</p>

<ul>
<li>an <strong>id</strong> of the same type as the <em>main</em> class; and</li>
<li>a <strong>reference</strong> to the <em>main</em> object;</li>
</ul>

<p>This <strong>reference</strong> would represent a <em>bidirectional relationship</em>, although we wanted a <em>unidirectional</em> one (you could want something different for your domain). However we could make it <strong>private</strong>, and <strong>it is not even used inside the class</strong>, it&#8217;s just used by NHibernate. Also, note that now we have two constructors. The <strong>public constructor</strong>, taking a <code>Client</code> parameter, is the one you will use in your code whenever you want to assign a client some alimentary habits, such as: <code>AlimentaryHabits = new AlimentaryHabits(this);</code>. The <strong>protected constructor</strong> is used internally by NHibernate, and must be present. You can completely ignore it.</p>

<p>This is the corresponding mapping:</p>

<pre class="prettyprint lang-csharp"><code><span class="kw1">public</span> <span class="kw4">class</span> AlimentaryHabitsMap <span class="sy0">:</span> ClassMap<span class="sy0">&lt;</span>AlimentaryHabits<span class="sy0">&gt;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; Id<span class="br0">&#40;</span>Reveal.<span class="me1">Property</span><span class="sy0">&lt;</span>AlimentaryHabits<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="st0">&quot;ClientId&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">GeneratedBy</span>.<span class="me1">Foreign</span><span class="br0">&#40;</span><span class="st0">&quot;Client&quot;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; HasOne<span class="br0">&#40;</span><br />
&nbsp; &nbsp; &nbsp; Reveal.<span class="me1">Property</span><span class="sy0">&lt;</span>AlimentaryHabits, Client<span class="sy0">&gt;</span><span class="br0">&#40;</span><span class="st0">&quot;Client&quot;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">Constrained</span><span class="br0">&#40;</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">ForeignKey</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<br />
&nbsp; &nbsp; Map<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">LikesPasta</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Map<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">LikesPizza</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; Map<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">AverageDailyCalories</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> ClientMap <span class="sy0">:</span> ClassMap<span class="sy0">&lt;</span>Client<span class="sy0">&gt;</span> <span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
&nbsp; &nbsp; HasOne<span class="br0">&#40;</span>x <span class="sy0">=&gt;</span> x.<span class="me1">AlimentaryHabits</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; .<span class="me1">Cascade</span>.<span class="me1">All</span><span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="coMULTI">/* ... */</span><br />
<span class="br0">&#125;</span></code></pre>

<p>(the <code>Reveal.Property</code> thing is because both <code>ClientId</code> and <code>Client</code> properties are <code>private</code>, and thus not accessible from the mapping class; they will be accessed through reflection by NHibernate)</p>

<p>This model setup, along with this mapping, will create <em>almost</em> the model we initially wanted to, and the exact database representation we wanted!</p>

<h2>Conclusion</h2>

<p>This involved more effort than I initially thought it would, but now I have a neat solution. And the next time I will need this, it will be easy to implement, following this directions.</p>

<p>If you know a way to simplify this further, or if you like it, or if you have any questions, leave a comment, I&#8217;d be glad to further improve this article!</p>

<p>Good luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://brunoreis.com/tech/fluent-nhibernate-hasone-how-implement-one-to-one-relationship/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>The nth permutation of a sequence of symbols in F#</title>
		<link>http://brunoreis.com/tech/nth-permutation-sequence-symbols/</link>
		<comments>http://brunoreis.com/tech/nth-permutation-sequence-symbols/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 06:45:21 +0000</pubDate>
		<dc:creator>Bruno França dos Reis</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://brunoreis.com/tech/?p=133</guid>
		<description><![CDATA[Project Euler&#8216;s problem 24 deals with permutation of symbols. More specifically, given the initial sequence of symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), it asks its 1.000.000th lexicographic permutation. The nth lexicographic permutation of a sequence of symbols is defined as the nth element of the lexicographically ordered set of permutations [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net">Project Euler</a>&#8216;s <a href="http://projecteuler.net/index.php?section=problems&amp;id=24">problem 24</a> deals with permutation of symbols.</p>

<p>More specifically, given the initial sequence of symbols (0, 1, 2, 3, 4, 5, 6, 7, 8, 9), it asks its 1.000.000th lexicographic permutation.</p>

<p><strong>The nth lexicographic permutation of a sequence of symbols is defined as the nth element of the lexicographically ordered set of permutations of that symbols</strong>.</p>

<p>To help clarify this definition, take for example the symbols (a, b, c). The lexicographically ordered set of permutations is computed by determining all the permutations, and then sorting them according to the lexicographical order (a.k.a. alphabetical order):</p>

<ol>
<li>abc</li>
<li>acb</li>
<li>bac</li>
<li>bca</li>
<li>cab</li>
<li>cba</li>
</ol>

<p>So, in this case, the 4th lexicograph permutation of (a, b, c) is &#8220;bca&#8221;.</p>

<p>A solution to the initial problem is then rather simple to construct in very naive way: list all the (10! = 3628800) permutations, sort them all, and take the 1.000.000th. However this may take a lot of memory and time. And most importantly, for any given sequence of symbols, <strong>it can be done in O(1)</strong>.</p>

<p>The constant-complexity algorithm is rather simple and can be easily implemented in F# as follows:</p>

<pre class="prettyprint lang-fsharp"><code><span class="co1">// You can use your own factorial</span><br />
<span class="co1">// factorial : BigInteger -&gt; BigInteger</span><br />
<span class="kw1">let</span> factorial n <span class="sy0">=</span> <span class="kw2">Seq</span>.<span class="me1">fold</span> <span class="br0">&#40;</span> <span class="sy0">*</span> <span class="br0">&#41;</span> 1I <span class="br0">&#91;</span> 1I .. <span class="me1">n</span> <span class="br0">&#93;</span><br />
<br />
<span class="kw1">let</span> nthPermutation n <span class="br0">&#40;</span>symbols:<span class="co2">#seq&lt;'S&gt;) =</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> symbols <span class="sy0">=</span> <span class="kw1">new</span> ResizeArray<span class="sy0">&lt;</span>'S<span class="sy0">&gt;</span><span class="br0">&#40;</span>symbols<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> <span class="kw1">rec</span> aux n acc <span class="sy0">=</span> <span class="kw1">function</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; | sz <span class="kw1">when</span> sz <span class="sy0">=</span> 0I <span class="sy0">-&gt;</span> acc <span class="sy0">|&gt;</span> <span class="kw2">List</span>.<span class="me1">rev</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; | sz <span class="sy0">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> sz' <span class="sy0">=</span> sz <span class="sy0">-</span> 1I<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> k <span class="sy0">=</span> factorial sz'<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> i <span class="sy0">=</span> n <span class="sy0">/</span> k<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> s <span class="sy0">=</span> symbols.<span class="br0">&#91;</span><span class="kw4">int</span> i<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; symbols.<span class="me1">RemoveAt</span><span class="br0">&#40;</span><span class="kw4">int</span> i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aux <span class="br0">&#40;</span>n<span class="sy0">%</span>k<span class="br0">&#41;</span> <span class="br0">&#40;</span>s::acc<span class="br0">&#41;</span> sz'<br />
&nbsp; &nbsp; aux n <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="br0">&#40;</span>BigInteger <span class="br0">&#40;</span>symbols.<span class="me1">Count</span><span class="br0">&#41;</span><span class="br0">&#41;</span><br />
<br />
<span class="kw1">let</span> problem24 <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span> <span class="nu0">0</span> .. <span class="nu0">9</span> <span class="br0">&#93;</span><br />
&nbsp; &nbsp; <span class="sy0">|&gt;</span> nthPermutation 999999I <span class="co1">// 0-based index!!</span></code></pre>

<p>This algorithm might be much more powerful than what is needed to solve problem 24, but it is much more generic in nature.</p>

<p>Indeed, it does not generate only <em>lexicographically</em> ordered permutations. The permutationsare generated in an order defined by that of the input sequence. So, let&#8217;s say you wanted the 1.000.000th reverse-lexicographic order, you would obtain it as follows:</p>

<pre class="prettyprint lang-fsharp"><code><span class="br0">&#91;</span> <span class="nu0">9</span> .. <span class="sy0">-</span><span class="nu0">1</span> .. <span class="nu0">0</span> <span class="br0">&#93;</span> <span class="sy0">|&gt;</span> nthPermutation 999999I</code></pre>

<p>If you would like the 15th permutation of the sequence (a, 1, b, 2, f, z, 3), you could do:</p>

<pre class="prettyprint lang-fsharp"><code><span class="br0">&#91;</span> <span class="kw8">box</span> 'a'; <span class="kw8">box</span> <span class="nu0">1</span>; <span class="kw8">box</span> 'b'; <span class="kw8">box</span> <span class="nu0">2</span>; <span class="kw8">box</span> 'f'; <span class="kw8">box</span> 'z';<br />
&nbsp; <span class="kw8">box</span> <span class="nu0">3</span> <span class="br0">&#93;</span><br />
<span class="sy0">|&gt;</span> nthPermutation 14I</code></pre>

<p>Finally, this algorithm provides an easy way to obtain random permutations. Simply generate a random integer to be used as the permutation index:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> symbols <span class="sy0">=</span> <span class="br0">&#91;</span> <span class="nu0">0</span> .. <span class="nu0">9</span> <span class="br0">&#93;</span><br />
<br />
<span class="kw1">let</span> totalPermutations <span class="sy0">=</span> <br />
&nbsp; &nbsp; symbols<br />
&nbsp; &nbsp; <span class="sy0">|&gt;</span> <span class="kw2">List</span>.<span class="me1">length</span><br />
&nbsp; &nbsp; <span class="sy0">|&gt;</span> BigInteger<br />
&nbsp; &nbsp; <span class="sy0">|&gt;</span> factorial<br />
<br />
<span class="kw1">open</span> System<br />
<br />
<span class="kw1">let</span> random <span class="sy0">=</span> <span class="kw1">new</span> Random<span class="br0">&#40;</span><span class="kw4">int</span> DateTime.<span class="me1">Now</span>.<span class="me1">Ticks</span><span class="br0">&#41;</span><br />
symbols<br />
<span class="sy0">|&gt;</span> nthPermutation <span class="br0">&#40;</span>BigInteger <span class="br0">&#40;</span>random.<span class="me1">Next</span><span class="br0">&#40;</span>totalPermutations<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></code></pre>

<p>That&#8217;s it! I hope this can be useful to someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://brunoreis.com/tech/nth-permutation-sequence-symbols/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>An old JavaScript implementation bug &#8211; parseInt</title>
		<link>http://brunoreis.com/tech/old-javascript-implementation-bug-parseint/</link>
		<comments>http://brunoreis.com/tech/old-javascript-implementation-bug-parseint/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 23:23:18 +0000</pubDate>
		<dc:creator>Bruno França dos Reis</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://brunoreis.com/tech/?p=113</guid>
		<description><![CDATA[Hello! Today in the company I&#8217;m working, a colleague was saying he had found a very very strange behaviour in JavaScript. I think he was parsing string representing dates by means of the parseInt function. The problem is that his strings were like &#8220;23/04/2008&#8243;, &#8220;17/08/2005&#8243;, and so on. If you split those strings in the [...]]]></description>
			<content:encoded><![CDATA[<p>Hello!</p>

<p>Today in the company I&#8217;m working, a colleague was saying he had found a very very strange behaviour in JavaScript. I think he was parsing string representing dates by means of the parseInt function.</p>

<p>The problem is that his strings were like &#8220;23/04/2008&#8243;, &#8220;17/08/2005&#8243;, and so on. If you split those strings in the slash character, and then try to parseInt each part obtained, you will end up trying to do a</p>

<pre class="prettyprint lang-javascript"><code><span class="kw2">var</span> day <span class="sy0">=</span> parseInt<span class="br0">&#40;</span><span class="st0">&quot;17&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="kw2">var</span> month <span class="sy0">=</span> parseInt<span class="br0">&#40;</span><span class="st0">&quot;08&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span><br />
<span class="kw2">var</span> year <span class="sy0">=</span> parseInt<span class="br0">&#40;</span><span class="st0">&quot;2005&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span></code></pre>

<p>For those who would expect to get:</p>

<ul>
<li>day = 17,</li>
<li>month = 8, and</li>
<li>year = 2005,</li>
</ul>

<p>they will have a surprise, if you run the script in most browsers. What you will get is:</p>

<ul>
<li>day = 17,</li>
<li><strong>month = 0</strong>, and</li>
<li>year = 2005,</li>
</ul>

<p>By playing around for less than one minute, you can find out what is happening: JavaScript sees that the string starts with a &#8220;0&#8243; and is <em>very smart</em> (sarcasm) and decides to parse it as an <strong>octal</strong>.</p>

<p>Googling (or Binging, nowadays&#8230;) a bit, you will see very old threads on forums discussing this bug. Some people say &#8220;JavaScript is broken&#8221;, others reply &#8220;JavaScript is not broken. RTFM.&#8221;, and then the usual flame war is started, people invoking flame war laws, and all.</p>

<p>So, what is really happening here? Who is correct?</p>

<h2>The ECMA-262 standard</h2>

<p>As tend to I dislike unfounded discussions, my first reaction was to look for a copy of the ECMA-262 standard, which can be downloaded here: <a href="http://www.ecma-international.org/publications/standards/Ecma-262.htm">http://www.ecma-international.org/publications/standards/Ecma-262.htm</a>.</p>

<p>Page 77, paragraph 15.1.2.2, states the definition of the parseInt function. THAT is authoritative information, isn&#8217;t it?</p>

<p>So, the definition of parseInt, which actually takes 2 arguments (string and radix) reads:</p>

<blockquote>The parseInt function produces an integer value dictated by interpretation of the contents of the string argument according to the specified radix. Leading whitespace in the string is ignored. <strong>If radix is undefined or 0, it is assumed to be 10 except when the number begins with the character pairs 0x or 0X, in which case a radix of 16 is assumed. Any radix-16 number may also optionally begin with the character pairs 0x or 0X.</strong>
</blockquote>

<p>As you can see, the standard states that JavaScript plays the smart guy when dealing with strings starting with &#8220;0x&#8221;, that is, hexadecimal string representation of numbers. Now, where does it say JavaScript should also be smart about octals?</p>

<p>Conclusion: JavaScript is indeed not implemented in the standard way in most browsers nowadays.</p>

<p>Actually, the specs, in the next page, permits implementations to interpret strings beginning by &#8220;0&#8243; without and &#8220;x&#8221; or &#8220;X&#8221; right after as octals. However, in encourages them to interpret it as decimal. <b>Why would leading browsers diverge from the official recommendations?</b></p>

<h2>Workarounds</h2>

<p>A number of simple workarounds are available. First, you can simply use the radix argument and specify it should be 10:</p>

<pre class="prettyprint lang-javascript"><code><span class="kw2">var</span> day <span class="sy0">=</span> parseInt<span class="br0">&#40;</span><span class="st0">&quot;08&quot;</span><span class="sy0">,</span> <span class="nu0">10</span><span class="br0">&#41;</span></code></pre>

<p>Another solution, the one I prefer (and use all the time) when converting strings to numbers is</p>

<pre class="prettyprint lang-javascript"><code><span class="kw2">var</span> day <span class="sy0">=</span> <span class="st0">&quot;08&quot;</span> <span class="sy0">-</span> <span class="nu0">0</span></code></pre>

<p>Yes, you subtract the INTEGER 0 from a string. To see why it is syntactically correct, you can go the the page 31, section 9.3.1 of the same document and read the ToNumber function, and then read the section 11.6.2 on the page 50, about the subtraction operator. Firstly it converts both operands to numbers with the ToNumber function. After that it calculates the result.</p>

<p>The point is, at leasts in Firefox, the implementation of ToNumber (which deals with &#8220;0x&#8221; as parseInt) works correctly when dealing with strings such as &#8220;08&#8243;, and correctly parses it to 8.</p>

<p><strong>Now, I ask: why does Firefox correctly implement ToNumber but gives unexpected results on the parseInt function, when the specs says it is recommended to use base 10?</strong></p>

<p>Comment!</p>
]]></content:encoded>
			<wfw:commentRss>http://brunoreis.com/tech/old-javascript-implementation-bug-parseint/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Uniformly distributed random list permutation in F#</title>
		<link>http://brunoreis.com/tech/uniformly-distributed-random-list-permutation/</link>
		<comments>http://brunoreis.com/tech/uniformly-distributed-random-list-permutation/#comments</comments>
		<pubDate>Tue, 05 May 2009 00:16:07 +0000</pubDate>
		<dc:creator>Bruno França dos Reis</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://brunoreis.com/tech/?p=77</guid>
		<description><![CDATA[The last time I presented a method to generate a random permutation of a given list, in linear time, based on another article. The problem with that approach is that the algorithm used does not provide a uniform distribution of the resulting lists, for a number of reasons. Actually, call the set of the permutations [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://brunoreis.com/tech/generate-random-int-list/">last time</a> I presented a method to generate a random permutation of a given list, in linear time, based on another article.</p>

<p>The problem with that approach is that the algorithm used <strong>does not provide a uniform distribution</strong> of the resulting lists, for a number of reasons. Actually, call <img src='http://s.wordpress.com/latex.php?latex=%5Cmathcal%7BP%7D%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt='\mathcal{P} ' title='\mathcal{P} ' class='latex' /> the set of the permutations of the input list, the function <em>randPermutateArray</em> <img src='http://s.wordpress.com/latex.php?latex=%3A%20%5Cmathcal%7BP%7D%20%5Cto%20%5Cmathcal%7BP%7D%20&#038;bg=ffffff&#038;fg=000000&#038;s=0' alt=': \mathcal{P} \to \mathcal{P} ' title=': \mathcal{P} \to \mathcal{P} ' class='latex' /> is not even surjective! This can easily be verified by noting that the function will always swap the element of index <em>i</em> by another one, of index between <em>0</em> and <em>i-1</em>, and thus the last element will always be displaced.</p>

<p>Although this can be desired for some applications, it is definitely not what I need.</p>

<h3>A linear, unformly distributed random list permutation algorithm</h3>

<p>A function with the desired properties (surjective, uniformly distributed permutations, linear complexity) would be like constructing a list with elements randomly extracted from a <em>source set</em>. The easiest way to achieve this is to use the input array (size <em>n</em>) as the source set, randomly select an index (from 0 up to the last one) and swap it with the last element, and then loop this considering an array of size <em>n-1</em>:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> randPermuteArray a <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> n <span class="sy0">=</span> <span class="kw2">Array</span>.<span class="me1">length</span> a<br />
&nbsp; &nbsp; <span class="kw1">let</span> <span class="kw1">rec</span> aux <span class="sy0">=</span> <span class="kw1">function</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; | <span class="nu0">0</span> <span class="sy0">-&gt;</span> a<br />
&nbsp; &nbsp; &nbsp; &nbsp; | k <span class="sy0">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> i <span class="sy0">=</span> rand<span class="br0">&#40;</span>k<span class="sy0">+</span><span class="nu0">1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> tmp <span class="sy0">=</span> a.<span class="br0">&#91;</span>i<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">&lt;-</span> a.<span class="br0">&#91;</span>k<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.<span class="br0">&#91;</span>k<span class="br0">&#93;</span> <span class="sy0">&lt;-</span> tmp<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aux <span class="br0">&#40;</span>k<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; aux <span class="br0">&#40;</span>n<span class="sy0">-</span><span class="nu0">1</span><span class="br0">&#41;</span></code></pre>

<p>As you can see, the key difference is the random index we select. With this approach, every permutation of the input sequence (the input sequence itself as well!) is as likely to be yielded as the others.</p>

<p>At least if your randomness source is <em>really</em> random&#8230; but that&#8217;s for another article!</p>
]]></content:encoded>
			<wfw:commentRss>http://brunoreis.com/tech/uniformly-distributed-random-list-permutation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating random int list in F#</title>
		<link>http://brunoreis.com/tech/generate-random-int-list/</link>
		<comments>http://brunoreis.com/tech/generate-random-int-list/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 02:00:29 +0000</pubDate>
		<dc:creator>Bruno França dos Reis</dc:creator>
				<category><![CDATA[F#]]></category>

		<guid isPermaLink="false">http://brunoreis.com/tech/?p=48</guid>
		<description><![CDATA[Hello! I was working on a F# project doing some maths, and I needed a time-efficient way to generate random permutations of the list [0; 1; ...; n ], for a positive integer n. First attempt My first attempt was very naive, poor in performance. It&#8217;s complexity was O(n²). Start with a random integer generator: [...]]]></description>
			<content:encoded><![CDATA[<p>Hello!</p>

<p>I was working on a F# project doing some maths, and I needed a time-efficient way to generate random permutations of the list [0; 1; ...; n ], for a positive integer n.</p>

<h3>First attempt</h3>

<p>My first attempt was very naive, poor in performance. It&#8217;s complexity was O(n²).</p>

<p>Start with a random integer generator:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> rand <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> rndGen <span class="sy0">=</span> <span class="kw1">new</span> System.<span class="me1">Random</span><span class="br0">&#40;</span><span class="kw4">int</span> System.<span class="me1">DateTime</span>.<span class="me1">Now</span>.<span class="me1">Ticks</span><span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#40;</span><span class="kw1">fun</span> <span class="sy0">max</span> <span class="sy0">-&gt;</span> rndGen.<span class="me1">Next</span><span class="br0">&#40;</span><span class="sy0">max</span><span class="br0">&#41;</span><span class="br0">&#41;</span></code></pre>

<p>The idea was to randomly extract elements from a &#8220;source&#8221; and use them to construct the list. The code looks like this:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> randUniqueIntList <span class="sy0">max</span> <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> <span class="kw1">rec</span> aux l s <span class="sy0">=</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">match</span> <span class="kw2">List</span>.<span class="me1">length</span> s <span class="kw1">with</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; | <span class="nu0">0</span> <span class="sy0">-&gt;</span> l<br />
&nbsp; &nbsp; &nbsp; &nbsp; | n <span class="sy0">-&gt;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> i <span class="sy0">=</span> rand<span class="br0">&#40;</span>n<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> r <span class="sy0">=</span> s.<span class="br0">&#91;</span>i<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; aux <span class="br0">&#40;</span>r::l<span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw2">List</span>.<span class="me1">filter</span> <span class="br0">&#40;</span><span class="kw1">fun</span> x <span class="sy0">-&gt;</span> x <span class="sy0">&lt;&gt;</span> r<span class="br0">&#41;</span> s<span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> l0 <span class="sy0">=</span> <span class="br0">&#91;</span><span class="nu0">0</span> .. <span class="br0">&#40;</span><span class="sy0">max</span> <span class="sy0">-</span> <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#93;</span><br />
&nbsp; &nbsp; aux <span class="br0">&#91;</span><span class="br0">&#93;</span> l0</code></pre>

<p>The recursive <em>aux</em> function takes an accumulator list and a source list. It is recursively called roughly <em>max</em> times. The bottleneck in each run of <em>aux</em> is clearly the call to <em>List.filter</em>, which is O(<em>List.length s</em>) in complexity. Therefore, the overall complexity of this function is O(n²)!</p>

<h3>More efficient solution</h3>

<p>In <a href="http://cs.hubfs.net/forums/thread/10181.aspx">this thread at hubFS</a>, I was pointed to <a href="http://lorgonblog.spaces.live.com/blog/cns!701679AD17B6D310!156.entry">an article</a> showing an efficient function that randomly permutes an array (using the fact that an array is a <em>mutable</em> structure), as follows:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> randPermutateArray a <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> n <span class="sy0">=</span> <span class="kw2">Array</span>.<span class="me1">length</span> a<br />
&nbsp; &nbsp; <span class="kw1">for</span> x <span class="kw1">in</span> <span class="nu0">1</span> .. <span class="me1">n</span> <span class="kw1">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> i <span class="sy0">=</span> n <span class="sy0">-</span> x<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> j <span class="sy0">=</span> rand<span class="br0">&#40;</span>i<span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">let</span> tmp <span class="sy0">=</span> a.<span class="br0">&#91;</span>i<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a.<span class="br0">&#91;</span>i<span class="br0">&#93;</span> <span class="sy0">&lt;-</span> a.<span class="br0">&#91;</span>j<span class="br0">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; a.<span class="br0">&#91;</span>j<span class="br0">&#93;</span> <span class="sy0">&lt;-</span> tmp<br />
&nbsp; &nbsp; a</code></pre>

<p>Since I wanted to work only with immutable data in my code, I wrapped that function around another one in order to encapsulate the mutable array, as follows:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> randPermutateList l <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="kw1">let</span> a <span class="sy0">=</span> <span class="kw2">Array</span>.<span class="me1">of_list</span> l<br />
&nbsp; &nbsp; a <span class="sy0">|&gt;</span> randPermutateArray <span class="sy0">|&gt;</span> <span class="kw2">List</span>.<span class="me1">of_array</span></code></pre>

<p>Voilà, a function to randomly permutate a generic list with O(n) complexity!</p>

<p>To generate the random permutation of [ 0 .. n ], the code is straightforward:</p>

<pre class="prettyprint lang-fsharp"><code><span class="kw1">let</span> genRandomIntList n <span class="sy0">=</span><br />
&nbsp; &nbsp; <span class="br0">&#91;</span> <span class="nu0">0</span> .. <span class="me1">n</span> <span class="br0">&#93;</span> <span class="sy0">|&gt;</span> randPermutateList</code></pre>

<p>Can you further improve the algorithm on performance? Comment on it!</p>
]]></content:encoded>
			<wfw:commentRss>http://brunoreis.com/tech/generate-random-int-list/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

