More fun with anonymous delegates

Don Box's Spoutlet

Syndication

While we're on the topic, it's interesting to note that mscorlib defines the following operations on arrays:
 
namespace System {
  public abstract class Array {
    public static TOutput[] ConvertAll<TInput, TOutput>(TInput[] input, Converter<TInput, TOutput> cc);
    public static bool Exists<T>(T[] array, Predicate<T> match);
    public static T Find<T>(T[] array, Predicate<T> match);
    public static T FindLast<T>(T[] array, Predicate<T> match);
    public static T[] FindAll<T>(T[] array, Predicate<T> match);
    public static int FindIndex<T>(T[] array, Predicate<T> match);
    public static int FindLastIndex<T>(T[] array, Predicate<T> match);
    public static void ForEach<T>(T[] array, Action<T> action);
    public static void Sort<T>(T[] array, Comparison<T> comparer);
    public static bool TrueForAll<T>(T[] array, Predicate<T> match);
  }
}
 
which rely on the following pre-defined delegate signatures:
 
namespace System {
  public delegate void    Action<T>(T item);
  public delegate int     Comparer<T>(T first, T second); // result works like strcmp
  public delegate TOutput Converter<TInput, TOutput>(TInput input);
  public delegate bool    Predicate<T>(T item);
}
 
Here's some code that puts these methods through their paces:
 
  int[] list = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
 
  int lastOdd = Array.FindLast(list, delegate(int n) { return n % 2 == 1; });
  // lastOdd is 9
 
  int[] evens = Array.FindAll(list, delegate(int n) { return n % 2 == 0; });
  // evens contains { 2, 4, 6, 8, 10 }
 
  bool hasMultipleOfSeven = Array.Exists(evens, delegate(int n) { return n % 7 == 0; });
  // hasMultipleOfSeven is false
 
  Array.Sort(evens, delegate(int a, int b) { return b - a; });
  // evens is now { 10, 8, 6, 4, 2 }
 
  string[] s = Array.ConvertAll<int, string>(evens, delegate(int n) { return "#" + n.ToString(); });
  // s is { "#10", "#8", "#6", "#4", "#2" }
Yes, it would be nice if the compiler could do the inferencing for ConvertAll. Unfortunately, the compiler won't inference the delegate signature based on the return type, hence the explicit <int, string>.

Posted Apr 23 2005, 02:01 PM by don-box

Comments

Geert Baeyaert wrote re: More fun with anonymous delegates
on 04-24-2005 11:55 PM
One thing I've wondered about since I first learned about this though. Why isn't there a static class with static versions of these methods? It's not a big problem, as I can easily write such a class myself, but still. In my mind, it's such an obvious feature that I don't understand why this is not included in the framework.

public static class Collections
{
public static bool Exists<T>(IEnumerable<T> list, Predicate<T> match)
{
foreach (T t in list)
{
if (match(t)) return true;
}
return false;
}
public static T Find<T>(IEnumerable<T> list, Predicate<T> match)
{
...
}
...
}
Jelle Druyts wrote Generics at the Indigo service boundary
on 05-23-2005 4:05 PM
Anatoly Lubarsky: Weblog wrote .NET 2.0 -- Anonymous methods
on 07-06-2005 6:34 PM
TheChaseMan's Frenetic SoapBox wrote PDC 2005, C# 3.0, LINQ, and all kinds of cool stuff!
on 09-13-2005 8:26 PM
TheChaseMan's Frenetic SoapBox wrote PDC 2005, C# 3.0, LINQ, and all kinds of cool stuff!
on 09-13-2005 9:29 PM

Add a Comment

(required)  
(optional)
(required)  
Remember Me?