<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>mob's dev blog</title>
    <link>http://blog.mikeobrien.net/</link>
    <description>Our eyes met across the crowded hat store. I, a customer, and she a coquettish haberdasher.</description>
    <image>
      <url>http://blog.mikeobrien.net/images/rss.png</url>
      <title>mob's dev blog</title>
      <link>http://blog.mikeobrien.net/</link>
    </image>
    <language>en-us</language>
    <copyright>Mike O'Brien</copyright>
    <lastBuildDate>Sat, 31 Oct 2009 23:08:51 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>mob@mikeobrien.net</managingEditor>
    <webMaster>mob@mikeobrien.net</webMaster>
    <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/MobsDevBlog" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=020f0d79-a6a1-426f-acab-a68d87aa3259</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,020f0d79-a6a1-426f-acab-a68d87aa3259.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,020f0d79-a6a1-426f-acab-a68d87aa3259.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=020f0d79-a6a1-426f-acab-a68d87aa3259</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you like Jazz/Blues I’d highly recommend picking up <a href="http://en.wikipedia.org/wiki/Allen_Toussaint">Allen
Toussaint</a>’s album <a href="http://www.amazon.com/The-Bright-Mississippi/dp/B0026E9IDS/ref=dm_cd_album_lnk">The
Bright Mississippi</a>. I heard his adaption of <a href="http://en.wikipedia.org/wiki/St._James_Infirmary_Blues">Saint
James Infirmary</a> on a <a href="http://www.shawu.edu/wsha/Listen_Live/Listen_Live.aspx">local
blues station</a> and was hooked. 
</p>
        <center>
          <a href="http://www.amazon.com/The-Bright-Mississippi/dp/B0026E9IDS/ref=dm_cd_album_lnk">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/AllenToussaintTheBrightMississippi_10CE4/image_3.png" width="238" height="237" />
          </a>
        </center>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=020f0d79-a6a1-426f-acab-a68d87aa3259" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/U6WKBfpBZw8" height="1" width="1" /></body>
      <title>Allen Toussaint - The Bright Mississippi</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,020f0d79-a6a1-426f-acab-a68d87aa3259.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/U6WKBfpBZw8/PermaLink,guid,020f0d79-a6a1-426f-acab-a68d87aa3259.aspx</link>
      <pubDate>Sat, 31 Oct 2009 23:08:51 GMT</pubDate>
      <description>&lt;p&gt;
If you like Jazz/Blues I’d highly recommend picking up &lt;a href="http://en.wikipedia.org/wiki/Allen_Toussaint"&gt;Allen
Toussaint&lt;/a&gt;’s album &lt;a href="http://www.amazon.com/The-Bright-Mississippi/dp/B0026E9IDS/ref=dm_cd_album_lnk"&gt;The
Bright Mississippi&lt;/a&gt;. I heard his adaption of &lt;a href="http://en.wikipedia.org/wiki/St._James_Infirmary_Blues"&gt;Saint
James Infirmary&lt;/a&gt; on a &lt;a href="http://www.shawu.edu/wsha/Listen_Live/Listen_Live.aspx"&gt;local
blues station&lt;/a&gt; and was hooked. 
&lt;/p&gt;
&lt;center&gt;&lt;a href="http://www.amazon.com/The-Bright-Mississippi/dp/B0026E9IDS/ref=dm_cd_album_lnk"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/AllenToussaintTheBrightMississippi_10CE4/image_3.png" width="238" height="237" /&gt;&lt;/a&gt;
&lt;/center&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=020f0d79-a6a1-426f-acab-a68d87aa3259" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,020f0d79-a6a1-426f-acab-a68d87aa3259.aspx</comments>
      <category>Yada Yada Yada</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,020f0d79-a6a1-426f-acab-a68d87aa3259.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=d0cf220f-bd47-49f7-b9da-3201975ad79f</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,d0cf220f-bd47-49f7-b9da-3201975ad79f.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,d0cf220f-bd47-49f7-b9da-3201975ad79f.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=d0cf220f-bd47-49f7-b9da-3201975ad79f</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I just changed the domain of my site and I wanted to create a page notifying that
the site has moved and then auto redirect them after a few seconds. Here’s how I did
it:
</p>
        <p>
1) I’m using IIS 7.5 so I opted to use the <a href="http://learn.iis.net/page.aspx/460/using-url-rewrite-module/">IIS7
RewriteModule</a>. I simply ran the installer and the applet appears under the site
options.
</p>
        <p>
2) I created a catch all site in IIS and bound it to all the old public domains:
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_thumb.png" width="488" height="227" />
          </a>
        </p>
        <p>
3) Then created a UrlRewrite Rule to rewrite all requests to /Default.aspx (Except
ones specifically to /Default.aspx):
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_thumb_1.png" width="488" height="480" />
          </a>
        </p>
        <p>
4) Next created the redirection page (Stripped down for clarity):
</p>
        <pre class="code">
          <span style="background: #ffee62">&lt;%</span>
          <span style="color: blue">@ </span>
          <span style="color: #a31515">Page </span>
          <span style="color: red">Language</span>
          <span style="color: blue">="C#" </span>
          <span style="background: #ffee62">%&gt;
&lt;% </span>
          <span style="color: blue">string </span>redirectUrl = <span style="color: blue">string</span>.Format(<span style="color: #a31515">"{0}://{1}{2}{3}"</span>,
Request.Url.Scheme, Request.Url.Host.Replace(<span style="color: #a31515">"mikeobrien.net"</span>, <span style="color: #a31515">"mikeobrien.net"</span>),
(Request.Url.Port != 80 ? <span style="color: #a31515">":" </span>+ Request.Url.Port
: <span style="color: blue">string</span>.Empty), Request.Headers[<span style="color: #a31515">"X-Original-URL"</span>]); <span style="background: #ffee62">%&gt; </span><span style="color: blue">&lt;</span><span style="color: #a31515">html</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">head</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">title</span><span style="color: blue">&gt;</span>We've
Moved<span style="color: blue">&lt;/</span><span style="color: #a31515">title</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">script </span><span style="color: red">language</span><span style="color: blue">="javascript"&gt;
function </span>RedirectPage(url, seconds) { self.setTimeout(<span style="color: #a31515">'self.location.href
= \'' </span>+ url + <span style="color: #a31515">'\';'</span>, seconds * 1000); } <span style="color: blue">function </span>CountDown(seconds,
elementId) { <span style="color: blue">if </span>(seconds == 0) <span style="color: blue">return</span>;
document.getElementById(elementId).innerHTML = seconds; self.setTimeout(<span style="color: #a31515">'CountDown(' </span>+
(seconds - 1) + <span style="color: #a31515">', \'' </span>+ elementId + <span style="color: #a31515">'\');'</span>,
1000); } <span style="color: blue">&lt;/</span><span style="color: #a31515">script</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">head</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">body </span><span style="color: red">onLoad</span><span style="color: blue">="RedirectPage('</span><span style="background: #ffee62">&lt;%</span> =
redirectUrl <span style="background: #ffee62">%&gt;</span><span style="color: blue">',
5);CountDown(5, 'timeLeft');"&gt; &lt;</span><span style="color: #a31515">h3</span><span style="color: blue">&gt;</span>We've
moved!<span style="color: blue">&lt;/</span><span style="color: #a31515">h3</span><span style="color: blue">&gt;
&lt;</span><span style="color: #a31515">p</span><span style="color: blue">&gt; </span>The
page you requested can now be found <span style="color: blue">&lt;</span><span style="color: #a31515">a </span><span style="color: red">href</span><span style="color: blue">="</span><span style="background: #ffee62">&lt;%</span> =
redirectUrl <span style="background: #ffee62">%&gt;</span><span style="color: blue">"&gt;</span>here<span style="color: blue">&lt;/</span><span style="color: #a31515">a</span><span style="color: blue">&gt;</span>.
You will be redirected in <span style="color: blue">&lt;</span><span style="color: #a31515">span </span><span style="color: red">id</span><span style="color: blue">="timeLeft"&gt;</span>5<span style="color: blue">&lt;/</span><span style="color: #a31515">span</span><span style="color: blue">&gt; </span>seconds. <span style="color: blue">&lt;/</span><span style="color: #a31515">p</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">body</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">html</span><span style="color: blue">&gt;</span></pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>5) And voila!
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_6.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_thumb_2.png" width="550" height="201" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=d0cf220f-bd47-49f7-b9da-3201975ad79f" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/e7ipBYMmz1A" height="1" width="1" /></body>
      <title>Creating a Catch-all Redirect Page with Countdown</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,d0cf220f-bd47-49f7-b9da-3201975ad79f.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/e7ipBYMmz1A/PermaLink,guid,d0cf220f-bd47-49f7-b9da-3201975ad79f.aspx</link>
      <pubDate>Wed, 21 Oct 2009 00:23:22 GMT</pubDate>
      <description>&lt;p&gt;
I just changed the domain of my site and I wanted to create a page notifying that
the site has moved and then auto redirect them after a few seconds. Here’s how I did
it:
&lt;/p&gt;
&lt;p&gt;
1) I’m using IIS 7.5 so I opted to use the &lt;a href="http://learn.iis.net/page.aspx/460/using-url-rewrite-module/"&gt;IIS7
RewriteModule&lt;/a&gt;. I simply ran the installer and the applet appears under the site
options.
&lt;/p&gt;
&lt;p&gt;
2) I created a catch all site in IIS and bound it to all the old public domains:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_thumb.png" width="488" height="227" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
3) Then created a UrlRewrite Rule to rewrite all requests to /Default.aspx (Except
ones specifically to /Default.aspx):
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_thumb_1.png" width="488" height="480" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
4) Next created the redirection page (Stripped down for clarity):
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;@ &lt;/span&gt;&lt;span style="color: #a31515"&gt;Page &lt;/span&gt;&lt;span style="color: red"&gt;Language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;C#&amp;quot; &lt;/span&gt;&lt;span style="background: #ffee62"&gt;%&amp;gt;
&amp;lt;% &lt;/span&gt;&lt;span style="color: blue"&gt;string &lt;/span&gt;redirectUrl = &lt;span style="color: blue"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #a31515"&gt;&amp;quot;{0}://{1}{2}{3}&amp;quot;&lt;/span&gt;,
Request.Url.Scheme, Request.Url.Host.Replace(&lt;span style="color: #a31515"&gt;&amp;quot;mikeobrien.net&amp;quot;&lt;/span&gt;, &lt;span style="color: #a31515"&gt;&amp;quot;mikeobrien.net&amp;quot;&lt;/span&gt;),
(Request.Url.Port != 80 ? &lt;span style="color: #a31515"&gt;&amp;quot;:&amp;quot; &lt;/span&gt;+ Request.Url.Port
: &lt;span style="color: blue"&gt;string&lt;/span&gt;.Empty), Request.Headers[&lt;span style="color: #a31515"&gt;&amp;quot;X-Original-URL&amp;quot;&lt;/span&gt;]); &lt;span style="background: #ffee62"&gt;%&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;We've
Moved&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;title&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;script &lt;/span&gt;&lt;span style="color: red"&gt;language&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;javascript&amp;quot;&amp;gt;
function &lt;/span&gt;RedirectPage(url, seconds) { self.setTimeout(&lt;span style="color: #a31515"&gt;'self.location.href
= \'' &lt;/span&gt;+ url + &lt;span style="color: #a31515"&gt;'\';'&lt;/span&gt;, seconds * 1000); } &lt;span style="color: blue"&gt;function &lt;/span&gt;CountDown(seconds,
elementId) { &lt;span style="color: blue"&gt;if &lt;/span&gt;(seconds == 0) &lt;span style="color: blue"&gt;return&lt;/span&gt;;
document.getElementById(elementId).innerHTML = seconds; self.setTimeout(&lt;span style="color: #a31515"&gt;'CountDown(' &lt;/span&gt;+
(seconds - 1) + &lt;span style="color: #a31515"&gt;', \'' &lt;/span&gt;+ elementId + &lt;span style="color: #a31515"&gt;'\');'&lt;/span&gt;,
1000); } &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;script&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;head&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;body &lt;/span&gt;&lt;span style="color: red"&gt;onLoad&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;RedirectPage('&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; =
redirectUrl &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;',
5);CountDown(5, 'timeLeft');&amp;quot;&amp;gt; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;h3&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;We've
moved!&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;h3&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;p&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;The
page you requested can now be found &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;a &lt;/span&gt;&lt;span style="color: red"&gt;href&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="background: #ffee62"&gt;&amp;lt;%&lt;/span&gt; =
redirectUrl &lt;span style="background: #ffee62"&gt;%&amp;gt;&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;quot;&amp;gt;&lt;/span&gt;here&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;a&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;.
You will be redirected in &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;span &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;timeLeft&amp;quot;&amp;gt;&lt;/span&gt;5&lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;span&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;seconds. &lt;span style="color: blue"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;p&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;body&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;html&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;5) And voila!
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/CreatingaCatchallRedirectPage_11E8D/image_thumb_2.png" width="550" height="201" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=d0cf220f-bd47-49f7-b9da-3201975ad79f" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,d0cf220f-bd47-49f7-b9da-3201975ad79f.aspx</comments>
      <category>C#</category>
      <category>IIS 7</category>
      <category>IIS7.5</category>
      <category>JavaScript</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,d0cf220f-bd47-49f7-b9da-3201975ad79f.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=7d74579b-8eff-4907-8bae-2294495cb016</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,7d74579b-8eff-4907-8bae-2294495cb016.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,7d74579b-8eff-4907-8bae-2294495cb016.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=7d74579b-8eff-4907-8bae-2294495cb016</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Thanks to Michael O’Brien of Akron Ohio for letting mikeobrien.net expire! I am now
able to drop the stupid hyphen in my domain name. If you are one of the 60 odd people
that subscribe to my blog feed (Thanks for subscribing BTW) you should be ok since
I use Feed Burner and I’ll update the new url there. If you have my old email address
it will be exactly the same just without the hyphen. 
</p>
        <p>
Life without the hyphen, it’s much better than I could have ever imagined.
</p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=7d74579b-8eff-4907-8bae-2294495cb016" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/-lvBus-FIpM" height="1" width="1" /></body>
      <title>Goodbye Hyphen!</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,7d74579b-8eff-4907-8bae-2294495cb016.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/-lvBus-FIpM/PermaLink,guid,7d74579b-8eff-4907-8bae-2294495cb016.aspx</link>
      <pubDate>Tue, 20 Oct 2009 15:23:31 GMT</pubDate>
      <description>&lt;p&gt;
Thanks to Michael O’Brien of Akron Ohio for letting mikeobrien.net expire! I am now
able to drop the stupid hyphen in my domain name. If you are one of the 60 odd people
that subscribe to my blog feed (Thanks for subscribing BTW) you should be ok since
I use Feed Burner and I’ll update the new url there. If you have my old email address
it will be exactly the same just without the hyphen. 
&lt;/p&gt;
&lt;p&gt;
Life without the hyphen, it’s much better than I could have ever imagined.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=7d74579b-8eff-4907-8bae-2294495cb016" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,7d74579b-8eff-4907-8bae-2294495cb016.aspx</comments>
      <category>Yada Yada Yada</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,7d74579b-8eff-4907-8bae-2294495cb016.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=fbe9743c-6229-4f9e-8888-95ec0feea793</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,fbe9743c-6229-4f9e-8888-95ec0feea793.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,fbe9743c-6229-4f9e-8888-95ec0feea793.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=fbe9743c-6229-4f9e-8888-95ec0feea793</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
F# October CTP for VS2008 <a href="http://blogs.msdn.com/dsyme/archive/2009/10/19/visual-studio-2010-beta2-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx">announcement</a> &amp; <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=b55f0532-ac3c-4106-918c-5586a953a7da&amp;displaylang=en">download</a>.
</p>
        <p>
VS2010 Beta2 <a href="http://www.hanselman.com/blog/VisualStudio2010Beta2.aspx">announcement</a> (Download
is available for MSDN subscribers).
</p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=fbe9743c-6229-4f9e-8888-95ec0feea793" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/maYsuNvYCuU" height="1" width="1" /></body>
      <title>F# October CTP for VS2008 &amp; VS2010 Beta2 is out</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,fbe9743c-6229-4f9e-8888-95ec0feea793.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/maYsuNvYCuU/PermaLink,guid,fbe9743c-6229-4f9e-8888-95ec0feea793.aspx</link>
      <pubDate>Tue, 20 Oct 2009 01:27:32 GMT</pubDate>
      <description>&lt;p&gt;
F# October CTP for VS2008 &lt;a href="http://blogs.msdn.com/dsyme/archive/2009/10/19/visual-studio-2010-beta2-with-f-is-now-available-plus-matching-f-ctp-update-for-vs2008.aspx"&gt;announcement&lt;/a&gt; &amp;amp; &lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=b55f0532-ac3c-4106-918c-5586a953a7da&amp;amp;displaylang=en"&gt;download&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
VS2010 Beta2 &lt;a href="http://www.hanselman.com/blog/VisualStudio2010Beta2.aspx"&gt;announcement&lt;/a&gt; (Download
is available for MSDN subscribers).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=fbe9743c-6229-4f9e-8888-95ec0feea793" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,fbe9743c-6229-4f9e-8888-95ec0feea793.aspx</comments>
      <category>.NET 4.0</category>
      <category>F#</category>
      <category>Visual Studio 2010</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,fbe9743c-6229-4f9e-8888-95ec0feea793.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=9ad560d7-5a4e-46e8-91f5-cdafcd0fba67</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,9ad560d7-5a4e-46e8-91f5-cdafcd0fba67.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,9ad560d7-5a4e-46e8-91f5-cdafcd0fba67.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=9ad560d7-5a4e-46e8-91f5-cdafcd0fba67</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’m working on an application where I’m sending output to a console window. If the
output is an integer or double I wanted the print function to perform special formatting.
For everything else just call .ToString(). Page 115 in Expert F# shows an example
of how to do this sort of thing with pattern matching, here is an example (Stripped
down for clarity):
</p>
        <pre class="code">
          <span style="color: blue">let </span>Print (t:obj) = <span style="color: blue">let </span>text
= <span style="color: blue">match </span>t <span style="color: blue">with </span>|
:? int <span style="color: blue">-&gt; </span>(t :?&gt; int).ToString(<span style="color: maroon">"#,###"</span>)
| :? double <span style="color: blue">-&gt; </span>(t :?&gt; double).ToString(<span style="color: maroon">"#,###.0"</span>)
| _ <span style="color: blue">-&gt; </span>t.ToString() <span style="color: blue">...</span> Console.Write(text)</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
A few points:
</p>
        <ol>
          <li>
The compiler complained that I didn’t have a type annotation on the parameter. Not
sure why this is but book shows that annotating it as an obj does the trick. 
</li>
          <li>
The :? &lt;type&gt; construct looks at the type of the value. 
</li>
          <li>
The :?&gt; operator casts the value down to a particular type. 
</li>
        </ol>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=9ad560d7-5a4e-46e8-91f5-cdafcd0fba67" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/tV0brgeMCAA" height="1" width="1" /></body>
      <title>Type Tests with Pattern Matching in F#</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,9ad560d7-5a4e-46e8-91f5-cdafcd0fba67.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/tV0brgeMCAA/PermaLink,guid,9ad560d7-5a4e-46e8-91f5-cdafcd0fba67.aspx</link>
      <pubDate>Sun, 11 Oct 2009 17:39:53 GMT</pubDate>
      <description>&lt;p&gt;
I’m working on an application where I’m sending output to a console window. If the
output is an integer or double I wanted the print function to perform special formatting.
For everything else just call .ToString(). Page 115 in Expert F# shows an example
of how to do this sort of thing with pattern matching, here is an example (Stripped
down for clarity):
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;Print (t:obj) = &lt;span style="color: blue"&gt;let &lt;/span&gt;text
= &lt;span style="color: blue"&gt;match &lt;/span&gt;t &lt;span style="color: blue"&gt;with &lt;/span&gt;|
:? int &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(t :?&amp;gt; int).ToString(&lt;span style="color: maroon"&gt;&amp;quot;#,###&amp;quot;&lt;/span&gt;)
| :? double &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;(t :?&amp;gt; double).ToString(&lt;span style="color: maroon"&gt;&amp;quot;#,###.0&amp;quot;&lt;/span&gt;)
| _ &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;t.ToString() &lt;span style="color: blue"&gt;...&lt;/span&gt; Console.Write(text)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
A few points:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
The compiler complained that I didn’t have a type annotation on the parameter. Not
sure why this is but book shows that annotating it as an obj does the trick. 
&lt;/li&gt;
&lt;li&gt;
The :? &amp;lt;type&amp;gt; construct looks at the type of the value. 
&lt;/li&gt;
&lt;li&gt;
The :?&amp;gt; operator casts the value down to a particular type. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=9ad560d7-5a4e-46e8-91f5-cdafcd0fba67" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,9ad560d7-5a4e-46e8-91f5-cdafcd0fba67.aspx</comments>
      <category>.NET</category>
      <category>F#</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,9ad560d7-5a4e-46e8-91f5-cdafcd0fba67.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=e80dadba-04e9-4bbb-a261-8b791a69c1a2</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,e80dadba-04e9-4bbb-a261-8b791a69c1a2.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,e80dadba-04e9-4bbb-a261-8b791a69c1a2.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=e80dadba-04e9-4bbb-a261-8b791a69c1a2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
We have a number of Windows Services in our infrastructure, all of which have a frequency
in their configuration. Originally this frequency was in milliseconds but this is
a real pain since when you set or read the configuration you are always doing the
math between milliseconds and hours/minutes/seconds. Then is occurred that we could
probably use a TimeSpan in the configuration to represent frequency instead of an
integer of milliseconds. The .NET configuration API has a built in converter for the
TimeSpan class so all you have to do is set the configuration property type to TimeSpan
and your good to go!
</p>
        <p>
Here is the configuration class:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">Section </span>: <span style="color: #2b91af">ConfigurationSection </span>{ <span style="color: blue">private
const string </span>FREQUENCY = <span style="color: #a31515">"frequency"</span>;
[<span style="color: #2b91af">ConfigurationProperty</span>(FREQUENCY)] <span style="color: blue">public </span><span style="color: #2b91af">TimeSpan </span>Frequency
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>(<span style="color: #2b91af">TimeSpan</span>)<span style="color: blue">this</span>[FREQUENCY];
} } }</pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>Here is the configuration, much easier
to read/set:
</p>
        <pre class="code">
          <span style="color: blue">&lt;</span>
          <span style="color: #a31515">company</span>
          <span style="color: blue">&gt; </span>
          <span style="color: blue"> &lt;</span>
          <span style="color: #a31515">services</span>
          <span style="color: blue">&gt;
&lt;</span>
          <span style="color: #a31515">someService </span>
          <span style="color: red">frequency</span>
          <span style="color: blue">=</span>"<span style="color: blue">00:00:30</span>"<span style="color: blue">/&gt;
&lt;/</span><span style="color: #a31515">services</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">company</span><span style="color: blue">&gt;</span></pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
And the usage: 
</p>
        <pre class="code">
          <span style="color: blue">private </span>Common.Timers.<span style="color: #2b91af">Timer </span>_processTimer
= <span style="color: blue">new </span>Common.Timers.<span style="color: #2b91af">Timer</span>(
Configuration.<span style="color: #2b91af">Manager</span>.Current.SomeService.Frequency.TotalMilliseconds,
Common.Timers.<span style="color: #2b91af">Timer</span>.<span style="color: #2b91af">TimerElapseStartMode</span>.Immediate,
Common.Timers.<span style="color: #2b91af">Timer</span>.<span style="color: #2b91af">TimerElapseReentranceMode</span>.NonReentrant);</pre>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=e80dadba-04e9-4bbb-a261-8b791a69c1a2" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/En5gKlmr0aU" height="1" width="1" /></body>
      <title>Using a TimeSpan to Represent a Frequency in Configuration</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,e80dadba-04e9-4bbb-a261-8b791a69c1a2.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/En5gKlmr0aU/PermaLink,guid,e80dadba-04e9-4bbb-a261-8b791a69c1a2.aspx</link>
      <pubDate>Sat, 10 Oct 2009 14:33:27 GMT</pubDate>
      <description>&lt;p&gt;
We have a number of Windows Services in our infrastructure, all of which have a frequency
in their configuration. Originally this frequency was in milliseconds but this is
a real pain since when you set or read the configuration you are always doing the
math between milliseconds and hours/minutes/seconds. Then is occurred that we could
probably use a TimeSpan in the configuration to represent frequency instead of an
integer of milliseconds. The .NET configuration API has a built in converter for the
TimeSpan class so all you have to do is set the configuration property type to TimeSpan
and your good to go!
&lt;/p&gt;
&lt;p&gt;
Here is the configuration class:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Section &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;ConfigurationSection &lt;/span&gt;{ &lt;span style="color: blue"&gt;private
const string &lt;/span&gt;FREQUENCY = &lt;span style="color: #a31515"&gt;&amp;quot;frequency&amp;quot;&lt;/span&gt;;
[&lt;span style="color: #2b91af"&gt;ConfigurationProperty&lt;/span&gt;(FREQUENCY)] &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimeSpan &lt;/span&gt;Frequency
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;TimeSpan&lt;/span&gt;)&lt;span style="color: blue"&gt;this&lt;/span&gt;[FREQUENCY];
} } }&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Here is the configuration, much easier
to read/set:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;company&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt; &lt;/span&gt;&lt;span style="color: blue"&gt; &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;someService &lt;/span&gt;&lt;span style="color: red"&gt;frequency&lt;/span&gt;&lt;span style="color: blue"&gt;=&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;00:00:30&lt;/span&gt;&amp;quot;&lt;span style="color: blue"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;services&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;company&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
And the usage: 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;Common.Timers.&lt;span style="color: #2b91af"&gt;Timer &lt;/span&gt;_processTimer
= &lt;span style="color: blue"&gt;new &lt;/span&gt;Common.Timers.&lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;(
Configuration.&lt;span style="color: #2b91af"&gt;Manager&lt;/span&gt;.Current.SomeService.Frequency.TotalMilliseconds,
Common.Timers.&lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;TimerElapseStartMode&lt;/span&gt;.Immediate,
Common.Timers.&lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode&lt;/span&gt;.NonReentrant);&lt;/pre&gt;&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=e80dadba-04e9-4bbb-a261-8b791a69c1a2" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,e80dadba-04e9-4bbb-a261-8b791a69c1a2.aspx</comments>
      <category>.NET</category>
      <category>C#</category>
      <category>Configuration</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,e80dadba-04e9-4bbb-a261-8b791a69c1a2.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=98d49a6f-c0fa-401f-8930-29d0ff801405</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,98d49a6f-c0fa-401f-8930-29d0ff801405.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,98d49a6f-c0fa-401f-8930-29d0ff801405.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=98d49a6f-c0fa-401f-8930-29d0ff801405</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I needed a timer that I could set to elapse immediately when it was enabled (Instead
of waiting for the first elapse event) and also not be reentrant on the elapsed event.
I couldn’t seem to find anything out there so I rolled my own. Unfortunately the System.Timers.Timer
doesn't offer a lot in the way of extension so I ended up just using composition over
inheritance. Here is a quick test:
</p>
        <pre class="code">
          <span style="color: blue">static </span>
          <span style="color: #2b91af">Timer </span>_timer
= <span style="color: blue">new </span><span style="color: #2b91af">Timer</span>(5000, <span style="color: #2b91af">Timer</span>.<span style="color: #2b91af">TimerElapseStartMode</span>.Immediate, <span style="color: #2b91af">Timer</span>.<span style="color: #2b91af">TimerElapseReentranceMode</span>.NonReentrant); <span style="color: blue">static
void </span>Main(<span style="color: blue">string</span>[] args) { <span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">"Started
@ {0}\r\n-----------------------------"</span>, <span style="color: #2b91af">DateTime</span>.Now);
_timer.Elapsed += _timer_Elapsed; _timer.Start(); <span style="color: #2b91af">Application</span>.Run();
} <span style="color: blue">static void </span>_timer_Elapsed(<span style="color: blue">object </span>sender, <span style="color: #2b91af">Timer</span>.<span style="color: #2b91af">ElapsedEventArgs </span>e)
{ <span style="color: #2b91af">Console</span>.WriteLine(<span style="color: #a31515">"Elapsed
@ {0}"</span>, e.SignalTime); <span style="color: #2b91af">Thread</span>.Sleep(10000);
}</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
Here is the implementation:
</p>
        <pre class="code">
          <span style="color: blue">public class </span>
          <span style="color: #2b91af">Timer </span>{ <span style="color: green">//
────────────────────────── Enumerations ────────────────────────── </span><span style="color: blue">public
enum </span><span style="color: #2b91af">TimerElapseStartMode </span>{ Immediate,
AfterInterval } <span style="color: blue">public enum </span><span style="color: #2b91af">TimerElapseReentranceMode </span>{
Reentrant, NonReentrant } <span style="color: green">// ──────────────────────────
Events ────────────────────────── </span><span style="color: blue">public delegate
void </span><span style="color: #2b91af">ElapsedEventHandler</span>(<span style="color: blue">object </span>sender, <span style="color: #2b91af">ElapsedEventArgs </span>e); <span style="color: blue">public
event </span><span style="color: #2b91af">ElapsedEventHandler </span>Elapsed; <span style="color: green">//
────────────────────────── Private Fields ────────────────────────── </span><span style="color: blue">private </span>System.Timers.<span style="color: #2b91af">Timer </span>_timer
= <span style="color: blue">new </span>System.Timers.<span style="color: #2b91af">Timer</span>(); <span style="color: blue">private
int </span>_executing; <span style="color: green">// ────────────────────────── Constructor
────────────────────────── </span><span style="color: blue">public </span>Timer(<span style="color: blue">double </span>interval)
: <span style="color: blue">this</span>( interval, <span style="color: #2b91af">TimerElapseStartMode</span>.AfterInterval, <span style="color: #2b91af">TimerElapseReentranceMode</span>.Reentrant)
{ } <span style="color: blue">public </span>Timer( <span style="color: blue">double </span>interval, <span style="color: #2b91af">TimerElapseStartMode </span>startMode, <span style="color: #2b91af">TimerElapseReentranceMode </span>reentranceMode)
{ _timer = <span style="color: blue">new </span>System.Timers.<span style="color: #2b91af">Timer</span>(interval);
_timer.Elapsed += OnElapsed; ElapseStartMode = startMode; ElapseReentranceMode = reentranceMode;
} <span style="color: green">// ────────────────────────── Public Members ────────────────────────── </span><span style="color: blue">public
bool </span>AutoReset { <span style="color: blue">get </span>{ <span style="color: blue">return </span>_timer.AutoReset;
} <span style="color: blue">set </span>{ _timer.AutoReset = <span style="color: blue">value</span>;
} } <span style="color: blue">public bool </span>Enabled { <span style="color: blue">get </span>{ <span style="color: blue">return </span>_timer.Enabled;
} <span style="color: blue">set </span>{ _timer.Enabled = <span style="color: blue">value</span>;
} } <span style="color: blue">public double </span>Interval { <span style="color: blue">get </span>{ <span style="color: blue">return </span>_timer.Interval;
} <span style="color: blue">set </span>{ _timer.Interval = <span style="color: blue">value</span>;
} } <span style="color: blue">public </span><span style="color: #2b91af">TimerElapseStartMode </span>ElapseStartMode
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } <span style="color: blue">public </span><span style="color: #2b91af">TimerElapseReentranceMode </span>ElapseReentranceMode
{ <span style="color: blue">get</span>; <span style="color: blue">set</span>; } <span style="color: blue">public
void </span>Start() { <span style="color: blue">if </span>(ElapseStartMode == <span style="color: #2b91af">TimerElapseStartMode</span>.Immediate) <span style="color: #2b91af">ThreadPool</span>.QueueUserWorkItem(
state =&gt; Elapse(<span style="color: blue">new </span><span style="color: #2b91af">ElapsedEventArgs</span>()));
Enabled = <span style="color: blue">true</span>; } <span style="color: blue">public
void </span>BeginInit() { _timer.BeginInit(); } <span style="color: blue">public void </span>Close()
{ _timer.Close(); } <span style="color: blue">public void </span>EndInit() { _timer.EndInit();
} <span style="color: blue">public void </span>Stop() { Enabled = <span style="color: blue">false</span>;
} <span style="color: green">// ────────────────────────── Private Members ────────────────────────── </span><span style="color: blue">private
void </span>OnElapsed(<span style="color: blue">object </span>sender, System.Timers.<span style="color: #2b91af">ElapsedEventArgs </span>e)
{ Elapse(<span style="color: blue">new </span><span style="color: #2b91af">ElapsedEventArgs</span>(e));
} <span style="color: blue">private void </span>Elapse(<span style="color: #2b91af">ElapsedEventArgs </span>args)
{ <span style="color: blue">if </span>(ElapseReentranceMode == <span style="color: #2b91af">TimerElapseReentranceMode</span>.NonReentrant
&amp;&amp; <span style="color: #2b91af">Interlocked</span>.CompareExchange(<span style="color: blue">ref </span>_executing,
1, 0) == 1) <span style="color: blue">return</span>; <span style="color: blue">if </span>(Elapsed
!= <span style="color: blue">null</span>) Elapsed(<span style="color: blue">this</span>,
args); _executing = 0; } <span style="color: green">// ──────────────────────────
Nested Types ────────────────────────── </span><span style="color: blue">public class </span><span style="color: #2b91af">ElapsedEventArgs </span>: <span style="color: #2b91af">EventArgs </span>{ <span style="color: blue">private </span><span style="color: #2b91af">DateTime </span>_signalTime; <span style="color: blue">public </span>ElapsedEventArgs()
{ _signalTime = <span style="color: #2b91af">DateTime</span>.Now; } <span style="color: blue">public </span>ElapsedEventArgs(System.Timers.<span style="color: #2b91af">ElapsedEventArgs </span>args)
{ _signalTime = args.SignalTime; } <span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>SignalTime
{ <span style="color: blue">get </span>{ <span style="color: blue">return </span>_signalTime;
} } } }</pre>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=98d49a6f-c0fa-401f-8930-29d0ff801405" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/LO_66Vphb8k" height="1" width="1" /></body>
      <title>Timer With Elapse Reentrance and Start Options</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,98d49a6f-c0fa-401f-8930-29d0ff801405.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/LO_66Vphb8k/PermaLink,guid,98d49a6f-c0fa-401f-8930-29d0ff801405.aspx</link>
      <pubDate>Thu, 08 Oct 2009 21:39:44 GMT</pubDate>
      <description>&lt;p&gt;
I needed a timer that I could set to elapse immediately when it was enabled (Instead
of waiting for the first elapse event) and also not be reentrant on the elapsed event.
I couldn’t seem to find anything out there so I rolled my own. Unfortunately the System.Timers.Timer
doesn't offer a lot in the way of extension so I ended up just using composition over
inheritance. Here is a quick test:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;static &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Timer &lt;/span&gt;_timer
= &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;(5000, &lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;TimerElapseStartMode&lt;/span&gt;.Immediate, &lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode&lt;/span&gt;.NonReentrant); &lt;span style="color: blue"&gt;static
void &lt;/span&gt;Main(&lt;span style="color: blue"&gt;string&lt;/span&gt;[] args) { &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine( &lt;span style="color: #a31515"&gt;&amp;quot;Started
@ {0}\r\n-----------------------------&amp;quot;&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now);
_timer.Elapsed += _timer_Elapsed; _timer.Start(); &lt;span style="color: #2b91af"&gt;Application&lt;/span&gt;.Run();
} &lt;span style="color: blue"&gt;static void &lt;/span&gt;_timer_Elapsed(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;.&lt;span style="color: #2b91af"&gt;ElapsedEventArgs &lt;/span&gt;e)
{ &lt;span style="color: #2b91af"&gt;Console&lt;/span&gt;.WriteLine(&lt;span style="color: #a31515"&gt;&amp;quot;Elapsed
@ {0}&amp;quot;&lt;/span&gt;, e.SignalTime); &lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(10000);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
Here is the implementation:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Timer &lt;/span&gt;{ &lt;span style="color: green"&gt;//
────────────────────────── Enumerations ────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;public
enum &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimerElapseStartMode &lt;/span&gt;{ Immediate,
AfterInterval } &lt;span style="color: blue"&gt;public enum &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode &lt;/span&gt;{
Reentrant, NonReentrant } &lt;span style="color: green"&gt;// ──────────────────────────
Events ────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;public delegate
void &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ElapsedEventHandler&lt;/span&gt;(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;ElapsedEventArgs &lt;/span&gt;e); &lt;span style="color: blue"&gt;public
event &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ElapsedEventHandler &lt;/span&gt;Elapsed; &lt;span style="color: green"&gt;//
────────────────────────── Private Fields ────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;System.Timers.&lt;span style="color: #2b91af"&gt;Timer &lt;/span&gt;_timer
= &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Timers.&lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;(); &lt;span style="color: blue"&gt;private
int &lt;/span&gt;_executing; &lt;span style="color: green"&gt;// ────────────────────────── Constructor
────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;Timer(&lt;span style="color: blue"&gt;double &lt;/span&gt;interval)
: &lt;span style="color: blue"&gt;this&lt;/span&gt;( interval, &lt;span style="color: #2b91af"&gt;TimerElapseStartMode&lt;/span&gt;.AfterInterval, &lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode&lt;/span&gt;.Reentrant)
{ } &lt;span style="color: blue"&gt;public &lt;/span&gt;Timer( &lt;span style="color: blue"&gt;double &lt;/span&gt;interval, &lt;span style="color: #2b91af"&gt;TimerElapseStartMode &lt;/span&gt;startMode, &lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode &lt;/span&gt;reentranceMode)
{ _timer = &lt;span style="color: blue"&gt;new &lt;/span&gt;System.Timers.&lt;span style="color: #2b91af"&gt;Timer&lt;/span&gt;(interval);
_timer.Elapsed += OnElapsed; ElapseStartMode = startMode; ElapseReentranceMode = reentranceMode;
} &lt;span style="color: green"&gt;// ────────────────────────── Public Members ────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;public
bool &lt;/span&gt;AutoReset { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_timer.AutoReset;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _timer.AutoReset = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
} } &lt;span style="color: blue"&gt;public bool &lt;/span&gt;Enabled { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_timer.Enabled;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _timer.Enabled = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
} } &lt;span style="color: blue"&gt;public double &lt;/span&gt;Interval { &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_timer.Interval;
} &lt;span style="color: blue"&gt;set &lt;/span&gt;{ _timer.Interval = &lt;span style="color: blue"&gt;value&lt;/span&gt;;
} } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimerElapseStartMode &lt;/span&gt;ElapseStartMode
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode &lt;/span&gt;ElapseReentranceMode
{ &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; } &lt;span style="color: blue"&gt;public
void &lt;/span&gt;Start() { &lt;span style="color: blue"&gt;if &lt;/span&gt;(ElapseStartMode == &lt;span style="color: #2b91af"&gt;TimerElapseStartMode&lt;/span&gt;.Immediate) &lt;span style="color: #2b91af"&gt;ThreadPool&lt;/span&gt;.QueueUserWorkItem(
state =&amp;gt; Elapse(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ElapsedEventArgs&lt;/span&gt;()));
Enabled = &lt;span style="color: blue"&gt;true&lt;/span&gt;; } &lt;span style="color: blue"&gt;public
void &lt;/span&gt;BeginInit() { _timer.BeginInit(); } &lt;span style="color: blue"&gt;public void &lt;/span&gt;Close()
{ _timer.Close(); } &lt;span style="color: blue"&gt;public void &lt;/span&gt;EndInit() { _timer.EndInit();
} &lt;span style="color: blue"&gt;public void &lt;/span&gt;Stop() { Enabled = &lt;span style="color: blue"&gt;false&lt;/span&gt;;
} &lt;span style="color: green"&gt;// ────────────────────────── Private Members ────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;private
void &lt;/span&gt;OnElapsed(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, System.Timers.&lt;span style="color: #2b91af"&gt;ElapsedEventArgs &lt;/span&gt;e)
{ Elapse(&lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ElapsedEventArgs&lt;/span&gt;(e));
} &lt;span style="color: blue"&gt;private void &lt;/span&gt;Elapse(&lt;span style="color: #2b91af"&gt;ElapsedEventArgs &lt;/span&gt;args)
{ &lt;span style="color: blue"&gt;if &lt;/span&gt;(ElapseReentranceMode == &lt;span style="color: #2b91af"&gt;TimerElapseReentranceMode&lt;/span&gt;.NonReentrant
&amp;amp;&amp;amp; &lt;span style="color: #2b91af"&gt;Interlocked&lt;/span&gt;.CompareExchange(&lt;span style="color: blue"&gt;ref &lt;/span&gt;_executing,
1, 0) == 1) &lt;span style="color: blue"&gt;return&lt;/span&gt;; &lt;span style="color: blue"&gt;if &lt;/span&gt;(Elapsed
!= &lt;span style="color: blue"&gt;null&lt;/span&gt;) Elapsed(&lt;span style="color: blue"&gt;this&lt;/span&gt;,
args); _executing = 0; } &lt;span style="color: green"&gt;// ──────────────────────────
Nested Types ────────────────────────── &lt;/span&gt;&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ElapsedEventArgs &lt;/span&gt;: &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;{ &lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;_signalTime; &lt;span style="color: blue"&gt;public &lt;/span&gt;ElapsedEventArgs()
{ _signalTime = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now; } &lt;span style="color: blue"&gt;public &lt;/span&gt;ElapsedEventArgs(System.Timers.&lt;span style="color: #2b91af"&gt;ElapsedEventArgs &lt;/span&gt;args)
{ _signalTime = args.SignalTime; } &lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;SignalTime
{ &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return &lt;/span&gt;_signalTime;
} } } }&lt;/pre&gt;&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=98d49a6f-c0fa-401f-8930-29d0ff801405" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,98d49a6f-c0fa-401f-8930-29d0ff801405.aspx</comments>
      <category>C#</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,98d49a6f-c0fa-401f-8930-29d0ff801405.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=07b81afe-e688-499c-a42a-22bb852ae5d0</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,07b81afe-e688-499c-a42a-22bb852ae5d0.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,07b81afe-e688-499c-a42a-22bb852ae5d0.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=07b81afe-e688-499c-a42a-22bb852ae5d0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I didn't really understand this until today when <a href="http://cs.hubfs.net/forums/thread/11952.aspx">I
asked the following on hubFS</a>:
</p>
        <p>
          <span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span">
            <span style="text-align: left; font-family: tahoma, arial, helvetica; font-size: 12px" class="Apple-style-span">
              <em>“I'm
wondering if the F# compiler has an optimization that will make it so that the sqrt5
and Phi functions only execute once, no matter how many times the fibn function is
called, since the results of those functions are always constant.</em>
            </span>
          </span>
        </p>
        <div style="font-family: 'Courier New'; background: white; color: black; font-size: 10pt; -webkit-background-clip: initial; -webkit-background-origin: initial">
          <pre style="margin: 0px">
            <span style="color: blue">let</span> sqrt5 = sqrt 5.0</pre>
          <pre style="margin: 0px">
            <span style="color: blue">let</span> Phi = (1.0 + sqrt5)
/ 2.0</pre>
          <pre style="margin: 0px">
            <span style="color: blue">let</span> fibn n = Phi ** float
n / sqrt5 |&gt; round |&gt; int</pre>
          <pre style="margin: 0px">
            <span style="color: blue">let</span> fib2 = Seq.unfold (<span style="color: blue">fun</span> index <span style="color: blue">-&gt;</span> Some
(fibn index,index + 1)) 1</pre>
        </div>
        <p>
          <span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span">
            <span style="text-align: left; font-family: tahoma, arial, helvetica; font-size: 12px" class="Apple-style-span">
              <em>I'm
trying to figure out if the compiler automagically makes this happen or if I have
to make it happen.”</em>
            </span>
          </span>
        </p>
        <p>
          <a href="http://cs.hubfs.net/members/kvb.aspx">Keith</a> kindly broke it down (Thanks
Keith!):
</p>
        <p>
          <span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium 'Times New Roman'; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span">
            <span style="text-align: left; font-family: tahoma, arial, helvetica; font-size: 12px" class="Apple-style-span">
              <em>“In
the code that you've written, sqrt5 and Phi are not functions but values (they don't
take any arguments).  As such, their definitions will never be re-evaluated…
For top level definitions, something is a value if it doesn't take parameters. 
For instance, given the two definitions:</em>
              <br />
              <br />
              <span style="background-color: transparent; font-family: 'Lucida Console'; color: black; font-size: 11px; font-weight: normal">
                <span style="background-color: transparent; font-family: 'Lucida Console'; color: blue; font-size: 11px; font-weight: normal">let</span>
                <span class="Apple-converted-space"> </span>now
= System.DateTime.Now 
<br /><span style="background-color: transparent; font-family: 'Lucida Console'; color: blue; font-size: 11px; font-weight: normal">let</span><span class="Apple-converted-space"> </span>add
x y = x + y</span>
              <br />
              <br />
              <em>"now" is a value, and will never change.  add is a function taking
two arguments, and each time both arguments are supplied, the definition on the right
will be evaluated to give the return value.</em>
              <br />
              <br />
              <em>However, when defining .NET classes, things become more complicated, because classes
can contain fields, methods, events, properties, etc.  Properties are re-evaluated
whenever they are gotten, and DateTime.Now is a property, which is why the value appears
to change over time. 
<br /><br />
Things can also be a bit confusing when using functions in a first-class way. 
For instance: 
<br /></em>
              <br />
              <span style="background-color: transparent; font-family: 'Lucida Console'; color: black; font-size: 11px; font-weight: normal">
                <span style="background-color: transparent; font-family: 'Lucida Console'; color: blue; font-size: 11px; font-weight: normal">let</span>
                <span class="Apple-converted-space"> </span>myFun
= 
<br />
 <span class="Apple-converted-space"> </span><span style="background-color: transparent; font-family: 'Lucida Console'; color: blue; font-size: 11px; font-weight: normal">let</span><span class="Apple-converted-space"> </span>time
= System.DateTime.Now 
<br />
 <span class="Apple-converted-space"> </span><span style="background-color: transparent; font-family: 'Lucida Console'; color: blue; font-size: 11px; font-weight: normal">fun</span><span class="Apple-converted-space"> </span>()
-&gt; System.DateTime.Now - time</span>
              <br />
              <br />
              <em>Here, myFun is a value (which happens to be a function!), and its definition is
only evaluated once (so, in particular, "time" is only ever set once, when
myFun is first defined).  However, calling the myFun function then results in
the evaluation of the function's body (that is, the right hand side of the last line
of myFun's definition, which calls System.DateTime.Now and subtracts the original
time).”</em>
            </span>
          </span>
        </p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=07b81afe-e688-499c-a42a-22bb852ae5d0" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/_u__qP8JZ5E" height="1" width="1" /></body>
      <title>F# Function or Value?</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,07b81afe-e688-499c-a42a-22bb852ae5d0.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/_u__qP8JZ5E/PermaLink,guid,07b81afe-e688-499c-a42a-22bb852ae5d0.aspx</link>
      <pubDate>Wed, 07 Oct 2009 22:50:43 GMT</pubDate>
      <description>&lt;p&gt;
I didn't really understand this until today when &lt;a href="http://cs.hubfs.net/forums/thread/11952.aspx"&gt;I
asked the following on hubFS&lt;/a&gt;:
&lt;/p&gt;
&lt;p&gt;
&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; font-family: tahoma, arial, helvetica; font-size: 12px" class="Apple-style-span"&gt;&lt;em&gt;“I'm
wondering if the F# compiler has an optimization that will make it so that the sqrt5
and Phi functions only execute once, no matter how many times the fibn function is
called, since the results of those functions are always constant.&lt;/em&gt;&lt;/span&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;div style="font-family: &amp;#39;Courier New&amp;#39;; background: white; color: black; font-size: 10pt; -webkit-background-clip: initial; -webkit-background-origin: initial"&gt;
&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; sqrt5 = sqrt 5.0&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; Phi = (1.0 + sqrt5)
/ 2.0&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; fibn n = Phi ** float
n / sqrt5 |&amp;gt; round |&amp;gt; int&lt;/pre&gt;
&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;let&lt;/span&gt; fib2 = Seq.unfold (&lt;span style="color: blue"&gt;fun&lt;/span&gt; index &lt;span style="color: blue"&gt;-&amp;gt;&lt;/span&gt; Some
(fibn index,index + 1)) 1&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;
&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; font-family: tahoma, arial, helvetica; font-size: 12px" class="Apple-style-span"&gt;&lt;em&gt;I'm
trying to figure out if the compiler automagically makes this happen or if I have
to make it happen.”&lt;/em&gt;&lt;/span&gt; &lt;/span&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://cs.hubfs.net/members/kvb.aspx"&gt;Keith&lt;/a&gt; kindly broke it down (Thanks
Keith!):
&lt;/p&gt;
&lt;p&gt;
&lt;span style="widows: 2; text-transform: none; text-indent: 0px; border-collapse: separate; font: medium &amp;#39;Times New Roman&amp;#39;; white-space: normal; orphans: 2; letter-spacing: normal; color: rgb(0,0,0); word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px" class="Apple-style-span"&gt;&lt;span style="text-align: left; font-family: tahoma, arial, helvetica; font-size: 12px" class="Apple-style-span"&gt;&lt;em&gt;“In
the code that you've written, sqrt5 and Phi are not functions but values (they don't
take any arguments).&amp;#160; As such, their definitions will never be re-evaluated…
For top level definitions, something is a value if it doesn't take parameters.&amp;#160;
For instance, given the two definitions:&lt;/em&gt; 
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: black; font-size: 11px; font-weight: normal"&gt;&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: blue; font-size: 11px; font-weight: normal"&gt;let&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;now
= System.DateTime.Now 
&lt;br /&gt;
&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: blue; font-size: 11px; font-weight: normal"&gt;let&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;add
x y = x + y&lt;/span&gt; 
&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;&amp;quot;now&amp;quot; is a value, and will never change.&amp;#160; add is a function taking
two arguments, and each time both arguments are supplied, the definition on the right
will be evaluated to give the return value.&lt;/em&gt; 
&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;However, when defining .NET classes, things become more complicated, because classes
can contain fields, methods, events, properties, etc.&amp;#160; Properties are re-evaluated
whenever they are gotten, and DateTime.Now is a property, which is why the value appears
to change over time. 
&lt;br /&gt;
&lt;br /&gt;
Things can also be a bit confusing when using functions in a first-class way.&amp;#160;
For instance: 
&lt;br /&gt;
&lt;/em&gt; 
&lt;br /&gt;
&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: black; font-size: 11px; font-weight: normal"&gt;&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: blue; font-size: 11px; font-weight: normal"&gt;let&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;myFun
= 
&lt;br /&gt;
&amp;#160;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: blue; font-size: 11px; font-weight: normal"&gt;let&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;time
= System.DateTime.Now 
&lt;br /&gt;
&amp;#160;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;&lt;span style="background-color: transparent; font-family: &amp;#39;Lucida Console&amp;#39;; color: blue; font-size: 11px; font-weight: normal"&gt;fun&lt;/span&gt;&lt;span class="Apple-converted-space"&gt;&amp;#160;&lt;/span&gt;()
-&amp;gt; System.DateTime.Now - time&lt;/span&gt; 
&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;Here, myFun is a value (which happens to be a function!), and its definition is
only evaluated once (so, in particular, &amp;quot;time&amp;quot; is only ever set once, when
myFun is first defined).&amp;#160; However, calling the myFun function then results in
the evaluation of the function's body (that is, the right hand side of the last line
of myFun's definition, which calls System.DateTime.Now and subtracts the original
time).”&lt;/em&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=07b81afe-e688-499c-a42a-22bb852ae5d0" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,07b81afe-e688-499c-a42a-22bb852ae5d0.aspx</comments>
      <category>F#</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,07b81afe-e688-499c-a42a-22bb852ae5d0.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=ef5afe0e-bc06-4989-8514-df2495be71b1</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,ef5afe0e-bc06-4989-8514-df2495be71b1.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,ef5afe0e-bc06-4989-8514-df2495be71b1.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=ef5afe0e-bc06-4989-8514-df2495be71b1</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I’m running a DBCC SHRINKFILE on a HUGE database (315Gb) after a major prune (Around
60%) and it’s taking forever. Anyways, found a helpful nugget on the internets for
determining it’s progress.
</p>
        <pre class="code">
          <span style="color: blue">SELECT </span>percent_complete<span style="color: gray">,
* </span><span style="color: blue">FROM </span><span style="color: green">sys.dm_exec_requests</span></pre>
        <p>
          <a href="http://11011.net/software/vspaste">
          </a>And you get something like this:
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/GettingtheProgressofaDBCCSHRINKFILE_A5E8/image_2.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/GettingtheProgressofaDBCCSHRINKFILE_A5E8/image_thumb.png" width="589" height="91" />
          </a>
        </p>
        <p>
I’ve noticed that even though it says “suspended” it’s still progressing. I think
it must be that this task is constantly firing off child tasks (Like #194 above) and
waiting for them to complete (And thus the status of “suspended” most of the time).
</p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=ef5afe0e-bc06-4989-8514-df2495be71b1" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/YOI0SizkN68" height="1" width="1" /></body>
      <title>Getting the Progress of a DBCC SHRINKFILE</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,ef5afe0e-bc06-4989-8514-df2495be71b1.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/YOI0SizkN68/PermaLink,guid,ef5afe0e-bc06-4989-8514-df2495be71b1.aspx</link>
      <pubDate>Mon, 05 Oct 2009 15:50:15 GMT</pubDate>
      <description>&lt;p&gt;
I’m running a DBCC SHRINKFILE on a HUGE database (315Gb) after a major prune (Around
60%) and it’s taking forever. Anyways, found a helpful nugget on the internets for
determining it’s progress.
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;SELECT &lt;/span&gt;percent_complete&lt;span style="color: gray"&gt;,
* &lt;/span&gt;&lt;span style="color: blue"&gt;FROM &lt;/span&gt;&lt;span style="color: green"&gt;sys.dm_exec_requests&lt;/span&gt;&lt;/pre&gt;
&lt;p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;And you get something like this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/GettingtheProgressofaDBCCSHRINKFILE_A5E8/image_2.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/GettingtheProgressofaDBCCSHRINKFILE_A5E8/image_thumb.png" width="589" height="91" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
I’ve noticed that even though it says “suspended” it’s still progressing. I think
it must be that this task is constantly firing off child tasks (Like #194 above) and
waiting for them to complete (And thus the status of “suspended” most of the time).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=ef5afe0e-bc06-4989-8514-df2495be71b1" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,ef5afe0e-bc06-4989-8514-df2495be71b1.aspx</comments>
      <category>TSQL/SQL Server</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,ef5afe0e-bc06-4989-8514-df2495be71b1.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=5884a3ef-8460-4f4d-88ad-c4bca60a3558</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,5884a3ef-8460-4f4d-88ad-c4bca60a3558.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,5884a3ef-8460-4f4d-88ad-c4bca60a3558.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=5884a3ef-8460-4f4d-88ad-c4bca60a3558</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://projecteuler.net/">Project Euler</a> rocks! I think it’s an especially
good way to learn another programming language. I’m planning to try to solve the problems
in 2 ways. First, if possible, the brute force method (Which I think helps you to
learn the language better) and second, if possible, the formula method (Which I think
helps you learn the mathematical concepts better). 
</p>
        <p>
          <a href="http://projecteuler.net/index.php?section=problems&amp;id=2">Problem #2</a> states:
</p>
        <font color="#008000" size="2">
          <font color="#008000" size="2">
            <p>
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
</p>
            <p>
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
</p>
            <p>
Find the sum of all the even-valued terms in the sequence which do not exceed four
million.
</p>
          </font>
        </font>
        <p>
          <strong>Solution 1</strong>
        </p>
        <p>
So the first approach is using the brute force method where we iterate through all
the numbers and determine the next Fibonacci number by adding the last two. I used
the Seq.unfold method to do this (Which I discuss further <a href="http://blog.mikeobrien.net/PermaLink,guid,b1850d19-190a-40f0-b624-0f2daa2cbf30.aspx">here</a>).
Short and sweet:
</p>
        <pre class="code">
          <span style="color: blue">let </span>max = 4000000 <span style="color: blue">let </span>fib
= Seq.unfold (<span style="color: blue">fun </span>(last, current) <span style="color: blue">-&gt; </span>Some
(last, (current, current + last))) (1, 1) <span style="color: blue">let </span>Run1
_ = fib |&gt; Seq.takeWhile (<span style="color: blue">fun </span>i <span style="color: blue">-&gt; </span>i
&lt;= max) |&gt; Seq.filter (<span style="color: blue">fun </span>i <span style="color: blue">-&gt; </span>i
% 2 = 0) |&gt; Seq.fold(<span style="color: blue">fun </span>state item <span style="color: blue">-&gt; </span>state
+ item) 0 |&gt; Display.PrintInteger</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <strong>Solution 2</strong>
        </p>
        <p>
The second approach actually calculates each Fibonacci number using the <a href="http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html">Binet
formula</a>. Now this is still pretty much brute force in that we still have to iterate
from 0 to the max and include only the positive values, but at least we’re using a
formula that doesn't require knowledge of the previous values in the sequence. <a href="http://www.computing.surrey.ac.uk/personal/ext/R.Knott/contactron.html">Ron
Knott</a> has a <a href="http://www.computing.surrey.ac.uk/personal/ext/R.Knott/Fibonacci/fibFormula.html">nice
page on calculating Fibonacci numbers</a> and a “reduced” Binet for calculating positive
integers.  The reduced Binet formula is <font color="#0000a0" size="2" face="Courier New">round(φ<sup>n</sup> /
√5)</font>. The rounding is required because of the irrational numbers in the equation.
Theoretically the result would be an integer but because of limits on the number of
digits we can calculate, the result will be slightly off and the rounding fixes this.
I discuss calculating φ (Or the Golden Ratio) in my <a href="http://blog.mikeobrien.net/PermaLink,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx">last
post</a>. Check that out to get a better understanding of how we calculate it. 
</p>
        <pre class="code">
          <span style="color: blue">let </span>max = 4000000 <span style="color: blue">let </span>sqrt5
= sqrt 5.0 <span style="color: blue">let </span>Phi = (1.0 + sqrt5) / 2.0 <span style="color: blue">let </span>fibn
n = Phi ** float n / sqrt5 |&gt; round |&gt; int <span style="color: blue">let </span>fib2
= Seq.unfold (<span style="color: blue">fun </span>index <span style="color: blue">-&gt; </span>Some
(fibn index,index + 1)) 1 <span style="color: blue">let </span>Run2 _ = fib2 |&gt;
Seq.takeWhile (<span style="color: blue">fun </span>i <span style="color: blue">-&gt; </span>i
&lt;= max) |&gt; Seq.filter (<span style="color: blue">fun </span>i <span style="color: blue">-&gt; </span>i
% 2 = 0) |&gt; Seq.fold(<span style="color: blue">fun </span>state item <span style="color: blue">-&gt; </span>state
+ item) 0 |&gt; Display.PrintInteger</pre>
        <a href="http://11011.net/software/vspaste">
        </a>
        <p>
          <strong>Results</strong>
        </p>
        <p>
          <font color="#ff0000">EDIT:</font> After the fact I realized that my original results
were skewed by the JIT compiler. Anyways, made some modifications to take this out
of the equation and got more accurate results. As you can see in this case that the
heavier calculations in solution 2 don’t buy us any performance since we have to iterate
through and find the positive numbers starting from 0 anyways. So solution 1 was the
best solution in this case. However if we needed to target a specific range of Fibonacci
numbers (Especially a high range) the second solution would most definitely win out.  
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/SolvingProjectEulerProblem2inF_12E23/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/SolvingProjectEulerProblem2inF_12E23/image_thumb_1.png" width="677" height="342" />
          </a>
        </p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=5884a3ef-8460-4f4d-88ad-c4bca60a3558" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/gFQ2-Ch25a0" height="1" width="1" /></body>
      <title>Solving Project Euler Problem #2 in F#</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,5884a3ef-8460-4f4d-88ad-c4bca60a3558.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/gFQ2-Ch25a0/PermaLink,guid,5884a3ef-8460-4f4d-88ad-c4bca60a3558.aspx</link>
      <pubDate>Sun, 04 Oct 2009 01:31:26 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://projecteuler.net/"&gt;Project Euler&lt;/a&gt; rocks! I think it’s an especially
good way to learn another programming language. I’m planning to try to solve the problems
in 2 ways. First, if possible, the brute force method (Which I think helps you to
learn the language better) and second, if possible, the formula method (Which I think
helps you learn the mathematical concepts better). 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://projecteuler.net/index.php?section=problems&amp;amp;id=2"&gt;Problem #2&lt;/a&gt; states:
&lt;/p&gt;
&lt;font color="#008000" size="2"&gt;&lt;font color="#008000" size="2"&gt; 
&lt;p&gt;
Each new term in the Fibonacci sequence is generated by adding the previous two terms.
By starting with 1 and 2, the first 10 terms will be:
&lt;/p&gt;
&lt;p&gt;
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
&lt;/p&gt;
&lt;p&gt;
Find the sum of all the even-valued terms in the sequence which do not exceed four
million.
&lt;/p&gt;
&lt;/font&gt;&lt;/font&gt; 
&lt;p&gt;
&lt;strong&gt;Solution 1&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
So the first approach is using the brute force method where we iterate through all
the numbers and determine the next Fibonacci number by adding the last two. I used
the Seq.unfold method to do this (Which I discuss further &lt;a href="http://blog.mikeobrien.net/PermaLink,guid,b1850d19-190a-40f0-b624-0f2daa2cbf30.aspx"&gt;here&lt;/a&gt;).
Short and sweet:
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;max = 4000000 &lt;span style="color: blue"&gt;let &lt;/span&gt;fib
= Seq.unfold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;(last, current) &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Some
(last, (current, current + last))) (1, 1) &lt;span style="color: blue"&gt;let &lt;/span&gt;Run1
_ = fib |&amp;gt; Seq.takeWhile (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;i
&amp;lt;= max) |&amp;gt; Seq.filter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;i
% 2 = 0) |&amp;gt; Seq.fold(&lt;span style="color: blue"&gt;fun &lt;/span&gt;state item &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state
+ item) 0 |&amp;gt; Display.PrintInteger&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Solution 2&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
The second approach actually calculates each Fibonacci number using the &lt;a href="http://mathworld.wolfram.com/BinetsFibonacciNumberFormula.html"&gt;Binet
formula&lt;/a&gt;. Now this is still pretty much brute force in that we still have to iterate
from 0 to the max and include only the positive values, but at least we’re using a
formula that doesn't require knowledge of the previous values in the sequence. &lt;a href="http://www.computing.surrey.ac.uk/personal/ext/R.Knott/contactron.html"&gt;Ron
Knott&lt;/a&gt; has a &lt;a href="http://www.computing.surrey.ac.uk/personal/ext/R.Knott/Fibonacci/fibFormula.html"&gt;nice
page on calculating Fibonacci numbers&lt;/a&gt; and a “reduced” Binet for calculating positive
integers.&amp;#160; The reduced Binet formula is &lt;font color="#0000a0" size="2" face="Courier New"&gt;round(φ&lt;sup&gt;n&lt;/sup&gt; /
√5)&lt;/font&gt;. The rounding is required because of the irrational numbers in the equation.
Theoretically the result would be an integer but because of limits on the number of
digits we can calculate, the result will be slightly off and the rounding fixes this.
I discuss calculating φ (Or the Golden Ratio) in my &lt;a href="http://blog.mikeobrien.net/PermaLink,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx"&gt;last
post&lt;/a&gt;. Check that out to get a better understanding of how we calculate it. 
&lt;/p&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;let &lt;/span&gt;max = 4000000 &lt;span style="color: blue"&gt;let &lt;/span&gt;sqrt5
= sqrt 5.0 &lt;span style="color: blue"&gt;let &lt;/span&gt;Phi = (1.0 + sqrt5) / 2.0 &lt;span style="color: blue"&gt;let &lt;/span&gt;fibn
n = Phi ** float n / sqrt5 |&amp;gt; round |&amp;gt; int &lt;span style="color: blue"&gt;let &lt;/span&gt;fib2
= Seq.unfold (&lt;span style="color: blue"&gt;fun &lt;/span&gt;index &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;Some
(fibn index,index + 1)) 1 &lt;span style="color: blue"&gt;let &lt;/span&gt;Run2 _ = fib2 |&amp;gt;
Seq.takeWhile (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;i
&amp;lt;= max) |&amp;gt; Seq.filter (&lt;span style="color: blue"&gt;fun &lt;/span&gt;i &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;i
% 2 = 0) |&amp;gt; Seq.fold(&lt;span style="color: blue"&gt;fun &lt;/span&gt;state item &lt;span style="color: blue"&gt;-&amp;gt; &lt;/span&gt;state
+ item) 0 |&amp;gt; Display.PrintInteger&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt; 
&lt;p&gt;
&lt;strong&gt;Results&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#ff0000"&gt;EDIT:&lt;/font&gt; After the fact I realized that my original results
were skewed by the JIT compiler. Anyways, made some modifications to take this out
of the equation and got more accurate results. As you can see in this case that the
heavier calculations in solution 2 don’t buy us any performance since we have to iterate
through and find the positive numbers starting from 0 anyways. So solution 1 was the
best solution in this case. However if we needed to target a specific range of Fibonacci
numbers (Especially a high range) the second solution would most definitely win out.&amp;#160; 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/SolvingProjectEulerProblem2inF_12E23/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/SolvingProjectEulerProblem2inF_12E23/image_thumb_1.png" width="677" height="342" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=5884a3ef-8460-4f4d-88ad-c4bca60a3558" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,5884a3ef-8460-4f4d-88ad-c4bca60a3558.aspx</comments>
      <category>Algebra</category>
      <category>F#</category>
      <category>Math</category>
      <category>Project Euler</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,5884a3ef-8460-4f4d-88ad-c4bca60a3558.aspx</feedburner:origLink></item>
    <item>
      <trackback:ping>http://blog.mikeobrien.net/Trackback.aspx?guid=34135b3d-c540-4641-834d-9bd9047e9fb5</trackback:ping>
      <pingback:server>http://blog.mikeobrien.net/pingback.aspx</pingback:server>
      <pingback:target>http://blog.mikeobrien.net/PermaLink,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx</pingback:target>
      <dc:creator>mob</dc:creator>
      <wfw:comment>http://blog.mikeobrien.net/CommentView,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx</wfw:comment>
      <wfw:commentRss>http://blog.mikeobrien.net/SyndicationService.asmx/GetEntryCommentsRss?guid=34135b3d-c540-4641-834d-9bd9047e9fb5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Patrick McKeague has an <a href="http://www.youtube.com/watch?v=-ncEEXekZek">excellent
video</a> explaining the math behind the <a href="http://en.wikipedia.org/wiki/Golden_rectangle">golden
rectangle</a>. It also explains how to derive the formula for the <a href="http://en.wikipedia.org/wiki/Golden_ratio">golden
ratio</a>. 
</p>
        <p>
First we start with a square like so:
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_4.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_1.png" width="132" height="132" />
          </a>
        </p>
        <p>
Mark halfway between one of the sides and draw a line from this point to one of the
opposite corners. This forms a right triangle. 
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_6.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_2.png" width="132" height="143" />
          </a>
        </p>
        <p>
Now if you were to swing the hypotenuse of this right triangle down to zero degrees
of the side of the origin (maintaining the same point of origin) and extend the square
to the termination of this line you would get a golden rectangle. 
</p>
        <p>
          <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_22.png">
            <img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_10.png" width="184" height="143" />
          </a>                    <a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_20.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_9.png" width="184" height="143" /></a></p>
        <p>
The golden ratio would be the length of this rectangle divided it’s width . We know
the width is 2. The length is made up of half of the side of the original square which
is 1, plus the hypotenuse of the triangle that was formed above. Using the <a href="http://en.wikipedia.org/wiki/Pythagorean_theorem">Pythagorean
Theorem</a> (a² + b² = c²) we see that the hypotenuse is:
</p>
        <pre>1² + 2² = c² 
1 + 4 = c² 
c² = 5 
√c² = √5 
c = √5</pre>
        <p>
So the length is 1 + √5. We can then see that the formula for the golden ration is:
</p>
        <pre>1 + √5 
——————  =  φ
   2 </pre>
        <p>
Now the cool thing is if you create a square of <em>any size</em> and follow the above
procedure, you will always be able to reduce the equation down to the one above. 
</p>
        <img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=34135b3d-c540-4641-834d-9bd9047e9fb5" />
      <xhtml:img xmlns:xhtml="http://www.w3.org/1999/xhtml" src="http://feeds.feedburner.com/~r/MobsDevBlog/~4/0dRi92umRI4" height="1" width="1" /></body>
      <title>Determining the Golden Ratio</title>
      <guid isPermaLink="false">http://blog.mikeobrien.net/PermaLink,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx</guid>
      <link>http://feedproxy.google.com/~r/MobsDevBlog/~3/0dRi92umRI4/PermaLink,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx</link>
      <pubDate>Sat, 03 Oct 2009 15:42:35 GMT</pubDate>
      <description>&lt;p&gt;
Patrick McKeague has an &lt;a href="http://www.youtube.com/watch?v=-ncEEXekZek"&gt;excellent
video&lt;/a&gt; explaining the math behind the &lt;a href="http://en.wikipedia.org/wiki/Golden_rectangle"&gt;golden
rectangle&lt;/a&gt;. It also explains how to derive the formula for the &lt;a href="http://en.wikipedia.org/wiki/Golden_ratio"&gt;golden
ratio&lt;/a&gt;. 
&lt;/p&gt;
&lt;p&gt;
First we start with a square like so:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_4.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_1.png" width="132" height="132" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Mark halfway between one of the sides and draw a line from this point to one of the
opposite corners. This forms a right triangle. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_6.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_2.png" width="132" height="143" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
Now if you were to swing the hypotenuse of this right triangle down to zero degrees
of the side of the origin (maintaining the same point of origin) and extend the square
to the termination of this line you would get a golden rectangle. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_22.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_10.png" width="184" height="143" /&gt;&lt;/a&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;a href="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_20.png"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="image" border="0" alt="image" src="http://blog.mikeobrien.net/content/binary/WindowsLiveWriter/DeterminingtheGoldenRatio_8DEC/image_thumb_9.png" width="184" height="143" /&gt;&lt;/a&gt; 
&lt;/p&gt;
&lt;p&gt;
The golden ratio would be the length of this rectangle divided it’s width . We know
the width is 2. The length is made up of half of the side of the original square which
is 1, plus the hypotenuse of the triangle that was formed above. Using the &lt;a href="http://en.wikipedia.org/wiki/Pythagorean_theorem"&gt;Pythagorean
Theorem&lt;/a&gt; (a² + b² = c²) we see that the hypotenuse is:
&lt;/p&gt;
&lt;pre&gt;1² + 2² = c² 
1 + 4 = c² 
c² = 5 
√c² = √5 
c = √5&lt;/pre&gt;
&lt;p&gt;
So the length is 1 + √5. We can then see that the formula for the golden ration is:
&lt;/p&gt;
&lt;pre&gt;1 + √5 
——————  =  φ
   2 &lt;/pre&gt;
&lt;p&gt;
Now the cool thing is if you create a square of &lt;em&gt;any size&lt;/em&gt; and follow the above
procedure, you will always be able to reduce the equation down to the one above. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blog.mikeobrien.net/aggbug.ashx?id=34135b3d-c540-4641-834d-9bd9047e9fb5" /&gt;</description>
      <comments>http://blog.mikeobrien.net/CommentView,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx</comments>
      <category>Algebra</category>
      <category>Math</category>
    <feedburner:origLink>http://blog.mikeobrien.net/PermaLink,guid,34135b3d-c540-4641-834d-9bd9047e9fb5.aspx</feedburner:origLink></item>
  </channel>
</rss>
