What are Predicates and Actions?

by Dominic Zukiewicz 12. March 2008 07:46

.Net Framework 2.0 brought us a cleaner way to deal with multiple items in an Collection. Sometimes the code can be cluttered up with trivial statements, printing information, or selecting certain items because their properties signify the object is useful in some way.

  • Predicates are a way of processing an array of values using a method call. The method call would process a single value (being called once for each index) and then returning a result. Depending on that result, will depend on if the processing continues or exits as the result may have been found.
  • Actions are similar to predicates, but there is no return value from the method. Its similar to a foreach statement, except a method is called instead of a foreach() {} loop being set up.

Below are some code examples that will hopefully clear things up.

 

Predicates

Here is a quick example. I will create a random array of bytes, and then filter everything that isn't below 0x30.

static void Main(string[] args)
{
     Random r = new Random();
     byte[] data = new byte[5000];
 
     for(int index=0;index<data.Length;index++)
     {
          data[index] = (byte)r.Next(byte.MaxValue);
     }
 
     Array newBytes = new Array();
 
     foreach(byte b in data)
     {
          if(b < 0x30)
               newBytes.Add(b);
     }
 
     byte[] newData = (byte[])newBytes.ToArray(byte[]);
}

Its doesn't really look cluttered as it is functional. So lets create some methods to carry out some of these function calls.

private bool LessThan0x30(byte value) 
{ 
     return value < 0x30; 
}

How does this help? Well, to the untrained eye, it appears that the code changes to..

static void Main(string[] args)
{
     ...
 
     Array newBytes = new Array();
 
     foreach(byte b in data)
     {
          if(LessThan0x30(b))
               newBytes.Add(b);
     }
 
     byte[] newData = (byte[])newBytes.ToArray(byte[]);
}

BUT, the Array class has methods to "filter" the items via a method we provide. The method being called is known as a predicate! Its a method which seperates the implementation away to a method.

static void Main(string[] args)
{
     Random r = new Random();
     byte[] data = new byte[5000];
 
     for(int index=0;index<data.Length;index++)
     {
          data[index] = (byte)r.Next(byte.MaxValue);
     }
 
     //Before
     //Array newBytes = new Array();
 
     //foreach(byte b in data)
     //{
     //     if(b < 0x30)
     //          newBytes.Add(b);
     //}
 
     //After
     byte[] newData = Array.FindAll(data,LessThan0x30);
}

So the Array class is doing the iteration for us. The Array class has several methods applicable to delegates, including Find() (finds the first), IndexOf, LastIndexOf, TrueForAll, BinarySearch, Convert, Exists.

Actions

There are also methods which use "Action"'s. Actions are similar to Predicates except they do not return any values. They just literally peform an action and thats it! For example, it is an easy way to print values from one place.

private static void PrintMe(byte value)
{
     Console.WriteLine(value); 
} 
 
static void Main(string[] args)
{
     Random r = new Random();
     byte[] data = new byte[5000];
 
     for(int index=0;index<data.Length;index++)
     {
          data[index] - (byte)r.Next(byte.MaxValue);
     }
 
     //Predicate    
     byte[] newData = Array.FindAll(data,LessThan0x30);
 
     //Print items
     Array.ForEach(newData, PrintMe);
} 

And thats a quick review of Predicates and Actions. Not as scary as they sound? Let me know!

Tags:

Framework

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  July 2010  >>
MoTuWeThFrSaSu
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

View posts in large calendar