<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Jeff&#039;s Blog</title>
	<atom:link href="http://blog.jeffbergman.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.jeffbergman.com</link>
	<description>Functional Programming, Web Development and other interests</description>
	<lastBuildDate>Sat, 24 Sep 2011 19:31:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.jeffbergman.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Jeff&#039;s Blog</title>
		<link>http://blog.jeffbergman.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.jeffbergman.com/osd.xml" title="Jeff&#039;s Blog" />
	<atom:link rel='hub' href='http://blog.jeffbergman.com/?pushpress=hub'/>
		<item>
		<title>Switching Gears</title>
		<link>http://blog.jeffbergman.com/2011/09/20/switching-gears/</link>
		<comments>http://blog.jeffbergman.com/2011/09/20/switching-gears/#comments</comments>
		<pubDate>Tue, 20 Sep 2011 05:01:00 +0000</pubDate>
		<dc:creator>jbergmanster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[org-mode]]></category>
		<category><![CDATA[org2blog]]></category>

		<guid isPermaLink="false">http://jbergmanster.wordpress.com/2011/09/20/switching-gears/</guid>
		<description><![CDATA[Update about moving from Windows to Linux and Emacs <a href="http://blog.jeffbergman.com/2011/09/20/switching-gears/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=83&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="outline-container-1" class="outline-3">
<h3 id="sec-1">New Horizons</h3>
<div class="outline-text-3" id="text-1">
<p>So I started a job at a new company, a famous search engine! One consequence is that I am no longer using Windows and the Microsoft Stack, instead,  Linux, shells, and emacs.  </p>
</p></div>
</p></div>
<div id="outline-container-2" class="outline-3">
<h3 id="sec-2">What do I miss from Windows development</h3>
<div class="outline-text-3" id="text-2">   </div>
<div id="outline-container-2-1" class="outline-4">
<h4 id="sec-2-1">F#</h4>
<div class="outline-text-4" id="text-2-1">
<p>This is a really cool functional language that has some nice facilities for parallel and async programming. Unfortunately, I wasn&#8217;t able to use it at my work. </p>
</div></div>
<div id="outline-container-2-2" class="outline-4">
<h4 id="sec-2-2">Visual Studio Intellisense</h4>
<div class="outline-text-4" id="text-2-2">
<p>Eclipse has this and Emacs has some completion and search facilities. But, Visual Studio&#8217;s blows both of these away in it&#8217;s speed. With Eclipse I often find it faster to type out the whole token instead of waiting for it to complete something. </p>
</div></div>
<div id="outline-container-2-3" class="outline-4">
<h4 id="sec-2-3">var keyword in C#</h4>
<div class="outline-text-4" id="text-2-3">
<p>Using java now, I am frustrated by its verboseness. This is especially a problem when one makes heavy use of Generics. </p>
</div></div>
<div id="outline-container-2-4" class="outline-4">
<h4 id="sec-2-4">lambda expressions</h4>
<div class="outline-text-4" id="text-2-4">
<p>Many classes here require function objects. The workaround is to usually create an anonymous class that implements one method on an interface. The syntax is much bulkier than C#, let alone F#. </p>
</div></div>
<div id="outline-container-2-5" class="outline-4">
<h4 id="sec-2-5">C# generics</h4>
<div class="outline-text-4" id="text-2-5">
<p>Java&#8217;s generics feel clumsy and less powerful. The main source of this clumsiness is the constant compiler warnings one gets and has to supress when using generics in java. </p>
</p></div>
</p></div>
</p></div>
<div id="outline-container-3" class="outline-3">
<h3 id="sec-3">What do I love about Linux development</h3>
<div class="outline-text-3" id="text-3">   </div>
<div id="outline-container-3-1" class="outline-4">
<h4 id="sec-3-1">Emacs</h4>
<div class="outline-text-4" id="text-3-1">
<p>Ok, this is an insanely powerful and addictive tool. Many people hate it but once you start to use it you want to spend more and more time using it. It has all these nooks and crannies to discover. For instance. </p>
</div></div>
<div id="outline-container-3-2" class="outline-4">
<h4 id="sec-3-2"><a href="http://orgmode.org">org-mode</a></h4>
<div class="outline-text-4" id="text-3-2">
<p>Org-Mode is a combination of a note-taking mode, to-do list, and publishing framework. People have found many ways to use it. In fact, this blog post is written within org-mode and published using  <a href="https://github.com/punchagan/org2blog">org2blog</a> without ever leaving emacs. org-mode has tags to make it super easy to embed source by bracketing it with tags. </p>
</div></div>
<div id="outline-container-3-3" class="outline-4">
<h4 id="sec-3-3">Other Emacs stuff</h4>
<div class="outline-text-4" id="text-3-3">
<p>There is a killer twitter application built into emacs for reading and publishing all your tweets. And the Calc application is a very advanced RPN Calculator with symbolic computation capabilities. The power of using these different tools from within emacs is the ability to use emacs text editing and scripting capabilities in a consistent way in each of these. Also, it makes integration of input and output from these different things very easy. </p>
</div></div>
<div id="outline-container-3-4" class="outline-4">
<h4 id="sec-3-4">Bash Shell</h4>
<div class="outline-text-4" id="text-3-4">
<p>Windows shell is really sad and the paradigm there is to do everything with the mouse. There is something liberating about being able to do so many things without lifting your hands off the keyboard, through, emacs, the shell, and all the utilities on Linux. Little things like the history and grep and the ease with which one can whip together shell scripts to automate things. I know one can install things like cygwin but it is all a bit of hack on windows. </p>
</div></div>
<div id="outline-container-3-5" class="outline-4">
<h4 id="sec-3-5"><a href="http://www.amazon.com/Effective-Java-Programming-Language-Guide/dp/0201310058">Effective Java</a></h4>
<div class="outline-text-4" id="text-3-5">
<p>Yes, its just a book, but I haven&#8217;t seen many books of this depth on programming. It illuminates many pitfalls and gotchas and emphasizes immutability, correctness, and efficiency. I think any programmer would benefit from reading this. </p>
</div></div>
<div id="outline-container-3-6" class="outline-4">
<h4 id="sec-3-6">Intangibles</h4>
<div class="outline-text-4" id="text-3-6">
<p>The feel of programming with the Linux stack is different, IMO. With Linux there are so many choices for tools and how to combine them. In contrast, on the windows platform, almost the entire stack comes from Microsoft from the OS, to the language and tools. While this simplifies certain things it feels limiting in a way. In Linux, I feel like I am exploring a vast ocean of possibilities. </p>
</div></div>
</p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbergmanster.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbergmanster.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbergmanster.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbergmanster.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbergmanster.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbergmanster.wordpress.com/83/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbergmanster.wordpress.com/83/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbergmanster.wordpress.com/83/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=83&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.jeffbergman.com/2011/09/20/switching-gears/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1b0f1eada613f9857b4b2ddfcb1b6cf5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbergmanster</media:title>
		</media:content>
	</item>
		<item>
		<title>Permutation Generation In F-Sharp</title>
		<link>http://blog.jeffbergman.com/2010/03/09/permutation-generation-in-f-sharp/</link>
		<comments>http://blog.jeffbergman.com/2010/03/09/permutation-generation-in-f-sharp/#comments</comments>
		<pubDate>Tue, 09 Mar 2010 01:52:47 +0000</pubDate>
		<dc:creator>jbergmanster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Combinatorics]]></category>
		<category><![CDATA[F-Sharp]]></category>

		<guid isPermaLink="false">http://jbergmanster.wordpress.com/?p=49</guid>
		<description><![CDATA[Someone asks you to generate all the perumtations from a set of objects and you might think that this is an easy problem  And it isn&#8217;t that challenging but I had to think about it for a bit to come &#8230; <a href="http://blog.jeffbergman.com/2010/03/09/permutation-generation-in-f-sharp/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=49&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Someone asks you to generate all the perumtations from a set of objects and you might think that this is an easy problem  And it isn&#8217;t that challenging but I had to think about it for a bit to come up with away to do this.  I thought it would be fun to code this up in F# to see if my algorithm was correct and to learn about the language a bit more.</p>
<p>The algorithm I came up with is essentially a depth first search like approach.  Taking the first element and printing it and then moving it to the ancestor list and printing it with the next one and so on.  This will print the permutations in lexicographic order providing the input elements are sorted.  Here is the source code below.</p>
<pre class="brush: fsharp;">
#light
/// List printing function need a generic version
let ListPrinter format a n =
 printf  &quot;%A. &quot; n
 a|&gt; List.iter (fun x -&gt; (printf format x))
 printf &quot;\n&quot;

/// Print all the permutation of a list of elements
/// Algorithm Pseudo Code
/// PermutationGenerator(list items)
///    PermuationHelper([], items)
/// end
///
/// PermutationHelper(list ancestors, list descendants)
///  if descandants is null return
///  foreach descendant
///    newAncestors  = ancestors + descendant
///    print(newAncestors)
///    newDescandants = descendants - descendant
///    PernumationHelper(newAncestors, newDescandants)
///  end
/// end
let PermutationGenerator printer items =
 ///internal counter function
 let counter =
  let count = ref 0I
  fun () -&gt; count.contents &lt;- count.contents + 1I; !count
 /// Helper recursive function that does the work
 let rec PermutationHelper ancestors descendants =
  if ancestors &lt;&gt; [] then printer ancestors (counter())

  match descendants with
  | [] -&gt; ()
  | y::ys -&gt; let fn = (fun (front, back : 'a list) curr -&gt;
                 let newFront = front @ [curr]
                 let newBack = back.Tail
                 let newAncestors = ancestors @ [curr]
                 let newDescendants = front @ newBack
                 PermutationHelper newAncestors newDescendants
                 (newFront, newBack))
   in List.fold_left fn ([],descendants) descendants |&gt; ignore
 // call the nested function
 PermutationHelper [] items

let items = [&quot;apple&quot;;&quot;banana&quot;; &quot;canteloupe&quot;;]
 //&quot;cherry&quot;; &quot;grape&quot;;&quot;orange&quot;;
 //&quot;peach&quot;;&quot;pear&quot;; &quot;strawberry&quot;]
let numbers = [ 1..5 ]

let chars = ['a'..'e']

let StringPrinter = ListPrinter &quot;%s &quot;
let NumericPrinter = ListPrinter &quot;%d&quot;
let CharPrinter = ListPrinter &quot;%c&quot;

let StringPermutations = PermutationGenerator StringPrinter
let NumericPermutations = PermutationGenerator NumericPrinter
let CharPermutations = PermutationGenerator CharPrinter
</pre>
<p>You can load the above into the F# interactive window and run the following command and see the following results.</p>
<p>&gt; StringPermutations items;;<br />
1I. apple<br />
2I. apple banana<br />
3I. apple banana canteloupe<br />
4I. apple canteloupe<br />
5I. apple canteloupe banana<br />
6I. banana<br />
7I. banana apple<br />
8I. banana apple canteloupe<br />
9I. banana canteloupe<br />
10I. banana canteloupe apple<br />
11I. canteloupe<br />
12I. canteloupe apple<br />
13I. canteloupe apple banana<br />
14I. canteloupe banana<br />
15I. canteloupe banana apple</p>
<p>Some of the more interesting parts as someone learning functional programming include the use of curried functions, the fold operator, and pattern matching.</p>
<p><strong>PermutationGenerator</strong> is a generic function that can work with any list of items of  type <strong>a&#8217; list</strong> as long as a corresponding printer for the items is supplied of type  <strong>a&#8217; list -&gt; bigint -&gt; unit</strong>.  The basic algorithm is listed above <strong>PermutationGeneration</strong> in the comments.  Although, F# supports loops the functional way to iterate over elements is to use a function like map or fold, so the foreach loop is replaced by <strong>List.fold_left</strong> with <strong>fn</strong> essentially being the body of the loop.</p>
<p>I then build a <strong>ListPrinter </strong>function that takes a format string.  From this I create printers for three different types using partial application or currying, <strong>StringPrinter, NumericPrinter</strong>, and <strong>CharPrinter</strong>.  This allows the creation of three new more specialized functions from the <strong>ListPrinter</strong>.  Currying is also used to create three specific Permutation Generators, <strong>StringPermutations</strong>, <strong>NumericPermutations</strong>, <strong>CharPermutations</strong> from <strong>PermutationGenerator</strong> by applying the particular printer.</p>
<p>There are other techniques for generating permutations.  Some involve shuffling of positions of elements after selecting the number of items.  Another technique is to use <a title="factoradics" href="http://en.wikipedia.org/wiki/Factorial_number_system" target="_self">factoradics</a> which give a way to select elements or permutations based on the factorial number system.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbergmanster.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbergmanster.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbergmanster.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbergmanster.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbergmanster.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbergmanster.wordpress.com/49/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbergmanster.wordpress.com/49/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbergmanster.wordpress.com/49/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=49&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.jeffbergman.com/2010/03/09/permutation-generation-in-f-sharp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1b0f1eada613f9857b4b2ddfcb1b6cf5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbergmanster</media:title>
		</media:content>
	</item>
		<item>
		<title>If you can&#8217;t beat them&#8230;</title>
		<link>http://blog.jeffbergman.com/2010/03/08/if-you-cant-beat-them/</link>
		<comments>http://blog.jeffbergman.com/2010/03/08/if-you-cant-beat-them/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 04:27:36 +0000</pubDate>
		<dc:creator>jbergmanster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://jbergmanster.wordpress.com/?p=16</guid>
		<description><![CDATA[I had previously tried to write and host my own blogging engine in asp.net mvc.  However, I learned that writing a great blog engine is a lot harder than it seems.  Some of the obstacles I ran into were, Time &#8230; <a href="http://blog.jeffbergman.com/2010/03/08/if-you-cant-beat-them/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=16&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I had previously tried to write and host my own blogging engine in asp.net mvc.  However, I learned that writing a great blog engine is a lot harder than it seems.  Some of the obstacles I ran into were,</p>
<ol>
<li><strong>Time consuming</strong> &#8211; between work and grad school I didn&#8217;t have time to pull it off</li>
<li><strong>Tons of features</strong> &#8211; having not blogged much I thought I only needed to build something that displayed posts and comments but the sheer number of features blog engines support today is daunting</li>
<li><strong>Need great themes</strong> &#8211; even if you do build a great blog engine it is only as good as the themes available for it</li>
<li><strong>Learning while building it</strong> &#8211; asp.net mvc was a beta product at the time and many of the patterns and best practices were not fully formed</li>
</ol>
<p>It wasn&#8217;t all for naught as I learned a ton about asp.net mvc which was helpful as I went on to do several projects at work with this technology.  And I think if I was starting something like this again I would have a better idea on how I would approach such a project.</p>
<p>That being said, I came away with a great respect for people who do this kind of open source work and actually produce great tools.</p>
<p>And for blogging wordpress is hard to beat.  I haven&#8217;t seen anything else that comes close.  So, I will be importing posts from my previous blog and I hope to post more frequently than once a year.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbergmanster.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbergmanster.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbergmanster.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbergmanster.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbergmanster.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbergmanster.wordpress.com/16/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbergmanster.wordpress.com/16/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbergmanster.wordpress.com/16/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=16&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.jeffbergman.com/2010/03/08/if-you-cant-beat-them/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1b0f1eada613f9857b4b2ddfcb1b6cf5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbergmanster</media:title>
		</media:content>
	</item>
		<item>
		<title>Birthday Paradox and Probability of Collisions</title>
		<link>http://blog.jeffbergman.com/2009/09/28/birthday-paradox-and-probability-of-collisions/</link>
		<comments>http://blog.jeffbergman.com/2009/09/28/birthday-paradox-and-probability-of-collisions/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 17:32:56 +0000</pubDate>
		<dc:creator>jbergmanster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[F-Sharp]]></category>
		<category><![CDATA[Probability]]></category>

		<guid isPermaLink="false">http://jbergmanster.wordpress.com/?p=24</guid>
		<description><![CDATA[The birthday paradox is the phenomenon where even with a small fraction of the number of days in the year the probability of two people having the same birthday becomes large.  For instance if there are 36 people in a &#8230; <a href="http://blog.jeffbergman.com/2009/09/28/birthday-paradox-and-probability-of-collisions/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=24&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://en.wikipedia.org/wiki/Birthday_paradox">birthday  paradox</a> is the phenomenon where even with a small fraction of the  number of days in the year the probability of two people having the same  birthday becomes large.  For instance if there are 36 people in a room  (10% of 365) there is a 83% probability that two of them have the same  birthday.  With only 23 people we are already over 50%.</p>
<p>This same logic doesn&#8217;t only apply, though, to birthdays.  One can  calculate the probability of collision for any set of elements given the  number of samples.  For instance, assume you want to assign a unique  number to each of your customers.   And let&#8217;s say you were going to  choose the number at random from a huge pool of numbers say 999,999,999.   If you anticipate having 100,000 customers then one would expect with  probability 99.3% to sample a number that was previously sampled for  another customer, even though you have only selected .01% of the  available numbers.  I thought it would be fun to write an F# program to  calculate this.</p>
<p>The probability is calculated as follows<strong> P = 1 &#8211; Probability of  no collisions</strong></p>
<p>It is easier to calculate the probability that there aren&#8217;t  collisions and subtract that from one. We can use simple counting  principles to calculate the probability of choosing n samples without  any collisions. For example if we have 365 days as the elements and we  have 3 people (samples), then the first person has 365 possibble days he  could choose without collision.  The second person has 364 possible  days out of 365 and the third 363 possible days.   So</p>
<p><strong>Probabiliby of No Collisions = (365/365)*(364/365)*(363/365)</strong><br />
and<br />
<strong>Probabiliby of Collision = 1 &#8211; (365/365)*(364/365)*(363/365)</strong></p>
<p>Here is the following code which implements the above algorithm in F#</p>
<pre class="brush: fsharp;">
let collisionProbability  elements samples =
 let probNthSample n =
  let a = (float  elements)
  let b = (float  (elements - n + 1))
  b / a
 let rec probNoCollision  counter answer =
  match counter with
  | 0 -&gt; answer
  | _ -&gt; probNoCollision (counter - 1) (answer *  (probNthSample counter))
 in
  1.0 - (probNoCollision samples 1.0)
</pre>
<p>Some things I like about F# are its terseness.  You don&#8217;t have to  provide type annotations for everything unlike in C# or Java.  The  compiler infers them based on how they are used.   Here, I am also using  nested functions, which are great for breaking up an algorithm into  smaller bits while still maintaining the scope of the functions to where  they will be used.   Also, nested functions can use values in scope.   For instance <em>elements</em> is not passed into <em>probNthSample</em>.  This  algorithm was coded using tail-recursion to iterate over the different  samples.  Typically one would use a loop for this in imperative  programming, but in a functional style recursion is used more often.   (One could also make a list of the samples and fold_left.)  Tail-recursion prevents the stack size from growing by not keeping  previously called procedures on the stack if there is no additional work  to be done.  In the inner <em>probNoCollision </em>method, this is the  case.   The last thing that is used here is pattern matching.   Stylistically, I like pattern matching better than if statements.</p>
<p>The <em>probNoCollision </em>function calculates what it name says.  It  multiplies together the probability of all the samples not colliding.</p>
<p><em>probNthSample </em>is a helper function which just gets a float of the  probability of the nth sample not colliding.  For instance, in the  birthday problem for the 2nd sample this method would return 364/365.</p>
<p>The last step just returns <strong>1-probNoCollision</strong>.</p>
<p>To run this you could load this in the F# interactive window. And  then run the command<br />
<strong>collisionProbability 365 23;;</strong><br />
<strong>collisionProbability 999999999 100000;;</strong></p>
<p>I am really enjoying F# and OCAML programming so far.  I will be  posting an article soon on permutations that I also coded in F#.  This  will also be in a functional style and will show more techniques of code  reuse that are powerful, that one doesn&#8217;t typically enjoy in languages  like C#.</p>
<p>Update -</p>
<p>I have rewritten the above function in a more functional style.</p>
<pre class="brush: fsharp;">

let collisionProb2 elements samples =
 let probNoCollision answer n =
  let a = (float elements)
  let b = (float (elements - n + 1))
  answer * b / a
 let answer = seq { 1 .. samples } |&gt; Seq.fold probNoCollision 1.0
 1.0 - answer
</pre>
<p>This version of the code is more succinct and uses the fold function, as opposed to explicitly recursing over the elements.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbergmanster.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbergmanster.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbergmanster.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbergmanster.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbergmanster.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbergmanster.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbergmanster.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbergmanster.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=24&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.jeffbergman.com/2009/09/28/birthday-paradox-and-probability-of-collisions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1b0f1eada613f9857b4b2ddfcb1b6cf5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbergmanster</media:title>
		</media:content>
	</item>
		<item>
		<title>A simple example of the Hole In the Middle Pattern</title>
		<link>http://blog.jeffbergman.com/2007/09/12/a-simple-example-of-the-hole-in-the-middle-pattern/</link>
		<comments>http://blog.jeffbergman.com/2007/09/12/a-simple-example-of-the-hole-in-the-middle-pattern/#comments</comments>
		<pubDate>Wed, 12 Sep 2007 05:05:19 +0000</pubDate>
		<dc:creator>jbergmanster</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Closure]]></category>
		<category><![CDATA[Design Pattern]]></category>

		<guid isPermaLink="false">http://blog.jeffbergman.com/?p=68</guid>
		<description><![CDATA[I mentioned the Hole in the Middle pattern in my previous blog article, What&#8217;s In a Language? So, I wanted to actually use the pattern in C# with anonymous delegates, because that is the platform that I use at work &#8230; <a href="http://blog.jeffbergman.com/2007/09/12/a-simple-example-of-the-hole-in-the-middle-pattern/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=68&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I mentioned the <a href="http://enfranchisedmind.com/blog/archive/2007/07/10/279">Hole in the Middle</a> pattern in my previous blog article, What&#8217;s In a Language? So, I wanted to actually use the pattern in C# with anonymous delegates, because that is the platform that I use at work primarily. One of the uses that jumped out was in a database layer. Typically when you are writing a database layer you have boilerplate code in every method to open and close connections and possibly handle exceptions and log exceptions. This is repetitive code that often gets pasted into every method. One approach for doing this is to create a code snippet that you can use from a snippet library. Another is to use the Hole in the Middle pattern.Here is a simple example of the idea:</p>
<pre class="brush: csharp;">
public delegate T DatabaseCodeBlock&lt;T&gt;(SqlConnection conn);

public class HoleInTheMiddle
{
  ConnectionHelper connHelper =new
  ConnectionHelper();

  public T ExecuteProcedure&lt;T&gt;(DatabaseCodeBlock&lt;T&gt; block)
  {
    SqlConnection conn = connHelper.GetSQLConnection();

    try
    {
      return block(conn);
    }
    catch(Exception ex)
    {
      LogError(ex.ToString());
      throw;
    }
    finally
    {
      conn.Close();
    }
  }
}

public class DbLayer
{
  HoleInTheMiddle holeInTheMiddle = new HoleInTheMiddle();

  public DataTable GetCustomerByCustomerKey(int customerKey)
  {
    return holeInTheMiddle.ExecuteProcedure&lt;DataTable&gt;( conn =&gt;
      {
        RunSpParmsClass parm = new RunSpParmsClass();

        parm.AppendEx(&quot;@CustomerID&quot;, SqlDbType. Int, ParameterDirection.Input, 4, customerKey);
        DataSet ds= dbHelper.RunSpGetDataset(&quot;GetCustomer&quot;, parm, conn);

        if (ds !=null &amp;&amp; ds.Tables[0].Rows.Count &gt; 0)
        {
          return ds.Tables[0];
        }
        else
        {
          return null;
        }
       }
     );
   }
}
</pre>
<p>Now some of you are wondering why go through this trouble when you can use the <a href="http://msdn2.microsoft.com/en-us/library/ms954827.aspx">Microsoft Data Access Application Block</a>. Good question, but the point of this sample is to illustrate one way of using this design pattern. Now let&#8217;s take a look at the code.  I have created a method <strong>ExecuteProcedure</strong> that has code to open and close a connection and to handle exceptions.  It then fires a delegate of type<strong> DatabaseCodeBlock</strong>.  This is the code block that we supply to fill the <em>hole</em> in this helper function.  I am using generics to strongly type the return type of the<strong> DatabaseCodeBlock</strong> and the <strong>ExecuteProcedure</strong> method.  This is not really neccessary as you could just set the value of a local variable in your anonymous delegate that you could return from the method that is calling <strong>ExecuteProcedure</strong>, but I think using generics makes for cleaner code.</p>
<p>Now the intersting part of this is the power of the anonymous delegate we are using for the <strong>DatabaseCodeBlock</strong>. Anonymous Delegates are what are known as closures and they can capture local variables and reference them in the code block.  Even if this code block is executed in a another context.  This allows one to not have to define different delegate interfaces if you want to use different patterns or to build some parameter array of inputs.  Instead, you can just pass in your anonymous delegate and use any local variable in it magically.  So in our example we are getting a Customer record by using the local customerKey variable.  Now imagine we have a different stored procedure that we want to invoke that requires three or four parameters.  If these are in the local scope of where the delegate is created then we can access them.  Easy as pie!  The last point is a subtle one.  I suggest reading up on <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closures</a> to get a better feel for some of the power of this way of doing things.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/jbergmanster.wordpress.com/68/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/jbergmanster.wordpress.com/68/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jbergmanster.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jbergmanster.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jbergmanster.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jbergmanster.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jbergmanster.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jbergmanster.wordpress.com/68/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jbergmanster.wordpress.com/68/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jbergmanster.wordpress.com/68/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.jeffbergman.com&amp;blog=7061610&amp;post=68&amp;subd=jbergmanster&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.jeffbergman.com/2007/09/12/a-simple-example-of-the-hole-in-the-middle-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/1b0f1eada613f9857b4b2ddfcb1b6cf5?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">jbergmanster</media:title>
		</media:content>
	</item>
	</channel>
</rss>
