<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" version="2.0">
  <channel>
    <title>Removing All Doubt</title>
    <link>http://www.removingalldoubt.com</link>
    <description />
    <copyright>Copyright 2005 Chuck Jazdzewski</copyright>
    <lastBuildDate>Sat, 29 Mar 2008 16:55:39 GMT</lastBuildDate>
    <generator>ChrisAn's BlogX</generator>
    <managingEditor>chuckjaz@hotmail.com</managingEditor>
    <webMaster>chuckjaz@hotmail.com</webMaster>
    <item>
      <title>If you are using a loop, you're doing it wrong</title>
      <guid>http://www.removingalldoubt.com/permalink.aspx/6080a8a8-bb63-4492-acd2-1398f086fca0</guid>
      <link>http://www.removingalldoubt.com/permalink.aspx/6080a8a8-bb63-4492-acd2-1398f086fca0</link>
      <pubDate>Sat, 29 Mar 2008 16:55:39 GMT</pubDate>
      <description>&lt;body xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;p&gt;&amp;quot;If you are using a loop, you&amp;#39;re doing it wrong.&amp;quot; &lt;/p&gt;
&lt;p&gt;That is the advice one of my college professors told us when he was teaching us
&lt;a href="http://en.wikipedia.org/wiki/APL_(programming_language)"&gt;APL&lt;/a&gt;. APL 
was designed to perform vector and matrix operations. Programming in APL is an 
exercise in stringing operators together to perform strange and wonderful 
things. Using a loop is just wrong and slows things down.&lt;/p&gt;
&lt;p&gt;It is similar with LINQ, if you are using a loop you are doing it wrong. I 
find myself 
doing a lot of prototyping lately and I am forcing myself to use LINQ; not 
because I don&amp;#39;t like it, far from it, I really like LINQ, but using loops is so 
ingrained into my psyche that I have to stop myself and force myself to think in 
LINQ. Every time I am tempted to write a loop that involves a collection or an 
array I ask myself, could I use LINQ instead? Programmers with more of a 
database background seem to take to LINQ like a duck to water. They think in 
sets and vectors, I don&amp;#39;t, but I am getting there.&lt;/p&gt;
&lt;p&gt;For example sometimes I find myself needing to return an &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; when 
I have a &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; but of a different &lt;code&gt;T&lt;/code&gt;. This often happens when I want to keep 
internal implementation details private. My internal &lt;code&gt;List&amp;lt;T&amp;gt;&lt;/code&gt; might have the 
actual class but I need to return an enumerable for some interface. Before I 
would simply write a loop using the C# 2.0&amp;#39;s &lt;code&gt;&lt;strong&gt;yield&lt;/strong&gt;&lt;/code&gt; syntax,&lt;/p&gt;
&lt;blockquote&gt;
  &lt;pre class="code"&gt;
List&amp;lt;Foo&amp;gt; foos = &lt;strong&gt;new&lt;/strong&gt; List&amp;lt;Foo&amp;gt;();

...

&lt;strong&gt;public&lt;/strong&gt; IEnumerable&amp;lt;IFoo&amp;gt; Foos {
    &lt;strong&gt;get&lt;/strong&gt; {
        &lt;strong&gt;foreach&lt;/strong&gt; (Foo f &lt;strong&gt;in&lt;/strong&gt; foos)
            &lt;strong&gt;yield return&lt;/strong&gt; f;
    }
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;This loop involves a collection, can I use LINQ? Sure! By using LINQ&amp;#39;s &lt;code&gt;Cast&amp;lt;T&amp;gt;()&lt;/code&gt; method this can be replaced with,&lt;/p&gt;
&lt;blockquote&gt;
  &lt;pre class="code"&gt;
&lt;strong&gt;public&lt;/strong&gt; IEnumerable&amp;lt;IFoo&amp;gt; Foos {
    &lt;strong&gt;get&lt;/strong&gt; { &lt;strong&gt;return&lt;/strong&gt; foos.Cast&amp;lt;IFoo&amp;gt;(); }
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;If you are trying to find if a list contains an object by some name, you 
could write a loop like,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;
&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;bool&lt;/strong&gt; Contains(&lt;strong&gt;string&lt;/strong&gt; value) {
    &lt;strong&gt;foreach&lt;/strong&gt; (Foo foo &lt;strong&gt;in&lt;/strong&gt; foos)
        &lt;strong&gt;if&lt;/strong&gt; (foo.Name == value)
            &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;true&lt;/strong&gt;;
    &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;false&lt;/strong&gt;;
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Using LINQ this might look like,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;
&lt;strong&gt;public&lt;/strong&gt; &lt;strong&gt;bool&lt;/strong&gt; Contains(&lt;strong&gt;string&lt;/strong&gt; value) {
    &lt;strong&gt;return&lt;/strong&gt; (&lt;strong&gt;from&lt;/strong&gt; foo &lt;strong&gt;in&lt;/strong&gt; foos &lt;strong&gt;where&lt;/strong&gt; foo.Name == value &lt;strong&gt;select&lt;/strong&gt; foo).Any();
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;A nice thing about LINQ is you can perform complicated queries in pieces. 
With deferred execution of enumerators, this is fairly efficient as well. This 
is really helpful in the debugger. In one chunk of code I was writing I needed to 
coalesce adjacent ranges that are marked with the same text value. Assume you 
have a structure called Range that looks like,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;struct&lt;/strong&gt; Range {
    &lt;strong&gt;public int&lt;/strong&gt; Start;
    &lt;strong&gt;public int&lt;/strong&gt; Length;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;and another &lt;code&gt;&lt;strong&gt;struct&lt;/strong&gt;&lt;/code&gt; that labels the ranges with names,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;struct&lt;/strong&gt; NamedRange {
    &lt;strong&gt;public string&lt;/strong&gt; Name;
    &lt;strong&gt;public&lt;/strong&gt; Range Range;
}
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now lets have a routine that calculates the range information over some 
stream,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;public&lt;/strong&gt; IEnumerable&amp;lt;NamedRange&amp;gt; GetNamedRanges(Stream stream) {
    ...
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Lets assume that name ranges are some things like &amp;quot;whtespace&amp;quot;, &amp;quot;reserved&amp;quot;, 
&amp;quot;identifier&amp;quot;, &amp;quot;number&amp;quot; &amp;quot;string&amp;quot;, etc. as you might expect to receive from a 
lexical scanner like found in this
&lt;a href="http://www.removingalldoubt.com/PermaLink.aspx/f615fab5-28e7-4560-8d9e-db90502a4c5e"&gt;
post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;What I want to do with these ranges is to convert the names into styles such 
as you might find referencing a CSS style sheet. So, in effect, I am mapping 
NameRange values to StyledRange values where StyleRange would look something 
like,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;struct&lt;/strong&gt; StyledRange {
    &lt;strong&gt;public&lt;/strong&gt; Style Style;
    &lt;strong&gt;public&lt;/strong&gt; Range Range;
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Lets create a dictionary that maps range names to styles such as,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre cla="code"&gt;styleMap["number"] = &lt;strong&gt;new&lt;/strong&gt; Style() { Name = "green" };
styleMap["reserved"] = &lt;strong&gt;new&lt;/strong&gt; Style() { Name = "bold" };&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;I only wanted to highlight numbers and reserved words, for everything 
else I will use the default style,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;defaultStyle = &lt;strong&gt;new&lt;/strong&gt; Style { Name = "normal" };&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;We can translate our named ranges directly into styled ranges by using a
LINQ query expression such as,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;var&lt;/strong&gt; ranges = GetNamedRanges(stream);
&lt;strong&gt;var&lt;/strong&gt; styledRanges = &lt;strong&gt;from&lt;/strong&gt; range &lt;strong&gt;in&lt;/strong&gt; ranges
                  &lt;strong&gt; select new&lt;/strong&gt; StyledRange() {
                       Style = styleMap.MapOrDefault(range.Name) ?? defaultStyle,
                       Range = range.Range
                   };
&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;where &lt;code&gt;MapOrDefault()&lt;/code&gt; is an extension method for &lt;code&gt;
IDictionary&amp;lt;TKey, TValue&amp;gt;&lt;/code&gt; that looks like,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;public static&lt;/strong&gt; TValue MapOrDefault&lt;TKey, TValue&gt;(&lt;strong&gt;this&lt;/strong&gt; IDictionary&lt;TKey, TValue&gt; dictionary, TKey key) {
    TValue result;
    &lt;strong&gt;if&lt;/strong&gt; (dictionary.TryGetValue(key, &lt;strong&gt;out&lt;/strong&gt; result))
        &lt;strong&gt;return&lt;/strong&gt; result;
    &lt;strong&gt;return&lt;/strong&gt; &lt;strong&gt;default&lt;/strong&gt;(TValue);
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;which is patterned after the existing LINQ methods for &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;,
&lt;code&gt;FirstOrDefault()&lt;/code&gt; and &lt;code&gt;LastOrDefault()&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Since many of the ranges that have different names will have the same style, it 
would be nice to coalesce adjacent styles together so no two adjacent ranges 
have the same style. In other words, we only want a new styled range when the 
style changes. The above query expression just produces a one-to-one 
mapping of named range to styled range. What we need is something that will 
merge adjacent ranges. Do do this I will introduce another extension method,
&lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; for &lt;code&gt;IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt;,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;public static&lt;/strong&gt; IEnumerable&amp;lt;T&amp;gt; Reduce&amp;lt;T&amp;gt;(&lt;strong&gt;this&lt;/strong&gt; IEnumerable&amp;lt;T&amp;gt; e,
    Func&amp;lt;T, T, &lt;strong&gt;bool&lt;/strong&gt;&amp;gt; match,
    Func&amp;lt;T, T, T&amp;gt; reduce) {
    &lt;strong&gt;var&lt;/strong&gt; en = e.GetEnumerator();
    T last;
    &lt;strong&gt;if&lt;/strong&gt; (en.MoveNext()) {
        last = en.Current;
        &lt;strong&gt;while&lt;/strong&gt; (en.MoveNext()) {
            &lt;strong&gt;if&lt;/strong&gt; (!match(last, en.Current)) {
                &lt;strong&gt;yield return&lt;/strong&gt; last;
                last = en.Current;
            }
            &lt;strong&gt;else&lt;/strong&gt;
                last = reduce(last, en.Current);
        }
        &lt;strong&gt;yield return&lt;/strong&gt; last;
    }
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;What this method does is if two adjacent elements match (as defined by the &lt;code&gt;match&lt;/code&gt; 
delegate returning &lt;code&gt;&lt;strong&gt;true&lt;/strong&gt;&lt;/code&gt;) they will be reduced into one element by calling the &lt;code&gt;reduce&lt;/code&gt; 
delegate. For example, the &lt;code&gt;Sum&amp;lt;T&amp;gt;()&lt;/code &gt;standard extension method could be 
implemented using &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; as,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;&lt;strong&gt;var&lt;/strong&gt; sum = numbers.Reduce((a, b) =&amp;gt; true, (a, b) =&amp;gt; a + b).First();&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;Now that we have &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt;, lets reduce the list of styled 
ranges to coalesce the adjacent ranges with the same style. This can be done by,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;styledRanges = styledRanges.Reduce(
    (r1, r2) =&amp;gt; r1.Style == r2.Style,
    (r1, r2) =&amp;gt; &lt;strong&gt;new&lt;/strong&gt; StyledRange() {
        Style = r1.Style,
        Range = MergeRanges(r1.Range, r2.Range)});&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;&lt;code&gt;MergeRanges()&lt;/code&gt; referenced above, is,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;Range MergeRanges(Range r1, Range r2) {
    &lt;strong&gt;return new&lt;/strong&gt; Range() { Start = r1.Start, Length = r2.Start - r1.Start + r2.Length };
}&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;In my example, this took 18 ranges for a typical line of C# source down to 7 
ranges. Pretty good. But I noticed that some of those ranges were styling 
whitespace as &amp;quot;normal&amp;quot;. This seems like a waste; why switch back from 
green to black text for writing whitespace? Why not combine those with 
adjacent ranges instead? A simple approach to this is to add the following code 
prior to mapping the styles,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;ranges = ranges.Reduce(
    (r1, r2) =&gt; r1.Name == "whitespace" || r2.Name == "whitespace",
    (r1, r2) =&gt; &lt;strong&gt;new&lt;/strong&gt; NamedRange() {
        Name = r1.Name == "whitespace" ? r2.Name : r1.Name,
        Range = MergeRanges(r1.Range, r2.Range)
    });&lt;/pre&gt;&lt;/blockquote&gt;
&lt;p&gt;This says to merge whitespace ranges with adjacent non-whitespace ranges. 
This reduces the range count from 7 to 4. Not bad from the original 18 and that 
was just for one line of source. This savings adds up quickly over an entire file.&lt;/p&gt;
&lt;p&gt;The complete example, made as a function, looks like,&lt;/p&gt;
&lt;blockquote&gt;&lt;pre class="code"&gt;IEnumerable&amp;lt;StyledRange&amp;gt; StyleRanges(IEnumerable&amp;lt;NamedRange&amp;gt; ranges) {

    &lt;em&gt;// Merge whitespace ranges with adjacent non-whitespace ranges&lt;/em&gt;
    ranges = ranges.Reduce(
        (r1, r2) =&amp;gt; r1.Name == "whitespace" || r2.Name == "whitespace",
        (r1, r2) =&amp;gt; new NamedRange() {
            Name = r1.Name == "whitespace" ? r2.Name : r1.Name,
            Range = MergeRanges(r1.Range, r2.Range)});

    &lt;em&gt;// Map named ranges to styles.&lt;/em&gt;
    &lt;strong&gt;var&lt;/strong&gt; styledRanges = &lt;strong&gt;from&lt;/strong&gt; range &lt;strong&gt;in&lt;/strong&gt; ranges 
                       &lt;strong&gt;select new&lt;/strong&gt; StyledRange() { 
                           Style = styleMap.MapOrDefault(range.Name) ?? defaultStyle,
                           Range = range.Range };

    &lt;em&gt;// Merge adjacent ranges with the same style.&lt;/em&gt;
    styledRanges = styledRanges.Reduce(
        (r1, r2) =&amp;gt; r1.Style == r2.Style,
        (r1, r2) =&amp;gt; &lt;strong&gt;new&lt;/strong&gt; StyledRange() {
            Style = r1.Style,
            Range = MergeRanges(r1.Range, r2.Range)});

    &lt;strong&gt;return&lt;/strong&gt; styledRanges;
}&lt;/pre&gt;
&lt;/blockquote&gt;
&lt;p&gt;There are a few nice things about this function. First, it builds up an &lt;code&gt;
IEnumerable&amp;lt;T&amp;gt;&lt;/code&gt; but this enumerable doesn&amp;#39;t execute until one of its 
enumerators is enumerated. This is due to the deferred execution of enumerators. 
Second, even though deferred execution makes single step debugging challenging, 
you can get a picture of what the function will do by using &lt;code&gt;ToArray()&lt;/code&gt; 
in the watch windows on the intermediate results. This allows you to inspect the 
intermediate result to see if the mapping or reducing is what you expected. 
Third, this routine has no loops. It operates on each enumerable as a set. Now 
some of you will rightly say there is a loop buried in the &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; 
method. True; &lt;code&gt;Reduce&amp;lt;T&amp;gt;()&lt;/code&gt; is just like many other LINQ extension 
methods, they contain loops implied by the returned enumerator. But LINQ allows me to write code that communicates 
what I am trying to do without it being obscured by the details of how it is done. 
I think of LINQ not so much as a set of features in C# but a way of programming. 
A way of programming that, if you are using loops, you are doing it wrong.&lt;/p&gt;
&lt;/body&gt;
                </description>
      <comments>http://www.removingalldoubt.com/commentview.aspx/6080a8a8-bb63-4492-acd2-1398f086fca0</comments>
      <category>Programming</category>
    </item>
  </channel>
</rss>