<?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>IntelliTect &#187; Ian</title>
	<atom:link href="http://intellitect.com/author/ian-davis/feed/" rel="self" type="application/rss+xml" />
	<link>http://intellitect.com</link>
	<description>IntelliTect Web Site</description>
	<lastBuildDate>Wed, 16 May 2012 06:03:31 +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>Performing the Gilded Rose kata</title>
		<link>http://intellitect.com/performing-the-gilded-rose-kata/</link>
		<comments>http://intellitect.com/performing-the-gilded-rose-kata/#comments</comments>
		<pubDate>Mon, 21 Nov 2011 04:15:05 +0000</pubDate>
		<dc:creator>Ian</dc:creator>
				<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://intellitechture.com/?p=2231</guid>
		<description><![CDATA[<p>I ran across the <a href="https://github.com/NotMyself/GildedRose" class="broken_link">Gilded Rose</a> kata and had to try it. I cloned the repo and <a href="http://goo.gl/77LVu">recorded the result</a>. I hope you enjoy it.</p>
<p></p>
]]></description>
			<content:encoded><![CDATA[<p>I ran across the <a href="https://github.com/NotMyself/GildedRose" class="broken_link">Gilded Rose</a> kata and had to try it. I cloned the repo and <a href="http://goo.gl/77LVu">recorded the result</a>. I hope you enjoy it.</p>
<p><iframe src="http://player.vimeo.com/video/32427432?title=0&amp;byline=0&amp;portrait=0" width="601" height="338" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></p>
]]></content:encoded>
			<wfw:commentRss>http://intellitect.com/performing-the-gilded-rose-kata/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toji &#8211; custom brewed psake goodness</title>
		<link>http://intellitect.com/toji-custom-brewed-psake-goodness/</link>
		<comments>http://intellitect.com/toji-custom-brewed-psake-goodness/#comments</comments>
		<pubDate>Wed, 09 Nov 2011 15:56:39 +0000</pubDate>
		<dc:creator>Ian</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[ALM]]></category>
		<category><![CDATA[Headlines]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[MSBuild]]></category>
		<category><![CDATA[NuGet]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[psake]]></category>

		<guid isPermaLink="false">http://intellitechture.com/?p=2184</guid>
		<description><![CDATA[<p>I have use <a href="https://github.com/jameskovacs/psake" class="broken_link">psake</a> a few times, and I wanted to see what I extract to be more productive. With the rate at which I spin off OSS and side projects, I need something that just works out of the box and is fast to set up. Borne of this need, I created <a href="https://github.com/idavis/Toji" class="broken_link">Toji</a>, and it is available as a <a href="http://nuget.org/List/Packages/Toji">NuGet package</a>.</p>
<p>I must have my build set up with compiling, testing, packaging, and deployment. I have some xunit, nunit, nuget, msbuild helpers in the project to get projects up quickly. The lack of a xunit [...]]]></description>
			<content:encoded><![CDATA[<p>I have use <a href="https://github.com/jameskovacs/psake" class="broken_link">psake</a> a few times, and I wanted to see what I extract to be more productive. With the rate at which I spin off OSS and side projects, I need something that just works out of the box and is fast to set up. Borne of this need, I created <a href="https://github.com/idavis/Toji" class="broken_link">Toji</a>, and it is available as a <a href="http://nuget.org/List/Packages/Toji">NuGet package</a>.</p>
<p>I must have my build set up with compiling, testing, packaging, and deployment. I have some xunit, nunit, nuget, msbuild helpers in the project to get projects up quickly. The lack of a xunit NuGet package, with test runner, is unfortunate as I can&#8217;t just use a script to quickly find the package.</p>
<p>To really give you a feel for what Toji does, here is a video showing off a little.</p>
<p><iframe src="http://player.vimeo.com/video/31833163?title=0&amp;byline=0&amp;portrait=0" width="601" height="338" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>
<p><a href="http://vimeo.com/31833163">Getting started with Toji</a> from <a href="http://vimeo.com/idavis">Ian Davis</a> on <a href="http://vimeo.com">Vimeo</a>.</p>
<p>Demonstrates the creation of a project and bootstrapping the build system with Toji.</p>
]]></content:encoded>
			<wfw:commentRss>http://intellitect.com/toji-custom-brewed-psake-goodness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Parallel.For where i does not go to i++</title>
		<link>http://intellitect.com/parallel-for-where-i-does-not-go-to-i/</link>
		<comments>http://intellitect.com/parallel-for-where-i-does-not-go-to-i/#comments</comments>
		<pubDate>Sun, 25 Apr 2010 13:16:08 +0000</pubDate>
		<dc:creator>Ian</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Headlines]]></category>
		<category><![CDATA[LINQ]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[PFX]]></category>

		<guid isPermaLink="false">http://www.intellitechture.com/?p=1431</guid>
		<description><![CDATA[<p>A problem was recent posed that I hadn't thought to do using <code>Parallel.For</code> in C#. How do you parallelize a <code>for</code> loop variable increased by something other than <code>i++</code>? Typically, I would simply iterate over a enumerable collection and run against the data in parallel using <code>Parallel.For</code> and <code>Parallel.ForEach</code>.</p>
<p>It turns out that there is no support for this in the PFX API. <code>Parallel.For</code> does not support incrementing other than one and it does not support reverse iteration. Also, <code>Enumerable</code> is a static class - which means we can't do extension methods on <code>Enumerable</code>. However, we can use a custom iterator to generate the sequence for us. By using a <code>Func&#60;int, int&#62;</code> allows us to use our familiar <code>i+=</code> syntax for the increment function. We can quickly create a very simple iterator that will help us out.</p>]]></description>
			<content:encoded><![CDATA[<p>A problem was recent posed that I hadn&#8217;t thought to do using <code>Parallel.For</code> in C#. How do you parallelize:</p>
<pre class="brush: csharp; title: ; notranslate">for(int i = min + offset; i &lt; max; i+=increment)
{
    DoSomething(i);
}</pre>
<p>Typically, I would simply iterate over a enumerable collection and run against the data in parallel using <code>Parallel.For</code> and <code>Parallel.ForEach</code></p>
<pre class="brush: csharp; title: ; notranslate">int min = 0;
int max = 500;
Parallel.For( min, max, DoSomething );
Parallel.ForEach( Enumerable.Range( min, max ), DoSomething );
Parallel.ForEach( ParallelEnumerable.Range( min, max ), DoSomething );</pre>
<p>It turns out that there is no support for this in the PFX API. <code>Parallel.For</code> does not support incrementing other than one and it does not support reverse iteration. Also, <code>Enumerable</code> is a static class &#8211; which means we can&#8217;t do extension methods on <code>Enumerable</code>. However, we can use a custom iterator to generate the sequence for us. By using a <code>Func&lt;int, int&gt;</code> allows us to use our familiar <code>i+=</code> syntax for the increment function. We can quickly create a very simple iterator that will help us out.</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">public class Sequence
{
    public static IEnumerable&lt;int&gt; From( int min, int max, int increment = 1, int offset = 0 )
    {
        return From( min, max, i =&gt; i += increment, offset );
    }

    public static IEnumerable&lt;int&gt; From( int min, int max, Func&lt;int, int&gt; increment, int offset = 0 )
    {
        return From( min, max, increment, i =&gt; true, offset );
    }

   // ... The rest of the API hidden for brevity ...

    public static IEnumerable&lt;int&gt; From( int min, int max, Func&lt;int, int&gt; increment, Func&lt;int, bool&gt; filter )
    {
        for ( int i = min; i &lt; max; i = increment( i ) )
        {
            if ( filter( i ) )
            {
                yield return i;
            }
        }
    }
}</pre>
<p>This is great, but it only works for integers. We have two options, create overloads for all types we want to generate sequences for, or write a generic version. I have chosen to implement this as a generic implementation, but it means we have a few issues. First, we need to be able to detect the terminating condition <code>( i &lt; max )</code> but we can&#8217;t use the <code>&lt;</code> operator. Second, we need to be able to increment the loop indexer by an arbitrary amount; we have to be able to take a numeric or a lambda expression to increment the target type, but C#s type system gets in the way. Third, we need to be able to apply the offset to the minimum without the use of the <code>+</code> operator.</p>
<p>In order to solve these problems, I have chosen to use expression trees to create delegates that I can invoke to do the work. This does slow our code down as we are invoking method calls on delegate pointers instead of being able to make simple comparisons on primitive types.</p>
<pre class="brush: csharp; title: ; wrap-lines: false; notranslate">public class Sequence
{
    public static IEnumerable&lt;T&gt; From&lt;T&gt;( T min, T max )
    {
        return From( min, max, i =&gt; Operator.Add( i, 1 ) );
    }

    public static IEnumerable&lt;T&gt; From&lt;T&gt;( T min, T max, T increment )
    {
        return From( min, max, i =&gt; Operator.Add&lt;T&gt;( i, increment ), default( T ) );
    }

    public static IEnumerable&lt;T&gt; From&lt;T&gt;( T min, T max, T increment, T offset )
    {
        return From( min, max, i =&gt; Operator.Add&lt;T&gt;( i, increment ), offset );
    }

    public static IEnumerable&lt;T&gt; From&lt;T&gt;( T min, T max, T increment, Func&lt;T, bool&gt; filter, T offset )
    {
        return From( Operator.Add&lt;T&gt;( min, offset ),
                     i =&gt; Operator.LessThan( i, max ),
                     i =&gt; Operator.Add&lt;T&gt;( i, increment ),
                     filter );
    }

   // ... The rest of the API hidden for brevity ...

    public static IEnumerable&lt;T&gt; From&lt;T&gt;( T min, Func&lt;T, bool&gt; max, Func&lt;T, T&gt; increment, Func&lt;T, bool&gt; filter )
    {
        for ( T i = min; max( i ); i = increment( i ) )
        {
            if ( filter( i ) )
            {
                yield return i;
            }
        }
    }
   // ... The Expression tree operator implementation available in the source ...
}</pre>
<p>Now we can get to the fun part and use the code to generate sequences. If you were to write overloads for int, float, double, decimal, uint, ulong, BigInteger, etc, like the first version, the code would be up to +20x faster than generating the sequence using Enumerable. We incur a lot of overhead using the expression trees and delegates as we invoke method calls instead of simple IL comparisons. However, I think that this version is much more maintainable and much smaller. Another downside to the use of generics is that we can&#8217;t use C# 4.0 default parameters which really cut down on the number of overloads we had to track manually. Keep in mind that like Enumerable, we are leveraging deferred execution in order to generate the sequences.</p>
<p>With the foundation laid, we can now generate sequences without having to do a post processing filter. Below is an example showing a typical usage of LINQ grabbing the first N odd numbers.</p>
<pre class="brush: csharp; title: ; notranslate">public void GetOddNumbers()
{
    int min = 1, max = Int32.MaxValue, take = 200;
    Func&lt;int, bool&gt; isOdd = i =&gt; i % 2 == 1;

    Enumerable.Range( min, max ).Where( isOdd ).Take( take );
    // becomes
    Sequence.From( min, max, 2 ).Take( take );
    // or
    Sequence.From( min, max, i =&gt; i+=2 ).Take( take );
    // or
    Sequence.From( min, max, 1, isOdd).Take( take );
    // or
    Sequence.From( min, max, i=&gt;i+=2, isOdd).Take( take );
    // or
    Sequence.From( min, max, i=&gt;i+=1, isOdd).Take( take );
}

// In addition to this, we can also generate the collection in reverse.
public void SequencesCanGoInReverse()
{
    int min = 0, max = 10;
    IEnumerable&lt;int&gt; sequence = Sequence.From( max - 1, i =&gt; i &gt;= min, i =&gt; --i);
    // generates {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
    sequence = Sequence.From( max - 1, i =&gt; i &gt;= min, i =&gt; i-=2);
    // generates { 9, 7, 5, 3, 1 };
}</pre>
<p>We can now go forward and backward, skip, and filter when generating sequences with a wide variety of types. Given this power, we can process our sequences in parallel using <code>Parallel.ForEach</code> just as we would any other collection. Our original goal of using <code>Parallel.For</code> is not possible so far as I can tell. But leveraging the versatility and deferred execution provided by this method, we can do just about anything we want now.</p>
<p>A word of warning. Do not use <code>i => i++</code> or <code>i => i--</code> as your incrementing function as you are not returning the incremented value and you will most likely generate an infinite sequence and an <code>OutOfMemoryException</code>!</p>
<p>If we look back to our original set of problems to solve, we have taken care of them all and can now run our variable incrementing series using <code>Parallel.ForEach</code>. Any of the examples show above can be run in parallel, but I will give you a few more examples.</p>
<pre class="brush: csharp; title: ; notranslate">Parallel.ForEach( Sequence.From( min, max, increment, offset ), Console.WriteLine );
Parallel.ForEach( Sequence.From( 3, N, i =&gt; i += 2, isPrime ), Console.WriteLine );
Parallel.ForEach( Sequence.From( 0, 6 * Math.PI, i =&gt; i + Math.PI ), Console.WriteLine );
Parallel.ForEach( Sequence.From( new BigInteger( min ), new BigInteger( max ), i =&gt; i += 3 ), i =&gt; Console.WriteLine( i.ToString() ) );</pre>
<p>If you want to take look at the full source to the <a href="http://intellitect.com/wp-content/uploads/2010/04/Sequence.cs_.txt">Sequence class</a>, you can click here to view it. Remember that this is code not rugged at all and is just a spike, but demonstrates what would be needed in order to create generic sequence implementation for parallelization.</p>
]]></content:encoded>
			<wfw:commentRss>http://intellitect.com/parallel-for-where-i-does-not-go-to-i/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

