tag:blogger.com,1999:blog-301254542024-03-08T01:28:44.704-08:00Jacob Carpenter's blogspotJacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.comBlogger22125tag:blogger.com,1999:blog-30125454.post-13604452279479287622007-11-16T11:12:00.001-08:002007-11-16T11:14:12.746-08:00New blogFor no good reason, I installed Windows Live writer and started a new blog. You can check it out at <a href="http://jacobcarpenter.wordpress.com/">http://jacobcarpenter.wordpress.com/</a>.Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-91952479778717084082007-07-26T19:48:00.000-07:002007-07-26T19:50:45.983-07:00<p>Program.cs:</p>
<code><pre>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace HelloOrcas
{
class Program
{
static void Main(string[] args)
{
"Hello, Orcas!".Print();
}
}
public static class StringExtensions
{
public static void Print(this string message)
{
Console.WriteLine(message);
}
}
}</code></pre>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-76251476565866358092007-04-04T17:33:00.001-07:002007-04-04T17:33:53.537-07:00<div style="font-family: Verdana; font-size: 10pt; color: black; background: white;">
<pre style="margin: 0px;"><span style="color: #2b91af;"> 7</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">class</span> <span style="color: #2b91af;">CollectionHelper</span></pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 8</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 9</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">IEnumerable</span><T> Enumerate<T>(<span style="color: blue;">params</span> T[] args)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 10</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 11</span> <span style="color: blue;">if</span> (args == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 12</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"args"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 13</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 14</span> <span style="color: blue;">foreach</span> (T arg <span style="color: blue;">in</span> args)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 15</span> <span style="color: blue;">yield</span> <span style="color: blue;">return</span> arg;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 16</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 17</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 18</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> T First<T>(<span style="color: #2b91af;">IEnumerable</span><T> source)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 19</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 20</span> <span style="color: blue;">return</span> First(source, ReturnTrue);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 21</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 22</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 23</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> T First<T>(<span style="color: #2b91af;">IEnumerable</span><T> source, <span style="color: #2b91af;">Func</span><T, <span style="color: blue;">bool</span>> predicate)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 24</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 25</span> T result;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 26</span> <span style="color: blue;">if</span> (!TryFindFirst(source, <span style="color: blue;">out</span> result, predicate))</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 27</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Source sequence empty or no items satisfy predicate."</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 28</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 29</span> <span style="color: blue;">return</span> result;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 30</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 31</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 32</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> T FirstOrDefault<T>(<span style="color: #2b91af;">IEnumerable</span><T> source)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 33</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 34</span> <span style="color: blue;">return</span> FirstOrDefault(source, ReturnTrue);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 35</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 36</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 37</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> T FirstOrDefault<T>(<span style="color: #2b91af;">IEnumerable</span><T> source, <span style="color: #2b91af;">Func</span><T, <span style="color: blue;">bool</span>> predicate)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 38</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 39</span> T result;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 40</span> TryFindFirst(source, <span style="color: blue;">out</span> result, predicate);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 41</span> <span style="color: blue;">return</span> result;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 42</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 43</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 44</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> TryFindFirst<T>(<span style="color: #2b91af;">IEnumerable</span><T> source, <span style="color: blue;">out</span> T result)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 45</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 46</span> <span style="color: blue;">return</span> TryFindFirst(source, <span style="color: blue;">out</span> result, ReturnTrue);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 47</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 48</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 49</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> TryFindFirst<T>(<span style="color: #2b91af;">IEnumerable</span><T> source, <span style="color: blue;">out</span> T result, <span style="color: #2b91af;">Func</span><T, <span style="color: blue;">bool</span>> predicate)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 50</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 51</span> <span style="color: blue;">if</span> (source == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 52</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"source"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 53</span> <span style="color: blue;">if</span> (predicate == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 54</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"predicate"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 55</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 56</span> <span style="color: blue;">foreach</span> (T item <span style="color: blue;">in</span> source)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 57</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 58</span> <span style="color: blue;">if</span> (predicate(item))</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 59</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 60</span> result = item;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 61</span> <span style="color: blue;">return</span> <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 62</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 63</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 64</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 65</span> result = <span style="color: blue;">default</span>(T);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 66</span> <span style="color: blue;">return</span> <span style="color: blue;">false</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 67</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 68</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 69</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">IEnumerable</span><TOut> Map<T, TOut>(<span style="color: #2b91af;">IEnumerable</span><T> source, <span style="color: #2b91af;">Func</span><T, TOut> converter)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 70</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 71</span> <span style="color: blue;">if</span> (source == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 72</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"source"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 73</span> <span style="color: blue;">if</span> (converter == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 74</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"fn"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 75</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 76</span> <span style="color: blue;">foreach</span> (T item <span style="color: blue;">in</span> source)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 77</span> <span style="color: blue;">yield</span> <span style="color: blue;">return</span> converter(item);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 78</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 79</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 80</span> <span style="color: blue;">public</span> <span style="color: blue;">static</span> <span style="color: #2b91af;">IEnumerable</span><TOut> Zip<TLeft, TRight, TOut>(<span style="color: #2b91af;">IEnumerable</span><TLeft> left, <span style="color: #2b91af;">IEnumerable</span><TRight> right, <span style="color: #2b91af;">Func</span><TLeft, TRight, TOut> fn)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 81</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 82</span> <span style="color: blue;">if</span> (left == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 83</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"left"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 84</span> <span style="color: blue;">if</span> (right == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 85</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"right"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 86</span> <span style="color: blue;">if</span> (fn == <span style="color: blue;">null</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 87</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">ArgumentNullException</span>(<span style="color: #a31515;">"fn"</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 88</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 89</span> <span style="color: blue;">using</span> (<span style="color: #2b91af;">IEnumerator</span><TLeft> leftEnumerator = left.GetEnumerator())</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 90</span> <span style="color: blue;">using</span> (<span style="color: #2b91af;">IEnumerator</span><TRight> rightEnumerator = right.GetEnumerator())</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 91</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 92</span> <span style="color: blue;">while</span> (<span style="color: blue;">true</span>)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 93</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 94</span> <span style="color: blue;">bool</span> leftMoved = leftEnumerator.MoveNext();</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 95</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 96</span> <span style="color: blue;">if</span> (!(leftMoved == rightEnumerator.MoveNext()))</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 97</span> <span style="color: blue;">throw</span> <span style="color: blue;">new</span> <span style="color: #2b91af;">InvalidOperationException</span>(<span style="color: #a31515;">"Sequences are of incompatible lengths."</span>);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 98</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 99</span> <span style="color: blue;">if</span> (!leftMoved)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 100</span> <span style="color: blue;">break</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 101</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 102</span> <span style="color: blue;">yield</span> <span style="color: blue;">return</span> fn(leftEnumerator.Current, rightEnumerator.Current);</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 103</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 104</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 105</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 106</span> </pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 107</span> <span style="color: blue;">private</span> <span style="color: blue;">static</span> <span style="color: blue;">bool</span> ReturnTrue<T>(T ignored)</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 108</span> {</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 109</span> <span style="color: blue;">return</span> <span style="color: blue;">true</span>;</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 110</span> }</pre>
<pre style="margin: 0px;"><span style="color: #2b91af;"> 111</span> }</pre>
</div>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-90608520661877980992006-11-14T18:08:00.000-08:002006-11-14T18:26:06.893-08:00Single Raised EventHandler<p>Earlier today, a friend described a scenario at his work where he needed to hook up an <code>EventHandler</code> that was only raised once.</p>
<p>A naïve solution could be to merely check whether the critical code had been executed within the handler; then set the state appropriately upon the first execution. Something like:</p>
<pre>
public class TestClass
{
public event EventHandler SimpleEvent;
public void RaiseAll()
{
if (SimpleEvent != null)
SimpleEvent(null, EventArgs.Empty);
}
}
// elsewhere:
TestClass test = new TestClass();
bool raised = false;
test.SimpleEvent += delegate
{
if (!raised)
{
// critical code:
raised = true;
Console.WriteLine("Hello world!");
}
};
test.RaiseAll();
test.RaiseAll();
test.RaiseAll();
// output:
// Hello world!
</pre>
<p>Okay, that's fine for an event on a simple, transient instance of an object. But what if, in the lifecycle of our application, we could potentially throw away hundered of <code>EventHandlers</code>? And what if, further complicating the problem, the <code>event</code> is <code>static</code>? It sure would be nice if we could actually remove the <code>EventHandler</code>, once it has been raised.</p>
<p>Well, we can. There are two ways to achieve this: one is simple but requires duplication for reuse; the other is complicated but easily reusable.</p>
<p>Let's look at the simple one first:</p>
<pre>
TestClass test = new TestClass();
EventHandler handler = null; // avoid: error CS0165: Use of unassigned local variable 'handler'
test.SimpleEvent += handler = delegate
{
test.SimpleEvent -= handler; // remove self before executing the critical code
Console.WriteLine("Hello world!");
};
test.RaiseAll();
test.RaiseAll();
test.RaiseAll();
// output:
// Hello world!
</pre>
<p>There are a couple of things to note here:</p>
<ol>
<li>Since we know the type of <code>TestClass</code>'s <code>SimpleEvent</code>, we can declare a strongly typed <code>EventHandler</code> and assign an anonymous method to it. Not knowing this type at compile-time is the source of much of the complication of the reusable solution below.</li>
<li>Also, we temporarily assign <code>null</code> to <code>handler</code> before referring to <code>handler</code> within the body of the anonymous method to avoid the noted compiler error. Then, we assign the anonymous method to <code>handler</code> before attaching <code>handler</code> to the <code>event</code>.</li>
</ol>
<p>But, Jacob, this works fine. Why would we care about "improving" it?</p>
<p>Well, for starters, it's not very extensible. The critical code is embedded into the anonymous method. So anytime we want to bring different functionality to this event, we'll need to repeat this pattern. Also, not only is the critical code not pluggable, but we've constrained ourselves to only <code>EventHandler</code> events. There are other types of strongly typed event handling delegates with far more interesting <code>EventArgs</code> (and how does the name "EventArgs" not violate the <a href="http://www.amazon.com/Framework-Design-Guidelines-Conventions-Development/dp/0321246756">Framework Design Guidelines</a>, anyway?). And lastly, … well… because we can:</p>
<p>[At this point in the post, the author suddenly switches voices: the hand-holdy, instructive teacher is replaced with the programmer who has spent too much time with the material at hand and pastes in swaths of code assuming his audience will understand. Apologies for the lack of exposition to follow.]
<pre>
public static class EventUtility
{
public static void AttachRaisedOnce<TTarget>(TTarget target, string eventName, EventHandler raisedOnce)
where TTarget : class
{
AttachRaisedOnce<TTarget, EventArgs>(target, eventName, CastDelegate<EventHandler<EventArgs>>(raisedOnce));
}
public static void AttachRaisedOnce<TTarget, TEventArgs>(TTarget target, string eventName, EventHandler<TEventArgs> raisedOnce)
where TTarget : class
where TEventArgs : EventArgs
{
EventInfo eventTarget = typeof(TTarget).GetEvent(eventName);
if (eventTarget == null)
throw new ArgumentException(String.Format("Couldn't find event with name '{0}'", eventName), "eventName");
Delegate self = null; // avoid unassigned local
EventHandler<TEventArgs> localMethod = delegate(object sender, TEventArgs e)
{
eventTarget.RemoveEventHandler(target, self);
raisedOnce(sender, e);
};
self = Delegate.CreateDelegate(eventTarget.EventHandlerType, localMethod.Target, localMethod.Method);
eventTarget.AddEventHandler(target, self);
}
// see <a href="http://jacobcarpenters.blogspot.com/2006/06/c-30-and-delegate-conversion.html">earlier post</a>
// also: belongs elsewhere; maybe a static DelegateUtility class
private static T CastDelegate<T>(Delegate source)
where T : class // CS0702: Constraint cannot be special class 'System.Delegate'
{
if (source == null)
return null;
Delegate[] delegates = source.GetInvocationList();
if (delegates.Length == 1)
return Delegate.CreateDelegate(typeof(T), delegates[0].Target, delegates[0].Method) as T;
for (int i = 0; i < delegates.Length; i++)
delegates[i] = Delegate.CreateDelegate(typeof(T), delegates[i].Target, delegates[i].Method);
return Delegate.Combine(delegates) as T;
}
}
</pre>
<p>Calling code looks like:</p>
<pre>
TestClass test = new TestClass();
EventUtility.AttachRaisedOnce(test, "SimpleEvent", delegate { Console.WriteLine("Hello world!"); });
test.RaiseAll();
test.RaiseAll();
test.RaiseAll();
// output:
// Hello world!
</pre>
<p>For strongly typed EventHandlers, like the <code>System.Web.UI.ImageClickEventHandler</code> delegate, the calling code looks a little weird:</p>
<pre>
EventUtility.AttachRaisedOnce<ImageButton, ImageClickEventArgs>(button, "Click", delegate { Response.Write("Hello web!"); });
</pre>
<p>Note that the second type argument is the type of the <code>EventArgs</code>, not the type of the <code>EventHandler</code> delegate itself. This is due to the inablity to apply a <code>Delegate</code> constraint on a type argument, coupled with the need to assign an anonymous method to a local variable with the right type. We rely on the fact that we can convert from an <code>EventHandler<T></code> to the strongly typed <code>EventHandler</code> delegate. This means that it is also possible to compile code with the wrong event/delegate combinations; but don't worry: the runtime will "inform you" of any conversion failures.</p>
<p>Enjoy.</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1tag:blogger.com,1999:blog-30125454.post-1162575583856721542006-11-03T09:39:00.000-08:002006-11-03T09:35:53.816-08:00Enum TryParse<p>I was somewhat surprised to discover that there's no TryParse method for the Enum class in the .NET Framework 2.0. (It's also odd that the docs/compiler keep referring to Enum as a class—and yet the apprpriate <code>where</code> constraint to apply is <code>struct</code>.) So here's a pair of generic methods to support TryParse for Enums:</p>
<pre>
public static class EnumUtility
{
public static bool TryParse<T>(string value, out T result)
where T : struct // error CS0702: Constraint cannot be special class 'System.Enum'
{
return TryParse<T>(value, out result, false);
}
public static bool TryParse<T>(string value, out T result, bool ignoreCase)
where T : struct // error CS0702: Constraint cannot be special class 'System.Enum'
{
result = default(T);
try
{
result = (T)Enum.Parse(typeof(T), value, ignoreCase);
return true;
}
catch { }
return false;
}
}
</pre>
Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com6tag:blogger.com,1999:blog-30125454.post-1161116452329975342006-10-17T13:19:00.000-07:002006-11-03T09:35:53.752-08:00Enumerate Using<p>If you ever need to enumerate a collection of <code>IDisposable</code> items, and you find the following code snippet ugly:</p>
<pre>
foreach (SomeDisposableClass item in collectionOfDisposableItems)
{
using (item)
{
// do stuff
}
}
</pre>
<p>You may find the following method useful:</p>
<pre>
public static IEnumerable<T> EnumerateUsing<T>(IEnumerable<T> disposableItems)
where T : IDisposable
{
foreach (T item in disposableItems)
using (item)
yield return item;
}
</pre>
<p>Calling code looks like:</p>
<pre>
foreach (SomeDisposableClass item in EnumerateUsing(collectionOfDisposableItems))
{
// do stuff
}
</pre>
<p>And yes, <code>Dispose()</code> does get called appropriately after you're finished "doing stuff".</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1tag:blogger.com,1999:blog-30125454.post-1160172247671709972006-10-06T15:04:00.000-07:002006-11-03T09:35:53.689-08:00Merging in Subversion<p>Once you've done it, merging from a branch to the trunk (or vice versa) in Subversion is a trivial task. But that first time you attempt it, it can seem a little daunting. Here's a quick how-to:</p>
<ol>
<li>Let's say you've got a development <var>branch</var> that's all committed and ready to be merged into the <var>trunk</var>. First, <kbd>switch</kbd> your working copy to where the result of the merge will ultimately be committed; in this case, the <var>trunk</var>.</li>
<li><kbd>merge</kbd> <em>from</em> the current location <em>to</em> the location that resembles your desired final result; for us, this is merging from the <var>trunk</var> to the development <var>branch</var>.</li>
<li>Resolve any conflicts and <kbd>commit</kbd> your current working copy.</li>
</ol>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1tag:blogger.com,1999:blog-30125454.post-1158352237675737342006-09-15T13:30:00.000-07:002006-11-03T09:35:53.626-08:00Visual Studio Macro<p>Never thought I'd be posting VB to my blog, but here's a little Visual Studio macro to switch between a Class file and it's associated TestFixture. The macro assumes a <code><i>ClassName</i>Fixture</code> naming convention. Also sorry if my VB sucks, it literally took me 3 minutes plus asking a coworker to figure out how test for <code>null</code>.</p>
<p>Enjoy:</p>
<pre>
Imports System
Imports System.IO
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module TestFixtures
Public Sub SwitchBetweenClassAndFixture()
Dim active As Document, filename As String, extension As String
Const fixtureSuffix = "Fixture"
active = DTE.ActiveDocument
If Not active Is Nothing Then
filename = Path.GetFileNameWithoutExtension(active.Name)
extension = Path.GetExtension(active.Name)
If filename.EndsWith(fixtureSuffix) Then
DTE.ExecuteCommand("Edit.OpenFile", filename.Substring(0, filename.Length - fixtureSuffix.Length) + extension)
Else
DTE.ExecuteCommand("Edit.OpenFile", filename + fixtureSuffix + extension)
End If
End If
End Sub
End Module
</pre>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1154560534308806272006-08-02T16:15:00.000-07:002006-11-03T09:35:53.562-08:00Generics and Delegates<p>I see a surprising number of search hits on this blog for things involving generics, delegates, and collections. So I guess it would be appropriate to write a series of posts on the subject.</p>
<p>This is all pretty rudimentary stuff, so if you're familiar with generics, delegates, and anonymous methods, you may want to just skip this one.</p>
<h3>Generics</h3>
<pre>public static class Assert
{
public void Throws<T>(/* ... */) where T: Exception {/* ... */}
}</pre>
<p>Here we have a method, named as if it were a part of some test framework (say, <a href="http://nunit.org/">NUnit</a>). This method takes one type parameter (<code>T</code>), so we can call this method a "generic method".</p>
<p>This method also contains what is called a "type constraint". That bit beginning with <code>where</code> says that any type that stands in for <code>T</code> must derive from (or be) <code>System.Exception</code>. There are other types of… well… type constraints that I don't think I'll cover in this post.</p>
<p>Okay, so what would we need to know to see if a block of code threw a specific exception? Obviously the <em>type</em> of exception we would expect—that's <code>T</code>. But we would also need the block of code, wouldn't we?</p>
<h3>Delegates / Anonymous methods</h3>
<p>Delegates have been in C# since forever. Event handling in Forms (and Web Forms) uses delegates extensively. So what's the big fuss?</p>
<p>Well, C# 2.0 introduces the very useful concept of anonymous methods. Arbitrary blocks of code that can be passed around and executed at whim.</p>
<p>Wheras before one had to define a method corresponding to a specific delegate's signature, and then construct the delegate (using syntax that still feels a little odd) before being able to pass it around, you can now say:</p>
<pre>public delegate void VoidNoArg();
// ...
VoidNoArg myCode = delegate { Console.WriteLine("Hello world!"); };</pre>
<p>Neat. But how useful can a void, no argument delegate <em>really</em> be? Very, actually, through the magic of <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29">closures</a>. That is, you can actually say:</p>
<pre>string greeting = "Hello world!";
<strike>VoidNoArg myCode = delegate { Console.WriteLine("Hello world!"); };</strike>
VoidNoArg myCode = delegate { Console.WriteLine(<strong>greeting</strong>); };
myCode();
// output: Hello world!</pre>
<p>Note that the anonymous method referrs to a variable outside of its own scope.</p>
<h3>Putting it together</h3>
<pre>public static class Assert
{
public void Throws<T>(VoidNoArg code) where T: Exception {/* ... */}
}
// ...
int i = Int32.MaxValue;
Assert.Throws<OverflowException>(delegate { checked(i + 1); });</pre>
<p>That's just the tip of the iceberg.</p>
<p>Delegates can be generic. Classes can be generic. Generic classes can have methods that take generic delegates. The <code>System.Collections.Generic</code> collections do all of these things. We'll look at this more in a future post…</p>
Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1tag:blogger.com,1999:blog-30125454.post-1153239794374186312006-07-18T09:23:00.000-07:002006-11-03T09:35:53.386-08:00Ruby Closures<p><a href="http://innig.net/software/ruby/closures-in-ruby.rb">This guide covering closures in Ruby</a> by Paul Cantrell is very insightful. It's written as a Ruby script to be both executed and read. Thanks to <a href="http://www.rubyinside.com/">Ruby Inside</a> for <a href="http://www.rubyinside.com/getting-down-with-closures-blocks-and-procs-120.html">linking to it</a>.</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1152633322371733832006-07-11T08:55:00.000-07:002006-11-03T09:35:53.328-08:00JSON Serialization Using C# 3.0 Extension Methods<p>This post on <a href="http://keithhill.spaces.msn.com/Blog/cns!5A8D2641E0963A97!515.entry">JSON Serialization Using Extension Methods</a> is a nice demonstration of an elegant solution made possible with extension methods. Nice job, Keith.</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1152313424017622592006-07-07T15:30:00.000-07:002006-11-03T09:35:53.272-08:00Collection<T> and explicit interface implementations<p>Say you're looking at <a href="http://msdn2.microsoft.com/en-us/library/ms132397.aspx"><code>Collection<T></code></a> and <a href="http://msdn2.microsoft.com/en-us/library/ms132474.aspx"><code>ReadOnlyCollection<T></code></a> in the <a href="http://msdn2.microsoft.com/en-us/library/system.collections.objectmodel.aspx">System.Collections.ObjectModel namespace</a>. You think, “I could use those! In fact, there's a collection I've been meaning to write for awhile:”</p>
<pre>class StringCollection : Collection<string>
{
public StringCollection(params string[] strings) : base(strings) { }
public string Join(string separator)
{
// clearly this is not an efficient solution!
string[] strings = new string[Items.Count];
Items.CopyTo(strings, 0);
return String.Join(separator, strings);
}
}
class Program
{
static void Main(string[] args)
{
StringCollection strings = new StringCollection("one", "two", "three");
Console.WriteLine(strings.Join(", "));
}
}
// Output:
// one, two, three</pre>
<p>Perfect! That's exactly what you expected.</p>
<p>Next, you decide to modify your program. You want to add another string:</p>
<pre>static void Main(string[] args)
{
StringCollection strings = new StringCollection("one", "two", "three");
strings.Add("four");
Console.WriteLine(strings.Join(", "));
}</pre>
<p>Builds fine and… <strong>NotSupportedException</strong> Collection is read-only!</p>
<p>But wait, we inherited from <code>Collection<T></code>, not <code>ReadOnlyCollection<T></code>. What gives?</p>
<p>Well, when we called the base constructor for <code>Collection<T></code>, we gave it an <code>IList<string></code>. Okay, so really we gave it an <code>Array</code> of strings, but it was implicitly converted to an <code>IList<string></code>. That <code>IList</code> was stored as a field within the <code>Collection</code> wrapper (accessible via the protected <code>Items</code> property).</p>
<p>Calling <code>Add</code> calls the <a href="http://www.google.com/search?q=explicit+interface+member+implementation">explicitly implemented interface member</a> <a href="http://msdn2.microsoft.com/en-us/library/system.array.system.collections.ilist.add.aspx"><code>IList.Add</code></a> on the <code>string[]</code> which (from the docs:) <b>Throws a not supported exception in all cases</b>.</p>
<p>So, how can we modify the <code>StringCollection</code> so that it continues to call the appropriate base constructor, but coerces the <code>string[]</code> to something a bit more useful?</p>
<p>One possibility is to check the <code>IsReadOnly</code> property of the <code>IList</code> before passing it along, like so:</p>
<pre>class StringCollection : Collection<string>
{
public StringCollection() : base() { }
public StringCollection(params string[] strings) : this((IList<string>) strings) { }
public StringCollection(IList<string> strings) : base(strings.IsReadOnly ? new List<string>(strings) : strings) { }
// ...
}</pre>
<p>I'm sure there are better ways, though. Any ideas?</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1151943247440244702006-07-03T08:55:00.000-07:002006-11-03T09:35:53.214-08:00NDoc<p>Right about the time I discovered <a href="http://ndoc.sourceforge.net/">NDoc</a>, we started running pre-release bits of the 2.0 .NET Framework. It was pretty frustrating to discover such a useful tool that didn't work at all with the new features. Even more frustrating has been the lack of updates as I've checked back in on the project.</p>
<p>Well, it turns out <a href="http://jonas.lagerblad.com/blog/">Jonas Lagerblad</a> is my <a href="http://jonas.lagerblad.com/blog/?cat=2">2.0 documentation generation</a> hero. It looks like the updated NDoc still isn't compatible with Visul Studio 2005 solution files, but pointing at an assembly+generated xml works great. Thanks, Jonas!</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1151710261441902592006-06-30T16:23:00.000-07:002006-11-03T09:35:53.105-08:00Cast Delegate<p>Following my <a href="http://jacobcarpenters.blogspot.com/2006/06/c-30-and-delegate-conversion.html">previous post</a>, <a href="http://www.ejball.com/EdAtWork/">Ed</a> went ahead and made a (C# 2.0-usable) Multicast-capable version:</p>
<pre>public static T CastDelegate<T>(Delegate source)
where T : class
{
if (source == null)
return null;
Delegate[] delegates = source.GetInvocationList();
if (delegates.Length == 1)
return Delegate.CreateDelegate(typeof(T), delegates[0].Target, delegates[0].Method) as T;
for (int i = 0; i < delegates.Length; i++)
delegates[i] = Delegate.CreateDelegate(typeof(T), delegates[i].Target, delegates[i].Method);
return Delegate.Combine(delegates) as T;
}</pre>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1tag:blogger.com,1999:blog-30125454.post-1151702268049404652006-06-30T13:52:00.000-07:002006-11-03T09:35:53.047-08:00C# 3.0 and Delegate Conversion<p><code>System.Query</code> defines a number of delegate types to help with query expressions. They follow the pattern of:</p>
<pre>delegate T Func<T>();
delegate T Func<A0, T>(A0 arg0);
delegate T Func<A0, A1, T>(A0 arg0, A1 arg1);
// etc.</pre>
<p>This is all well and good.</p>
<p>Certain methods in <code>System.Query.Sequence</code> take a parameter of <code>Func<T, bool></code>. An example is <code>Where<T>(this IEnumerable<T> source, Func<T, bool> predicate)</code>. Note that the underlying signatures of <code>Func<T, bool></code> and <code>Predicate<T></code> match.</p>
<p>Well it turns out that <code>Delegate</code>s are kind of funky. For one thing, <a href="http://pluralsight.com/blogs/mike/archive/2005/12/06/17307.aspx">the <code>Delegate</code> class cannot be used as a generic type constraint</a>. Also, conversion between compatible types of delegates isn't allowed (that is, you can't cast them).</p>
<p>That means that if one has a reference to an existing <code>Predicate<T></code> one cannot use it as an argument to <code>Where</code>. I'm sure it won't come up much, because it's not often that one has a reference to a delegate without being able to reach the method that the delegate referrs to, but it could happen.</p>
<p>The best way I came up with to convert between two types of compatible delegates:</p>
<pre>
// CS0702: Constraint cannot be special class 'System.Delegate'
public static T ConvertTo<T>(this Delegate source)
where T : class //, Delegate
{
if (source.GetInvocationList().Length > 1)
throw new ArgumentException("Cannot safely convert MulticastDelegate");
return Delegate.CreateDelegate(typeof(T), source.Target, source.Method) as T;
}</pre>
<p>This isn't great. Something like the following compiles just fine:</p>
<pre>Predicate<int> isEven = n => n % 2 == 0;
MailAddress email = isEven.ConvertTo<MailAddress>();</pre>
<p>Since <code>Delegate</code> is invalid as a type constraint, there's not really any way around it. I don't think that will ever cause any difficulty, though.</p>
<p>More subtly evil, however, is that conversion between incompatible delegates will appear to work fine at compile time, but result in a runtime error. Oh well. At least now I can do this:</p>
<pre>int[] numbers = { 1, 2, 3, 4, 5 };
Predicate<int> isEven = n => n % 2 == 0;
foreach (var n in numbers.Where(isEven.ConvertTo<Func<int, bool>>()))
Console.WriteLine(n);
// Output:
// 2
// 4</pre>
<p>… not that I ever would…</p>
<p><b>Update:</b> the <a href="http://jacobcarpenters.blogspot.com/2006/06/cast-delegate.html">delegate conversion method has been extended/updated</a>.</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com4tag:blogger.com,1999:blog-30125454.post-1151598215171553132006-06-29T09:21:00.000-07:002006-11-03T09:35:52.980-08:00Best Visual Studio Help Option<p>Best Visual Studio Help option I've unchecked, today: <kbd>Tools > Options > General: Reuse topic window</kbd>. Now hitting <kbd>F1</kbd> in Visual studio is a lot more like my web browsing experience (external links open a new tab within Firefox). <kbd>Ctrl-F4</kbd> or middle-click the tab to close it.</p>
<p><b>Update:</b> to be clear, I'm talking about Help/Documentation Viewer/what used to be called MSDN; not actually Visual Studio itself.</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1151454615799775372006-06-27T17:28:00.000-07:002006-11-03T09:35:52.908-08:00Testing internal code<p>A few upfront assertions:</p>
<ol>
<li>Unit testing is good. For C# code I use <a href="http://www.nunit.org/">NUnit</a> (along with <a href="http://www.testdriven.net/">TestDriven.NET</a>).</li>
<li>Languages (including C#) have been designed with the
<code>internal</code> keyword for a reason. There are legitimate uses for
something that's accessible only within one's own assembly.</li>
<li>These <code>internal</code> classes/methods are reasonable candidates
for unit testing.</li>
</ol>
<p>So, how do you test <code>internal</code> bits without compiling test code
into your deliverable assembly?</p>
<p>It turns out that it's trivial (in the .NET 2.0 Framework): <a href="http://msdn2.microsoft.com/en-us/library/0tke9fxk.aspx">Friend
Assemblies</a> (<a href="http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx">see also</a>).</p>
<p>Say you have two projects: <samp>MyCoolAssembly</samp> and <samp>MyCoolAssembly.UnitTests</samp>.</p>
<ol>
<li>In your <samp>MyCoolAssembly</samp>, open up the
Properties/AssemblyInfo.cs.</li>
<li>Add <code>[assembly:
InternalsVisibleTo("MyCoolAssembly.UnitTests")]</code></li>
</ol>
<p>That's it! Now, <samp>MyCoolAssembly.UnitTests</samp> can access all of
the <code>internal</code> classes/methods within
<samp>MyCoolAssembly</samp>.</p>
<p>No conditional compilation symbols. No messy inner classes. Problem
solved.</p>
<p>I found out about this useful attribute in <a href="http://www.microsoft.com/MSPress/books/6522.asp">CLR via C#</a>, but
Jay Bazuzi (whose <a href="http://blogs.msdn.com/jaybaz_MS/">blog</a> I'd
really like to read except that he apparently doesn't post anymore) had
already <a href="http://blogs.msdn.com/jaybaz_ms/archive/2004/09/09/227602.aspx">blogged
about this very usage</a> back in September of 2004.</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com2tag:blogger.com,1999:blog-30125454.post-1151042425080553322006-06-22T22:53:00.000-07:002006-11-03T09:35:52.846-08:00Ruby's Enumerable mixin again<style type="text/css">
TABLE#RubyEnumerableToCSharp { border-collapse: collapse; border: none; }
#RubyEnumerableToCSharp TD { border: solid 1px #ddd; vertical-align: bottom; padding: .5em; }
#RubyEnumerableToCSharp TD CODE { white-space: nowrap; }
</style>
<p>On the topic of Ruby's Enumerable mixin, it might be interesting to
compare them to C# 2.0's Array/List<T> methods—static for Array, instance for List<T>—and 3.0's Standard Query
Operators (<code>System.Query</code> namespace).</p>
<p>In the cases where C# does not support the method, I'd recommend following the link to the ruby docs. There are some rather interesting methods, and a lot can be learned from looking at what other languages support. In fact, some of these methods (<code>each_slice</code>, <code>each_with_index</code>) might be worth implementing as extension methods in C# (a trivial excercise left to the reader).</p>
<table id="RubyEnumerableToCSharp">
<tbody>
<tr>
<th>Ruby Enumerable</th>
<th>C# 2.0 Array/List<T> methods*</th>
<th>C# 3.0 System.Query</th>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003124">all?</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003124">all?
{ |obj| block }</a></code></td>
<td><code>bool TrueForAll(Predicate<T> match)</code></td>
<td><code>bool All<T>(this IEnumerable<T> source,
Func<T, bool> predicate)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003125">any?</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003125">any?
{ |obj| block }</a></code></td>
<td><code>bool Exists(Predicate<T> match)</code></td>
<td><code>bool Any<T>(this IEnumerable<T> source,
Func<T, bool> predicate)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003120">collect
{ |obj| block }</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003121">map
{ |obj| block}</a></code></td>
<td><code>List<TOutput> ConvertAll<TOutput>(Converter<T,
TOutput> converter)</code></td>
<td><code>IEnumerable<S> Select<T, S>(this
IEnumerable<T> source, Func<T, S> selector)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003116">detect(ifnone
= nil) { |obj| block }</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003115">find(ifnone
= nil) { |obj| block }</a></code></td>
<td><code>T Find(Predicate<T> match)</code></td>
<td><code>T First<T>(this IEnumerable<T> source, Func<T,
bool> predicate)</code><br>
<code>T FirstOrDefault<T>(this IEnumerable<T> source,
Func<T, bool> predicate)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003136">each_cons(n)
{...}</a></code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003134">each_slice(n)
{...}</a></code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003130">each_with_index
{ |obj, index| block }</a></code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003110">to_a</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003111">entries</a></code></td>
<td></td>
<td><code>T[] ToArray<T>(this IEnumerable<T>
source)</code><br>
<code>List<T> ToList<T>(this IEnumerable<T>
source)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003117">find_all
{ |obj| block }</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003118">select
{ |obj| block }</a></code></td>
<td><code>List<T> FindAll(Predicate<T> match)</code></td>
<td><code>IEnumerable<T> Where(this IEnumerable<T> source,
Func<T, bool> predicate)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003114">grep(pattern)</a></code></td>
<td><code>FindAll</code> could certainly specify a predicate that
applied a regular expression against each item of the collection.</td>
<td>same for <code>Where</code>.</td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003129">include?(obj)</a></code></td>
<td><code>bool Contains(T item)</code></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003122">inject(initial)
{ |memo, obj| block }</a></code><code><br>
<a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003122">inject
{ |memo, obj| block }</a></code></td>
<td></td>
<td><code>U Aggregate<T, U>(this IEnumerable<T> source, U
seed, Func<U, T, U> func)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003127">max</a>
/ <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003126">min</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003127">max</a>
/ <a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003126">min</a>
{ |a, b| block }</code></td>
<td></td>
<td><code>T Max/Min<T>(this IEnumerable<T> source)</code>,
etc. Note that while there are a few overloads for this method, it
looks like the delegates passed in merely provide value extraction. I
don't see an overload where you can provide your own
<code>IComparer<T></code> (which is what the second Ruby method
does)</td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003123">partition
{ |obj| block }</a></code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003119">reject
{ |obj| block }</a> </code>merely returns the collection of the
inverse of <code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003117">find_all</a></code>
/ <code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003118">select</a></code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003112">sort</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003112">sort
{ |a, b | block }</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003113">sort_by
{ |obj| block }</a></code></td>
<td><code>Sort()</code><br>
<code>Sort(IComparer<T> comparison)</code></td>
<td><code>OrderedSequence<T, K>
OrderBy/OrderByDescending/ThenBy/ThenByDescending(<br/> this
IEnumerable<T> source, Func<T, K> keySelector)</code><br>
<code>OrderedSequence<T>
OrderBy/OrderByDescending/ThenBy/ThenByDescending(<br/> this
IEnumerable<T> source, Func<T, K> keySelector,
IComparer<K> comparer)</code></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003132">to_set</a></code></td>
<td></td>
<td></td>
</tr>
<tr>
<td><code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003131">zip(arg,
...)</a></code><br>
<code><a href="http://www.ruby-doc.org/core/classes/Enumerable.html#M003131">zip(arg,
...) { |arr| block }</a></code></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1151041419338375802006-06-22T22:41:00.000-07:002006-11-03T09:35:52.788-08:00Enumerable mixin<p>I really like Ruby's <a href="http://www.ruby-doc.org/core/classes/Enumerable.html">Enumerable mixin</a>. Methods like <code>each_slice</code> and <code>each_with_index</code> can inspire some really straightforward, elegant solutions.</p>
<p>Porting such a library to C# is trivial, but having to use the 2.0's verbose delegate syntax detracts from the coolness a bit. Using 3.0's lambda expressions on the other hand…</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1151025210986628002006-06-22T18:11:00.000-07:002006-11-03T09:35:52.726-08:00Okay, blogger's template is obviously not well suited to <code>pre</code>formatted text. I should try and fix that sometime…Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com0tag:blogger.com,1999:blog-30125454.post-1151024463332579232006-06-22T17:57:00.000-07:002006-11-03T09:35:52.659-08:00Ruby and C#<h3>Ruby</h3>
<pre>numbers = [1, 2, 3, 4, 5]
is_even = proc { |n| (n.modulo 2).zero? }
(numbers.map {|n| "#{n} #{is_even[n] ? 'is' : 'is not'} even"}).each {|summary| puts summary}</pre>
<h3>C# 3.0</h3>
<pre>int[] numbers = {1, 2, 3, 4, 5};
Predicate<int> isEven = n => n % 2 == 0;
Array.ForEach(numbers, n => Console.WriteLine("{0} {1} even", n, isEven(n) ? "is" : "is not"));</pre>
<p>or if you prefer...</p>
<pre><strike>Array.ForEach(numbers, n => Console.WriteLine("{0} {1} even", n, isEven(n) ? "is" : "is not"));</strike>
numbers.Each(n => Console.WriteLine("{0} {1} even", n, isEven(n) ? "is" : "is not"));
// elsewhere
public static class EnumerableExtensions
{
public static void Each<T>(this IEnumerable<T> collection, Action<T> action)
{
foreach (T item in collection)
action(item);
}
}</pre>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1tag:blogger.com,1999:blog-30125454.post-1151020230302556322006-06-22T16:48:00.000-07:002006-11-03T09:35:52.591-08:00C# and Ruby<p>Searching Google for <a href="http://www.google.com/search?q=c%23%20ruby">C# Ruby</a> sure yields some interesting articles. I hope to add to the mix in the coming weeks as I play around with the C# 3.0/LINQ May CTP.</p>
<p>Stay tuned…</p>Jacobhttp://www.blogger.com/profile/07300518452274773472noreply@blogger.com1