﻿<?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>Programming By A Tool &#187; FizzBuzz</title>
	<atom:link href="http://byatool.com/tag/fizzbuzz/feed/" rel="self" type="application/rss+xml" />
	<link>http://byatool.com</link>
	<description>This is my recorded stumbling through ASP.Net, Framework 3.5, C# 3.0, Ajax, Javascript, and Love.  Stay, learn, destroy.  It's your life.</description>
	<lastBuildDate>Thu, 03 May 2012 17:28:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>What Is Readable</title>
		<link>http://byatool.com/pontification/what-is-readable/</link>
		<comments>http://byatool.com/pontification/what-is-readable/#comments</comments>
		<pubDate>Fri, 10 Oct 2008 14:51:57 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[Pontification]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FizzBuzz]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Lambda]]></category>
		<category><![CDATA[Linq]]></category>
		<category><![CDATA[Select]]></category>

		<guid isPermaLink="false">http://byatool.com/?p=56</guid>
		<description><![CDATA[So a couple of posts I read recently have been about readability of Linq, more so Linq query expressions versus the Linq methods. Don&#8217;t know what I mean? Expression: var result = from knowledge in Sean select knowledge.Linq; As opposed to: var result = Sean.Select(knowledge =&#62; knowledge.Linq); Personally I would replace the lambda expression with [...]]]></description>
			<content:encoded><![CDATA[<p>So a couple of posts I read recently have been about readability of Linq, more so Linq query expressions versus the Linq methods.  Don&#8217;t know what I mean?</p>
<p>Expression:</p>
<pre><span style="color: #0000ff;">var</span> result = <span style="color: #0000ff;">from</span> knowledge <span style="color: #0000ff;">in</span> Sean
             <span style="color: #0000ff;">select</span> knowledge.Linq;</pre>
<p>As opposed to:</p>
<pre><span style="color: #0000ff;">var</span> result = Sean.Select(knowledge =&gt; knowledge.Linq);</pre>
<p>Personally I would replace the lambda expression with a Func, but I can live with it right now. Anywho, the argument is that the first looks better than the second. I really don&#8217;t see this as a looks problem, but a useage problem. Fact is, they both have their uses and you should know how to read both. Why is that? Well here&#8217;s an example CAUSE I KNOW YOU WANT ONE!</p>
<p>One of my earlier posts had to do with <a title="FizzBuzz" href="http://byatool.com/?p=46">solving the FizzBuzz</a> thing with Linq where I gave you this bad ass solution:</p>
<pre> <span style="color: #0000ff;">var</span> result =
      listToConvert
      .<span style="color: #000000;">Where</span>(WhereBothDivisible(fizzNumber, buzzNumber))
      .<span style="color: #000000;">Select</span>(selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>))
      .Concat(
            listToConvert
            .Where(WhereBuzzDivisable(fizzNumber, buzzNumber))
            .Select(selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>)))
            .Concat(
                  listToConvert
                  .Where(WhereFizzDivisable(fizzNumber, buzzNumber))
                  .Select(selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>)))
                  .Concat(
                         listToConvert
                        .Where(WhereNeitherDivisable(fizzNumber, buzzNumber))
                        .Select(selectKeyValuePair(<span style="color: #ff0000;">"Nothing"</span>)));</pre>
<p>As you can see, I&#8217;ve used both Func fields and methods to return Funcs to clean up how this would look. I&#8217;ll even show what it would look like without this approach:</p>
<pre><span style="color: #0000ff;">var</span> result = listToConvert.Where(currentItem =&gt;
             IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber)
             ).Select(currentItem =&gt; new KeyValuePair(currentItem, <span style="color: #ff0000;">"FizzBuzz"</span>)).Concat(...</pre>
<p>Now I can totally admit that this second one I am showing is just ouch. So the first lesson to be learn is that Funcs and Methods that return Funcs can significantly clean up the Linq Method approach.</p>
<p>Now you could do the same with expressions:</p>
<pre> <span style="color: #0000ff;">var</span> fizzBuzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>);

 <span style="color: #0000ff;">var</span> buzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
            <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
            <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>);

 <span style="color: #0000ff;">var</span> fizz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
            <span style="color: #0000ff;">where</span> WhereFizzDivisable(fizzNumber, buzzNumber)
            <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>);

<span style="color: #0000ff;">var</span> neither = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
              <span style="color: #0000ff;">where</span> WhereNeitherDivisable(fizzNumber, buzzNumber)
              <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Fizz"</span>);</pre>
<p>Ok so nice and pretty, but now what? Concatenation. This is where is gets ugly:</p>
<pre>  fizzBuzz.Concat(fizz.Concat(buzz.Concat(neither))));</pre>
<p>OR</p>
<pre> <span style="color: #0000ff;">var</span> fizzBuzz = <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"FizzBuzz"</span>)
                .Concat(
                     <span style="color: #0000ff;">from</span> currentNumber <span style="color: #0000ff;">in</span> listToConvert
                     <span style="color: #0000ff;">where</span> WhereBuzzDivisable(fizzNumber, buzzNumber)
                     <span style="color: #0000ff;">select</span> selectKeyValuePair(<span style="color: #ff0000;">"Buzz"</span>))
                     .Concat(....);</pre>
<p>See what I&#8217;m getting at? The non expression one is looking a bit better now or maybe this is a fight to see which is less fugly. Now I admit that this may not be the best FizzBuzz solution, but it gives an example or where the Linq queries can go very wrong.</p>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/pontification/what-is-readable/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Solve FizzBuzz Using Linq Extension Methods</title>
		<link>http://byatool.com/general-coding/fizzbuzz-with-linq-extension-methods/</link>
		<comments>http://byatool.com/general-coding/fizzbuzz-with-linq-extension-methods/#comments</comments>
		<pubDate>Mon, 08 Sep 2008 13:40:00 +0000</pubDate>
		<dc:creator>Sean</dc:creator>
				<category><![CDATA[General Coding]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[FizzBuzz]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[Linq]]></category>

		<guid isPermaLink="false">http://rockcityghost.wordpress.com/2008/09/08/fizzbuzz-with-linq-extension-methods/</guid>
		<description><![CDATA[So if you haven&#8217;t heard of the FizzBuzz test, it&#8217;s basically taking in a list of numbers and figuring out if they are divisible, cleanly, by two numbers. Say you have 3 and 5 and this is your list: 1 3 5 10 15 If the number is divisible by 3, then return the Fizz [...]]]></description>
			<content:encoded><![CDATA[<p>So if you haven&#8217;t heard of the FizzBuzz test, it&#8217;s basically taking in a list of numbers and figuring out if they are divisible, cleanly, by two numbers. Say you have 3 and 5 and this is your list:</p>
<p>1</p>
<p>3</p>
<p>5</p>
<p>10</p>
<p>15</p>
<p>If the number is divisible by 3, then return the Fizz string. If the number is divisible by 5, return a Buzz string. If it&#8217;s divisible by both, then return FizzBuzz.</p>
<p>1</p>
<p>Fizz</p>
<p>Buzz</p>
<p>Buzz</p>
<p>FizzBuzz</p>
<p>Pretty simple and in actuality pretty easy to do with old C# tools, but I wanted to do this with Linq. With the use of Funcs, Actions, and Linq extension methods it can be done fairly easily. Technically you can do the whole thing in one line if you don&#8217;t want to bother with refactoring.</p>
<p>I have no idea how to format this cleanly, so sorry if the format is confusing. Basically it is take a list, get the ones you want, concatenate it with the next list.</p>
<pre><span style="color: #3333ff;">public</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">IList</span>&lt;<span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">String</span>&gt;&gt; ConvertListOfIntegersWithLinqMethods(<span style="color: #00cccc;">IList</span>&lt;<span style="color: #00cccc;">Int32</span>&gt; listToConvert, <span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
<span style="color: #3333ff;">var</span> result =
  listToConvert
    .Where(WhereBothDivisible(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"FizzBuzz"</span>))
    .Concat(
  listToConvert
    .Where(WhereBuzzDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"Buzz"</span>)))
    .Concat(
  listToConvert
    .Where(WhereFizzDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"Fizz"</span>)))
    .Concat(
  listToConvert
    .Where(WhereNeitherDivisable(fizzNumber, buzzNumber))
    .Select(selectKeyValuePair(<span style="color: #cc0000;">"Nothing"</span>)));

 return result.ToList().OrderBy(currentItem =&gt; currentItem.Key).ToList();
}</pre>
<p>Using these:</p>
<pre><span style="color: #3333ff;">private</span> <span style="color: #3333ff;">stati</span>c <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">String</span>&gt;&gt; selectKeyValuePair(<span style="color: #00cccc;">String</span> value)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; <span style="color: #3333ff;">new</span> <span style="color: #00cccc;">KeyValuePair</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">String</span>&gt;(currentItem, value);
}

<span style="color: #3333ff;">private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereBothDivisible(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span>  currentItem =&gt; IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber);
}

<span style="color: #3333ff;">private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereFizzDivisable(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; IsDivisible(currentItem, fizzNumber) &amp;&amp; !IsDivisible(currentItem, buzzNumber);
}

<span style="color: #3333ff;">private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereBuzzDivisable(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; !IsDivisible(currentItem, fizzNumber) &amp;&amp; IsDivisible(currentItem, buzzNumber);
}

<span style="color: #3333ff;"> private</span> <span style="color: #3333ff;">static</span> <span style="color: #00cccc;">Func</span>&lt;<span style="color: #00cccc;">Int32</span>, <span style="color: #00cccc;">Boolean</span>&gt; WhereNeitherDivisable(<span style="color: #00cccc;">Int32</span> fizzNumber, <span style="color: #00cccc;">Int32</span> buzzNumber)
{
 <span style="color: #3333ff;">return</span> currentItem =&gt; !IsDivisible(currentItem, fizzNumber) &amp;&amp; !IsDivisible(currentItem, buzzNumber);
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://byatool.com/general-coding/fizzbuzz-with-linq-extension-methods/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

