<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ChalamiuS&#039; Blog &#187; jisakujien</title>
	<atom:link href="http://blog.chalamius.se/author/jisakujien/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.chalamius.se</link>
	<description>Yet another random blog on the internet</description>
	<lastBuildDate>Fri, 30 Sep 2011 01:46:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>C#: Lambda Expressions</title>
		<link>http://blog.chalamius.se/2010/01/c-lambda-expressions</link>
		<comments>http://blog.chalamius.se/2010/01/c-lambda-expressions#comments</comments>
		<pubDate>Fri, 08 Jan 2010 22:47:05 +0000</pubDate>
		<dc:creator>jisakujien</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://blog.chalamius.se/?p=183</guid>
		<description><![CDATA[As a belated follow up to my post on C# Anonymous Methods, let&#8217;s look at a closely related feature of the CLS: lambda expressions.  Then let&#8217;s use a lambda expression to improve the code example from that earlier article. Lambda &#8230; <a href="http://blog.chalamius.se/2010/01/c-lambda-expressions">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://static.chalamius.se/wp-content/uploads/2010/01/LambdaTransparent.png" rel="lightbox[183]"><img class="alignright size-full wp-image-189" title="Lambda" src="http://static.chalamius.se/wp-content/uploads/2010/01/LambdaTransparent.png" alt="" width="168" height="168" /></a>As a belated follow up to my post on <a title="C#: Anonymous Methods" href="http://blog.chalamius.se/jisakujien/c-anonymous-methods" target="_blank">C# Anonymous Methods</a>, let&#8217;s look at a closely related feature of the <a href="http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx">CLS</a>: <a title="Lamda Expressions (C# Programming Guide) - MSDN" href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">lambda expressions</a>.  Then let&#8217;s use a lambda expression to improve the code example from that earlier article.<span id="more-183"></span></p>
<p>Lambda Expressions were introduced in the 3.0 version of the Common Language Specification.  Lambdas are essentially a refinement of anonymous methods.  They allow you to define a block of code which operates on one or more input variables and returns a single result.  Let&#8217;s break down a simple lambda example, from <a title="Lamda Expressions (C# Programming Guide) - MSDN" href="http://msdn.microsoft.com/en-us/library/bb397687.aspx" target="_blank">the MSDN page on Lambda Expressions</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">delegate</span> <span style="color: #6666cc; font-weight: bold;">int</span> del<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    del myDelegate <span style="color: #008000;">=</span> x <span style="color: #008000;">=&gt;</span> x <span style="color: #008000;">*</span> x<span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> myDelegate<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">//j = 25</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In this example, a delegate type is defined, <tt>del</tt>.   Then an instance of the delegate is created using a lambda expression.   Finally, the delegate is executed, and the return value is assigned to the integer variable <tt>j</tt>.   Let&#8217;s ignore everything but the lambda expression itself: <tt>x =&gt; x * x</tt>.</p>
<p>The first character of the expression actually defines a variable named <tt>x</tt>, which only exists inside the lambda expression.   In the MSDN example, when the lambda is actually executed, <tt>x</tt> will be assigned the input value of <tt>5</tt>.</p>
<p>The <tt>=&gt;</tt> character sequence tells C# that your lambda expression operates on the variable <tt>x</tt>.   To oversimplify, everything to the left of <tt>=&gt;</tt> is an input variable declaration, and everything on the right is an expression which determines the return value.  In our examples, there will only ever be one input variable.</p>
<p>The <tt>x * x</tt> character sequence is the actual expression, the bit of code that gets executed.   So we could rewrite down the code above to this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">int</span> x <span style="color: #008000;">=</span> <span style="color: #FF0000;">5</span><span style="color: #008000;">;</span>
<span style="color: #6666cc; font-weight: bold;">int</span> j <span style="color: #008000;">=</span> x <span style="color: #008000;">*</span> x<span style="color: #008000;">;</span></pre></div></div>

<p>&#8230;and achieve the same result.</p>
<h3>Filtering Cars with Lambda Expressions</h3>
<p>Returning to our <a title="C#: Anonymous Methods" href="http://blog.chalamius.se/jisakujien/c-anonymous-methods" target="_blank">anonymous methods example</a>, we found that the <a title="List(T).FindAll Method (System.Collections.Generic) - MSDN" href="http://msdn.microsoft.com/en-us/library/fh1w7y8z.aspx" target="_blank"><tt>FindAll</tt></a> method of the <a title="List(T) Class (System.Collections.Generic) - MSDN" href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" target="_blank"><tt>List&lt;&gt;</tt></a> generic class can take a delegate.  The delegate is used to filter which items in the list are returned.</p>
<h4>With Anonymous Methods</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Create a list of cars meeting criteria by calling the FindAll()</span>
    <span style="color: #008080; font-style: italic;">// method of the List returned by CreateCars().</span>
    List cars <span style="color: #008000;">=</span> CreateCars<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FindAll</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span>Car candidate<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Return whether the car matches our criteria.  FindAll()</span>
        <span style="color: #008080; font-style: italic;">// expects our anonymous method to return a boolean, and</span>
        <span style="color: #008080; font-style: italic;">// the compiler makes sure this is the case.</span>
        <span style="color: #0600FF; font-weight: bold;">return</span>
            candidate<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Toyota&quot;</span> <span style="color: #008000;">||</span>
            candidate<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Nissan&quot;</span> <span style="color: #008000;">&amp;&amp;</span>
            candidate<span style="color: #008000;">.</span><span style="color: #0000FF;">Turbocharged</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span>Car car <span style="color: #0600FF; font-weight: bold;">in</span> cars<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>car<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Instead, let&#8217;s replace that anonymous method block with a lambda expression.</p>
<h4>With Lambda Expressions</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    List cars <span style="color: #008000;">=</span> CreateCars<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FindAll</span><span style="color: #008000;">&#40;</span>car <span style="color: #008000;">=&gt;</span>
        car<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Toyota&quot;</span> <span style="color: #008000;">||</span>
        car<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Nissan&quot;</span> <span style="color: #008000;">&amp;&amp;</span>
        car<span style="color: #008000;">.</span><span style="color: #0000FF;">Turbocharged</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">false</span> <span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    car<span style="color: #008000;">.</span><span style="color: #0000FF;">Turbocharged</span> <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Invalid code, &quot;car&quot; does not exist.</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>In this case, our lambda expression creates the variable <tt>car</tt>, and then does the same set of conditional tests as the old example.  Note that the <tt>car</tt> variable only exists in the scope of the lambda expression, just like in the anonymous method.  For that reason, the second to last line of code would be a compiler error.</p>
<h3>So What&#8217;s the Difference?</h3>
<p>For the purposes of our example, not a whole lot:</p>
<ul>
<li>The input variable and return types are <a title="C# Version 3.0 Specification - Implicitly Typed Local Variables - MSDN" href="http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx" target="_blank"><i>inferred</i></a>.  C# is able to figure out what type <tt>car</tt> is by looking at where the lambda is used.   The type is actually defined in the <tt>CreateCars()</tt> method of our earlier example.  C# looks at the return type of <tt>List&lt;Car&gt;</tt> and <i>infers</i> (or deduces) that a lambda expression operating on the <tt>FindAll</tt> method of that <tt>List&lt;Car&gt;</tt> would operate on a single input variable of type <tt>Car</tt>.  Note that in the old anonymous methods example, C# was only able to infer the return type.</li>
<li>The lambda expression does not need curly braces, since it is a basic expression.</li>
<li>The lambda expression does not use the ugly <tt>delegate()</tt> syntax, thanks to the type inference.</li>
</ul>
<p>Of course lambda expressions are a bit more complicated than I let on; they can declare multiple input variables, and they can also contain normal procedural code statements (such as calling a method &#8212; and that does require using curly braces).</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chalamius.se/2010/01/c-lambda-expressions/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C#: Anonymous Methods</title>
		<link>http://blog.chalamius.se/2009/06/c-anonymous-methods</link>
		<comments>http://blog.chalamius.se/2009/06/c-anonymous-methods#comments</comments>
		<pubDate>Tue, 02 Jun 2009 15:54:39 +0000</pubDate>
		<dc:creator>jisakujien</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.chalamius.se/?p=146</guid>
		<description><![CDATA[To continue ChalamiuS&#8217; theme of C# goodness, let&#8217;s explore a feature of C# introduced in version 2 of the CLS: the anonymous method. Along the way we&#8217;ll also encounter Generics and Object Initializers, but they are not the focus of &#8230; <a href="http://blog.chalamius.se/2009/06/c-anonymous-methods">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>To continue ChalamiuS&#8217; theme of C# goodness, let&#8217;s explore a feature of C# introduced in version 2 of the <a href="http://msdn.microsoft.com/en-us/library/12a7a7h3.aspx">CLS</a>: the <a href="http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic8">anonymous method</a>.  Along the way we&#8217;ll also encounter <a href="http://msdn.microsoft.com/en-us/library/512aeb7t(VS.100).aspx">Generics</a> and <a href="http://msdn.microsoft.com/en-us/library/bb308966.aspx#csharp3.0overview_topic13">Object Initializers</a>, but they are not the focus of this post.<br />
<span id="more-146"></span><br />
Anonymous methods and other features of C# 2.0 and 3.0 incorporate paradigms from <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a> and other (relatively) exotic notions.  The full implications of this are and the theory behind it are only minimally covered in this post.  It should also be noted that in C# 3.0, <a href="http://geekswithblogs.net/michelotti/archive/2007/08/15/114702.aspx">lambda expressions were introduced</a>, which <a href="http://blogs.msdn.com/ericlippert/archive/2007/01/10/lambda-expressions-vs-anonymous-methods-part-one.aspx">work similarly to anonymous methods and look a whole lot cooler</a>.</p>
<h3>What is an anonymous method?</h3>
<p>To answer this question, let&#8217;s first define what a named (eg, not anonymous) method is, using Object.ToString() as an example:</p>
<ul>
<li>A method name.</li>
<li>A return type.  Object.ToString, unsurprisingly, returns a string.  Note that void is a type.</li>
<li>A parameter list.  Object.ToString does not take any parameters.</li>
<li>A method body.  The default Object.ToString implementation returns the type name.</li>
</ul>
<p>Without all of these elements, we cannot have a method.  Let&#8217;s similarly analyze the makeup of an anonymous method:</p>
<ul>
<li>A return type.</li>
<li>A parameter list.</li>
<li>A method body.</li>
</ul>
<p>The major difference is, of course, the lack of a method name.  Another significant difference not apparent in our comparison is that the return type of parameter list of an anonymous method is declared in a delegate.  Any number of anonymous methods may conform to the same delegate.  This is a very basic look at anonymous methods.  I suggest you read more by <a href="http://msdn.microsoft.com/en-us/magazine/cc163682.aspx">people who actually know what they&#8217;re talking about</a>.</p>
<h3>Why use an anonymous method?</h3>
<p>There are any number of reasons to use an anonymous method instead of a regular named method, but my favorite is to help organize small blocks of specialized code.  Instead of declaring a very short method and then calling it from only one place in your code, you can put the anonymous method code right inside the method that needs it.  Anonymous methods are also a great way of sharing information between blocks of code without having use classes or complex method declarations.</p>
<p>We must be careful to not declare anonymous methods when the code they contain may be used in more than once place.  While this can be fixed by refactoring the anonymous method into a named method, it&#8217;s best to avoid this pitfall in the first place.</p>
<h3>Wait, what?</h3>
<p>Instead of drowning in <a href="http://en.wikipedia.org/wiki/Typed_lambda_calculus">theory and technical arcana</a>, let&#8217;s see a (highly contrived) example.  In this example, we create a list of cars and print out any car meeting a certain criteria.  The code declaring the Car class and filling the list of cars is at the end of this post.</p>
<h4>Example without anonymous method</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> cars <span style="color: #008000;">=</span> CreateCars<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span>Car car <span style="color: #0600FF; font-weight: bold;">in</span> cars<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>DoesCarMeetCriteria<span style="color: #008000;">&#40;</span>car<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>car<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">bool</span> DoesCarMeetCriteria<span style="color: #008000;">&#40;</span>Car car<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span>
        car<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Toyota&quot;</span> <span style="color: #008000;">||</span>
        car<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Nissan&quot;</span> <span style="color: #008000;">&amp;&amp;</span>
        car<span style="color: #008000;">.</span><span style="color: #0000FF;">Turbocharged</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4>Example with anonymous method</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Create a list of cars meeting criteria by calling the FindAll()</span>
    <span style="color: #008080; font-style: italic;">// method of the List returned by CreateCars().</span>
    List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> cars <span style="color: #008000;">=</span> CreateCars<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">FindAll</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">delegate</span><span style="color: #008000;">&#40;</span>Car candidate<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Return whether the car matches our criteria.  FindAll()</span>
        <span style="color: #008080; font-style: italic;">// expects our anonymous method to return a boolean, and</span>
        <span style="color: #008080; font-style: italic;">// the compiler makes sure this is the case.</span>
        <span style="color: #0600FF; font-weight: bold;">return</span>
            candidate<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Toyota&quot;</span> <span style="color: #008000;">||</span>
            candidate<span style="color: #008000;">.</span><span style="color: #0000FF;">Manufacturer</span> <span style="color: #008000;">==</span> <span style="color: #666666;">&quot;Nissan&quot;</span> <span style="color: #008000;">&amp;&amp;</span>
            candidate<span style="color: #008000;">.</span><span style="color: #0000FF;">Turbocharged</span> <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span>Car car <span style="color: #0600FF; font-weight: bold;">in</span> cars<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>car<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h3>Conclusion</h3>
<p>Which do you prefer?  I like the anonymous method; while the syntax may seem strange it becomes quite natural after using it a few times.  Sadly this example is somewhat weak; using an anonymous method doesn&#8217;t really gain us anything.  However, showing the true power would touch on language design paradigms and the like, which I am avoiding in this post.</p>
<p>Here&#8217;s the rest of the code required to compile and run the example:</p>
<h4>The Car class</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #6666cc; font-weight: bold;">class</span> Car
<span style="color: #008000;">&#123;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">string</span> Manufacturer, Model<span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> Turbocharged<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Format</span><span style="color: #008000;">&#40;</span>
            <span style="color: #666666;">&quot;{0} {1} ({2}Turbo)&quot;</span>, 
            Manufacturer, 
            Model, 
            Turbocharged <span style="color: #008000;">?</span> <span style="color: #666666;">&quot;&quot;</span> <span style="color: #008000;">:</span> <span style="color: #666666;">&quot;Non-&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<h4>Creating the Car list</h4>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">static</span> List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> CreateCars<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span> cars <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span>Car<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    cars<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Manufacturer <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Toyota&quot;</span>,
        Model <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;AE-86 Trueno GT-APEX&quot;</span>,
        Turbocharged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    cars<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Manufacturer <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Toyota&quot;</span>,
        Model <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;MR2&quot;</span>,
        Turbocharged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    cars<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Manufacturer <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Nissan&quot;</span>,
        Model <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;180SX&quot;</span>,
        Turbocharged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// Some models are turbo-charged.</span>
&nbsp;
    cars<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> Car<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        Manufacturer <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Nissan&quot;</span>,
        Model <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;Sileighty&quot;</span>,
        Turbocharged <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span>
    <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> cars<span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.chalamius.se/2009/06/c-anonymous-methods/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mine Mania</title>
		<link>http://blog.chalamius.se/2009/02/mine-mania</link>
		<comments>http://blog.chalamius.se/2009/02/mine-mania#comments</comments>
		<pubDate>Sun, 22 Feb 2009 19:38:35 +0000</pubDate>
		<dc:creator>jisakujien</dc:creator>
				<category><![CDATA[Random]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.chalamius.se/?p=139</guid>
		<description><![CDATA[A fanatic is one who can&#8217;t change his mind and won&#8217;t change the subject. Sir Winston Leonard Spencer-Churchill, KG, OM, CH, TD, FRS Not to let our rodent friends have all the fun mine-sweeping, I&#8217;ve created a basic C++ ncurses &#8230; <a href="http://blog.chalamius.se/2009/02/mine-mania">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3 style="text-align: left;">A fanatic is one who can&#8217;t change his mind and won&#8217;t change the subject.</h3>
<p style="text-align: right;"><em>Sir Winston Leonard Spencer-Churchill, KG, OM, CH, TD, FRS</em></p>
</blockquote>
<p style="text-align: left;">
<p style="text-align: left;"><img class="alignright size-full wp-image-78" title="minesweeper2" src="http://blog.chalamius.se/wp-content/uploads/2009/02/minesweeper2.png" alt="minesweeper2" width="347" height="358" />Not to let our rodent friends have all the fun mine-sweeping, I&#8217;ve created a basic C++ <a title="NCURSES Programming Howto" href="http://tldp.org/HOWTO/NCURSES-Programming-HOWTO/" target="_blank">ncurses</a> minesweeper game.  This diverges some from the theme of Standard Template Library exploration, although it does make use of the <em><a title="max - C++ Reference" href="http://www.cplusplus.com/reference/algorithm/max.html" target="_blank">std::max</a></em> and <em><a title="min - C++ Reference" href="http://www.cplusplus.com/reference/algorithm/min.html" target="_blank">std::min</a></em> functions.</p>
<p style="text-align: left;">
<p><span id="more-139"></span></p>
<p style="text-align: left;">Good data-structures and prototypes are the cornerstone of any program.  Let&#8217;s define ours.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#define SIZE 20</span>
&nbsp;
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">struct</span> _position
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">bool</span> mine<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">bool</span> activated<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span> position<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> <span style="color: #0000ff;">enum</span> _gamestate <span style="color: #008000;">&#123;</span> init, run, finish <span style="color: #008000;">&#125;</span> gamestate<span style="color: #008080;">;</span>
<span style="color: #0000ff;">typedef</span> position minefield<span style="color: #008000;">&#91;</span>SIZE<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>SIZE<span style="color: #008000;">&#93;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> draw_field<span style="color: #008000;">&#40;</span>minefield<span style="color: #000040;">&amp;</span>,<span style="color: #0000ff;">int</span>,<span style="color: #0000ff;">int</span>,gamestate<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">int</span> get_neighbors<span style="color: #008000;">&#40;</span>minefield<span style="color: #000040;">&amp;</span>,<span style="color: #0000ff;">int</span>,<span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p style="text-align: left;">The main loop implements an extremely simple <a title="Finite State Machine - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Finite_state_machine" target="_blank">state machine</a> to control the gameplay stages.  It draws the field and processes user input, managing the current location of the cursor.  <em>std::min</em> and <em>std::max</em> are used to keep the cursor within bounds.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   initscr<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Initialize the ncurses screen.</span>
   raw<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Disable screen buffering.</span>
   noecho<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Do not show user input.</span>
   keypad<span style="color: #008000;">&#40;</span>stdscr, TRUE<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Enable directional keys.</span>
   <span style="color: #0000dd;">srand</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">time</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">NULL</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   minefield field<span style="color: #008080;">;</span>
   gamestate state <span style="color: #000080;">=</span> init<span style="color: #008080;">;</span>
   <span style="color: #0000ff;">int</span> xpos <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span>, ypos <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Cursor position.</span>
&nbsp;
   <span style="color: #0000ff;">while</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      draw_field<span style="color: #008000;">&#40;</span>field, xpos, ypos, state<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      move<span style="color: #008000;">&#40;</span>xpos, ypos<span style="color: #000040;">*</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      refresh<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">char</span> c <span style="color: #000080;">=</span> getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>init <span style="color: #000080;">==</span> state<span style="color: #008000;">&#41;</span>
         state <span style="color: #000080;">=</span> run<span style="color: #008080;">;</span>  <span style="color: #666666;">// We're initialized, advance to next state.</span>
      <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>finish <span style="color: #000080;">==</span> state<span style="color: #008000;">&#41;</span>
         <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span> <span style="color: #666666;">// We've drawn the finish display, exit.</span>
      <span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>c<span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">case</span> KEY_UP<span style="color: #008080;">:</span>
         xpos <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">--</span>xpos, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">case</span> KEY_DOWN<span style="color: #008080;">:</span>
         xpos <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">min</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">++</span>xpos, SIZE<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">case</span> KEY_LEFT<span style="color: #008080;">:</span>
         ypos <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">--</span>ypos, <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">case</span> KEY_RIGHT<span style="color: #008080;">:</span>
         ypos <span style="color: #000080;">=</span> std<span style="color: #008080;">::</span><span style="color: #007788;">min</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">++</span>ypos, SIZE<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">case</span> <span style="color: #FF0000;">' '</span><span style="color: #008080;">:</span>
         <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>field<span style="color: #008000;">&#91;</span>xpos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>ypos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">mine</span> <span style="color: #000080;">==</span> <span style="color: #0000ff;">true</span><span style="color: #008000;">&#41;</span>
         <span style="color: #008000;">&#123;</span> <span style="color: #666666;">// Player hit a mine.</span>
            mvprintw<span style="color: #008000;">&#40;</span>xpos, ypos<span style="color: #000040;">*</span><span style="color: #0000dd;">2</span>, <span style="color: #FF0000;">&quot;*&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            getch<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            state <span style="color: #000080;">=</span> finish<span style="color: #008080;">;</span>
         <span style="color: #008000;">&#125;</span>
         field<span style="color: #008000;">&#91;</span>xpos<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>ypos<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">activated</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">true</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
      <span style="color: #0000ff;">default</span><span style="color: #008080;">:</span>
         state <span style="color: #000080;">=</span> finish<span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   <span style="color: #008000;">&#125;</span>
   endwin<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #666666;">// Clean up ncurses window.</span>
   <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Drawing the field consists of looping through the map and writing the appropriate symbols to the screen.  The ncurses <em>mvprintw</em> function is indispensable for this.  I find it helpful to picture a <a title="Cartesian Coordinate System - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Cartesian_plane" target="_blank">Cartesian plane</a> when looking at this code.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> draw_field<span style="color: #008000;">&#40;</span>minefield <span style="color: #000040;">&amp;</span>field, <span style="color: #0000ff;">int</span> xpos, <span style="color: #0000ff;">int</span> ypos, gamestate state<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> x <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> x<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
   <span style="color: #008000;">&#123;</span>
      <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> y <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span> y <span style="color: #000080;">&lt;</span> SIZE<span style="color: #008080;">;</span> y<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span>
      <span style="color: #008000;">&#123;</span>
         <span style="color: #0000ff;">char</span> sym<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #FF0000;">'.'</span>, <span style="color: #FF0000;">' '</span>, <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>init <span style="color: #000080;">==</span> state<span style="color: #008000;">&#41;</span>
         <span style="color: #008000;">&#123;</span>
            field<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>y<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">mine</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">rand</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #000040;">%</span><span style="color:#800080;">3</span> <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
            field<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>y<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">activated</span> <span style="color: #000080;">=</span> <span style="color: #0000ff;">false</span><span style="color: #008080;">;</span>
         <span style="color: #008000;">&#125;</span>
         <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>finish <span style="color: #000080;">==</span> state<span style="color: #008000;">&#41;</span>
         <span style="color: #008000;">&#123;</span>
            sym<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> field<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>y<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">mine</span> <span style="color: #008080;">?</span> <span style="color: #FF0000;">'*'</span> <span style="color: #008080;">:</span> <span style="color: #FF0000;">' '</span><span style="color: #008080;">;</span>
         <span style="color: #008000;">&#125;</span>
         <span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span>field<span style="color: #008000;">&#91;</span>x<span style="color: #008000;">&#93;</span><span style="color: #008000;">&#91;</span>y<span style="color: #008000;">&#93;</span>.<span style="color: #007788;">activated</span><span style="color: #008000;">&#41;</span>
            <span style="color: #0000dd;">sprintf</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">&amp;</span>sym<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #FF0000;">&quot;%d&quot;</span>, get_neighbors<span style="color: #008000;">&#40;</span>field, x, y<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         mvprintw<span style="color: #008000;">&#40;</span>x, y<span style="color: #000040;">*</span><span style="color: #0000dd;">2</span>, <span style="color: #000040;">&amp;</span>sym<span style="color: #008000;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
      <span style="color: #008000;">&#125;</span>
   <span style="color: #008000;">&#125;</span>
   refresh<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p><img class="size-medium wp-image-61 alignright" title="minesweeper" src="http://blog.chalamius.se/wp-content/uploads/2009/02/minesweeper.png" alt="minesweeper" width="290" height="300" /></p>
<p style="text-align: left;">Notice that the minefield is passed between the functions by <a title="Reference (C++) - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Reference_(C%2B%2B)" target="_blank">reference</a>, indicated by the <em>&amp;</em> prefix in the function prototype (eg, <tt>minefield &amp;field</tt>).  Passing by reference affords us two general advantages: the entire minefield structure is not copied onto the <a title="Call Stack - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Call_stack" target="_blank">callstack</a>, and we can modify the minefield easily inside functions we have passed it to.  In this case, <a title="An Overview of C++ References - Steve Jacobson" href="http://www-cs-students.stanford.edu/~sjac/c-to-cpp-info/references" target="_blank">passing by reference</a> also affords us two advantages over pointers: we do not have to use a pointer dereference (<em>*</em> or <em>-&gt;</em>), and we know the minefield will never be NULL or otherwise invalid, so we don&#8217;t need to add more logic to handle invalid pointers.  Passing by reference, when used appropriately, can result in simpler and more robust programs than those using exclusively pointers.</p>
<p style="text-align: left;">Use the directional keys to navigate the board, space to test for presence of a mine, and any other key to exit.  See the <a rel="attachment wp-att-63" href="http://blog.chalamius.se/wp-content/uploads/2009/02/minesweeper.cpp">full source</a> for the implementation of the <em>get_neighbors</em> function and some other miscellany.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chalamius.se/2009/02/mine-mania/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Exploding Rats and the C++ Standard Vector</title>
		<link>http://blog.chalamius.se/2009/02/exploding-rats-and-the-c-standard-vector</link>
		<comments>http://blog.chalamius.se/2009/02/exploding-rats-and-the-c-standard-vector#comments</comments>
		<pubDate>Sat, 21 Feb 2009 17:12:54 +0000</pubDate>
		<dc:creator>jisakujien</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.chalamius.se/?p=133</guid>
		<description><![CDATA[Rats are small, cheap, easy to maintain and transport. As many are aware, land mines present a significant danger in many war-ravaged countries around the world. Numerous innovative techniques have been developed to clear mine fields, rendering the land once &#8230; <a href="http://blog.chalamius.se/2009/02/exploding-rats-and-the-c-standard-vector">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<blockquote>
<h3>Rats are small, cheap, easy to maintain and transport.</h3>
</blockquote>
<p>
<a href="http://en.wikipedia.org/wiki/HeroRAT"><img class="size-full wp-image-21 alignright" title="A Nose for Danger" src="http://blog.chalamius.se/wp-content/uploads/2009/02/herorat.jpg" alt="Mine-sniffing rats" width="310" height="163" /></a>As many are aware, land mines present a significant danger in many war-ravaged countries around the world.  Numerous innovative techniques have been developed to <a title="Demining - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Demining" target="_blank">clear mine fields</a>, rendering the land once again usable and preventing tragic injuries and deaths.  A Belgian non-profit organization, <a title="A P O P O" href="http://www.apopo.org" target="_blank">APOPO</a>, began a unique program to develop a new method &#8212; using Giant Pouched Rats (<a title="Giant pouched rat - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Giant_pouched_rat" target="_blank"><em><span class="family">Nesomyidae </span></em></a><span class="subfamily"><a title="Giant pouched rat - Wikipedia, the free encyclopedia" href="http://en.wikipedia.org/wiki/Giant_pouched_rat" target="_blank"><em><span class="mw-redirect">Cricetomyinae</span></em></a>) to detect mines &#8212; dubbed <a title="Home | Herorat.org" href="http://www.herorat.org/" target="_blank">HeroRATS</a> in 2004, and has now deployed the intrepid rodents in Mozambique.<br />
</span><br />
The rats are too light to set off mines, so disappointingly they are not destroyed in the process (sorry, I lied).  Instead, the rats are attached to guide lines and trained to scratch at the ground when they sniff out explosives.  The mine is then flagged for removal using more appropriate tools.</p>
<blockquote>
<h3><span dir="ltr">A zebra is a horse designed by a committee.</span></h3>
</blockquote>
<p><span dir="ltr">I&#8217;ll be the first to admit that I am not an expert with the C++ language.  In fact, I can use all the practice I can get.  I&#8217;ve been using the Standard Template Library on and off for almost 3 years now, but know embarrassingly little about it&#8217;s full capabilities.  In that vein, I will (hopefully) be posting a series of snippets and short programs about the STL.  I plan to start simple, and work my way up.  Without further ado, the very basics of the STL vector and it&#8217;s iterator.</span></p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;vector&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
   <span style="color: #666666;">// Define a vector, which is an array-like container of items.</span>
   <span style="color: #666666;">// Unlike std::set, vectors can contain the same element any</span>
   <span style="color: #666666;">// number of times.</span>
   std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span> strings<span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #666666;">// Add some strings to the vector.</span>
   strings.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;foo&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
   strings.<span style="color: #007788;">push_back</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;bar&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #666666;">// Insert a string at a specific point in the vector.  Here &quot;baz&quot;</span>
   <span style="color: #666666;">// is appended to the vector.</span>
   strings.<span style="color: #007788;">insert</span><span style="color: #008000;">&#40;</span>strings.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">&quot;baz&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #666666;">// STL containers (such as vector, set, hash, etc) can be iterated</span>
   <span style="color: #666666;">// in a manner almost like foreach some in other languages.  First</span>
   <span style="color: #666666;">// an iterator must be declared.</span>
   std<span style="color: #008080;">::</span><span style="color: #007788;">vector</span><span style="color: #000080;">&lt;</span>std<span style="color: #008080;">::</span><span style="color: #007788;">string</span><span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">iterator</span> iter <span style="color: #000080;">=</span> strings.<span style="color: #007788;">begin</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
   <span style="color: #666666;">// The iterator can be used to access each element of the vector.</span>
   <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span><span style="color: #008080;">;</span>
       iter <span style="color: #000040;">!</span><span style="color: #000080;">=</span> strings.<span style="color: #007788;">end</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span> <span style="color: #ff0000; font-style: italic;">/* Don't iterate past the end. */</span>
       iter<span style="color: #000040;">++</span><span style="color: #008000;">&#41;</span> <span style="color: #ff0000; font-style: italic;">/* iters overload the increment (++) operator. */</span>
   <span style="color: #008000;">&#123;</span>
      <span style="color: #666666;">// To retrieve the value at the index represented by the</span>
      <span style="color: #666666;">// iterator, some more STL syntactical sugar makes it a</span>
      <span style="color: #666666;">// simply matter of using the dereference (*) operator.</span>
      std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #000040;">*</span>iter<span style="color: #008080;">;</span>
      std<span style="color: #008080;">::</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> std<span style="color: #008080;">::</span><span style="color: #007788;">endl</span><span style="color: #008080;">;</span>
   <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<pre>$ g++ vector.cpp -o vector &amp;&amp; ./vector
foo
bar
baz
$</pre>
<p>Let me know if I&#8217;ve made obvious mistakes, which I tend to do frequently!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.chalamius.se/2009/02/exploding-rats-and-the-c-standard-vector/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

