<?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>BLARGH!! for the people &#187; c#</title>
	<atom:link href="http://blargh.tommymontgomery.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://blargh.tommymontgomery.com</link>
	<description>It&#039;s time to bring forth the rhythm and the rhyme</description>
	<lastBuildDate>Fri, 09 Jul 2010 04:03:13 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Linq Walk</title>
		<link>http://blargh.tommymontgomery.com/2010/03/linq-walk/</link>
		<comments>http://blargh.tommymontgomery.com/2010/03/linq-walk/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 08:05:57 +0000</pubDate>
		<dc:creator>tmont</dc:creator>
				<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blargh.tommymontgomery.com/?p=460</guid>
		<description><![CDATA[So, we all know Linq is rad. Not in that silly faux-SQL syntax, but in the manly, declarative, fluent syntax.
But the sad part is that there is no walk extension method. Walk is a functional programming staple that iterates over a collection of stuff and applies a callback to each one. In PHP, there is [...]]]></description>
			<content:encoded><![CDATA[<p>So, we all know <a href="http://msdn.microsoft.com/en-us/netframework/aa904594.aspx">Linq</a> is rad. Not in that silly faux-SQL syntax, but in the manly, declarative, fluent syntax.</p>
<p>But the sad part is that there is no <kbd>walk</kbd> extension method. Walk is a functional programming staple that iterates over a collection of stuff and applies a callback to each one. In PHP, there is <a href="http://php.net/manual/en/function.array-walk.php"><kbd>array_walk</kbd></a>, in JavaScript there is <a href="http://javascriptkit.com/jsref/arrays.shtml"><kbd>each</kbd></a>.</p>
<p>You might be thinking, &#8220;but there is a <kbd>ForEach()</kbd> extension method on <kbd>List</kbd>!&#8221;. And you would be wrong, because there is a subtle, yet important difference between <kbd>IEnumerable</kbd> and <kbd>IList</kbd>.</p>
<p><kbd>IEnumerable</kbd> is late-binding. That means you can do whatever you want to it, but execution is deferred until you actually enumerate the enumeration, i.e. using a <kbd>foreach</kbd> construct. In a list, there is no deferred execution. This deferred execution is accomplished through the magic of closures and expression trees. Read up on them.</p>
<p>In the meantime, here is a fluent implementation of <kbd>walk</kbd>:</p>
<pre class="brush: csharp;">
public static class LinqExtensions {
	public static IEnumerable&lt;T&gt; Walk&lt;T&gt;(this IEnumerable&lt;T&gt; source, Action&lt;T&gt; action) {
		foreach (var t in source) {
			action(t);
			yield return t;
		}
	}
}
</pre>
<p>You can prove that this is in fact using deferred execution with a simple test:</p>
<pre class="brush: csharp;">
class Program {

	class Foo {
		public int Bar { get; set; }

		public override string ToString() {
			return string.Format(&quot;Foo(Bar={0})&quot;, Bar);
		}
	}

	static void Main() {
		IEnumerable&lt;Foo&gt; foos = new[] { new Foo { Bar = 2 }, new Foo { Bar = 1 }, new Foo { Bar = 3 } };

		Console.WriteLine(&quot;IEnumerable.Walk():&quot;);
		foos.Walk(Console.WriteLine);
		Console.WriteLine();

		Console.WriteLine(&quot;List.ForEach():&quot;);
		foos.ToList().ForEach(Console.WriteLine);
		Console.WriteLine();

		Console.ReadLine();
	}

}
</pre>
<p>The output looks like this:<br />
<img src="http://blargh.tommymontgomery.com/wp-content/uploads/2010/03/linqwalk1.png" alt="linqwalk" title="linqwalk" width="287" height="123" class="aligncenter size-full wp-image-468" /></p>
<p>Notice that nothing got printed the first time through, using the <kbd>Walk()</kbd> extension method. There&#8217;s your deferred execution. That means our extension method did not enumerate the enumeration, so it&#8217;s safe and efficient to use walk in a normal linq expression where you are depending on deferred execution, like this:</p>
<pre class="brush: csharp;">
IEnumerable&lt;Foo&gt; foos = new[] { new Foo { Bar = 2 }, new Foo { Bar = 1 }, new Foo { Bar = 3 } };

Console.WriteLine(&quot;Doing more stuff:&quot;);
var listOStuff = foos
	.Where(foo =&gt; foo.Bar &gt;= 2)
	.Walk(foo =&gt; foo.Bar += 10)
	.Select(foo =&gt; new Baz { Foofy = foo })
	.Walk(baz =&gt; baz.Foofy.Bar--)
	.OrderBy(baz =&gt; baz.Foofy.Bar);

foreach (var stuff in listOStuff) {
	Console.WriteLine(stuff);
}

Console.ReadLine();
</pre>
<p><img src="http://blargh.tommymontgomery.com/wp-content/uploads/2010/03/linqwalk2.png" alt="linqwalk2" title="linqwalk2" width="360" height="110" class="aligncenter size-full wp-image-466" /></p>
<p>Well, maybe that&#8217;s not &#8220;normal.&#8221; Whatever.</p>
]]></content:encoded>
			<wfw:commentRss>http://blargh.tommymontgomery.com/2010/03/linq-walk/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Setting null values in anonymous object initializer in C#</title>
		<link>http://blargh.tommymontgomery.com/2010/02/setting-null-values-in-anonymous-object-initializer-in-c/</link>
		<comments>http://blargh.tommymontgomery.com/2010/02/setting-null-values-in-anonymous-object-initializer-in-c/#comments</comments>
		<pubDate>Fri, 05 Feb 2010 09:12:20 +0000</pubDate>
		<dc:creator>tmont</dc:creator>
				<category><![CDATA[c#]]></category>

		<guid isPermaLink="false">http://blargh.tommymontgomery.com/?p=404</guid>
		<description><![CDATA[The spec claims you can&#8217;t do this:
It is a compile-time error for an expression in an anonymous object initializer to be of the null type.

And you can&#8217;t. Directly.

var obj = new {
  Value = null //syntax error: &#34;cannot assign null to anonymous type property&#34;
};

var obj = new {
  Value = (object)null //full of [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/ms364047%28VS.80%29.aspx#cs3spec_topic6">spec claims</a> you can&#8217;t do this:</p>
<blockquote><p>It is a compile-time error for an expression in an anonymous object initializer to be of the null type.</p>
</blockquote>
<p>And you can&#8217;t. Directly.</p>
<pre class="brush: csharp;">
var obj = new {
  Value = null //syntax error: &quot;cannot assign null to anonymous type property&quot;
};

var obj = new {
  Value = (object)null //full of win
};

//seriously, it works
Console.WriteLine(obj.Value == null); //True
</pre>
<p>Somebody please explain to me why this works.</p>
]]></content:encoded>
			<wfw:commentRss>http://blargh.tommymontgomery.com/2010/02/setting-null-values-in-anonymous-object-initializer-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>string.Format() in PHP</title>
		<link>http://blargh.tommymontgomery.com/2010/01/string-format-in-php/</link>
		<comments>http://blargh.tommymontgomery.com/2010/01/string-format-in-php/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 21:22:08 +0000</pubDate>
		<dc:creator>tmont</dc:creator>
				<category><![CDATA[c#]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blargh.tommymontgomery.com/?p=377</guid>
		<description><![CDATA[.NET&#8217;s sprintf() equivalent has ordered parameters.

var s = string.Format(&#34;My name is {1}, {0}&#34;, &#34;Tommy&#34;, &#34;Montgomery&#34;);
//s = &#34;My name is Montgomery, Tommy&#34;

var s = string.Format(&#34;My name is {{{0}}}&#34;, &#34;Tommy&#34;);
//s = &#34;My name is {Tommy}&#34;

I needed that. Here it is, PHPified:

function format($format) {
	$args = func_get_args();
	$format = array_shift($args);

	preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
	$offset = 0;
	foreach ($matches[1] as $data) {
		$i = [...]]]></description>
			<content:encoded><![CDATA[<p>.NET&#8217;s <a href="http://php.net/sprintf"><kbd>sprintf()</kbd></a> <a href="http://msdn.microsoft.com/en-us/library/txafckwd.aspx">equivalent</a> has ordered parameters.</p>
<pre class="brush: csharp;">
var s = string.Format(&quot;My name is {1}, {0}&quot;, &quot;Tommy&quot;, &quot;Montgomery&quot;);
//s = &quot;My name is Montgomery, Tommy&quot;

var s = string.Format(&quot;My name is {{{0}}}&quot;, &quot;Tommy&quot;);
//s = &quot;My name is {Tommy}&quot;
</pre>
<p>I needed that. Here it is, PHPified:</p>
<pre class="brush: php;">
function format($format) {
	$args = func_get_args();
	$format = array_shift($args);

	preg_match_all('/(?=\{)\{(\d+)\}(?!\})/', $format, $matches, PREG_OFFSET_CAPTURE);
	$offset = 0;
	foreach ($matches[1] as $data) {
		$i = $data[0];
		$format = substr_replace($format, @$args[$i], $offset + $data[1] - 1, 2 + strlen($i));
		$offset += strlen(@$args[$i]) - 2 - strlen($i);
	}

	return $format;
}
</pre>
<p>There&#8217;s a lot going on in here that isn&#8217;t used very often in PHP. Here is a terse explanation of the not-so-obvious features:</p>
<ul>
<li>The <a href="http://php.net/manual/en/pcre.constants.php"><kbd>PREG_OFFSET_CAPTURE</kbd></a> constant tells the regex engine to capture the string index offset of each match.</li>
<li><kbd>(?=\{)</kbd> and <kbd>(?!\})</kbd> in the regular expression are <a href="http://www.regular-expressions.info/lookaround.html">positive and negative lookarounds</a>, respectively. The nice thing about that is they&#8217;re also zero-width matching, which means they won&#8217;t be returned as part of the match result. These are needed because to output a literal <kbd>{</kbd> in a C# string you precede it with another <kbd>{</kbd> (a little know fact about C#).</li>
<li><a href="http://php.net/manual/en/function.substr-replace.php"><kbd>substr_replace</kbd></a> is an infrequently used, extremely useful function, that replaces a substring within a string, expanding to the given width.</li>
<li>The <kbd>$offset</kbd> variable tracks the string length delta. Since we&#8217;re modifying the string, the offsets captured in the regex match becomes out of date.</li>
</ul>
<p>Mmmmm&#8230; smells like win.</p>
]]></content:encoded>
			<wfw:commentRss>http://blargh.tommymontgomery.com/2010/01/string-format-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
