471,325 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,325 software developers and data experts.

Predicates explained: three ways to declare predicates

Inspired by Jon, I did a demo prorgram showing three ways to declare
predicates, in for example the "FindIndex" and "FindLastIndex" methods
of Lists, but in general you can do this for any predicate.

Note the most compact way is to use Lambda notation and anonymous
functions, the first way shown below. The most 'intuitive' way is to
'hard code' the predicate using a predicate method, the second way
shown below, but the best way (for me) is to use an idea Goran gave me
and that is to construct a predicate class that 'generalizes' the hard
codes value you are using in your predicate. This is shown in the
third way below.

If you study this example you will have mastered predicates, which are
used everywhere, for example in Array.Find as well.

RL

// see also http://msdn.microsoft.com/en-us/libr...bz(VS.80).aspx
(gives a simplistic example, that really does not do justice to the
topic, as is typical for MSDN)

// September 11, 2008
//OUTPUT:

/*
0 1 2 3 4 5 6 7 8 -10

lastIndex is: 8, firstIndex is 5
lastIndex2 is: 8
firstIndex2 is: 5
lastIndex3 is: 8
firstIndex3 is: 5
Press any key to continue . . .

*/

namespace Console1
{
class Program
{
static void Main(string[] args)
{

List<intnewList = new List<int>();

for (int i = 0; i < 10; i++)
{
newList.Add(i);
}
newList.Remove(9);
//note: commenting out this above line gives: 0 1 2 3 4 5 6 7 8 -10 9
(ten elements) since nothing removed!

newList.Insert(9, -10); //gives output 0 1 2 3 4 5 6 7 8
-10

foreach (int i in newList)
{
Console.Write(" {0}", i);
}

Console.WriteLine("\n");
int y = 5; //the value we want as the comparison point

int lastIndex = newList.FindLastIndex(value =value >=
y);
int firstIndex = newList.FindIndex(value =value >= y);

//the above way uses anonymous functions and 'lambda' notation and is
very compact as you can tell

Console.WriteLine("lastIndex is: {0}, firstIndex is {1}",
lastIndex, firstIndex); //last index is 8 (i.e. List[8] = 8) first
index is 5 (List[5]= 5)

// now the second 'hard coded' way--note y>=5 is 'hard coded' into the
predicate method

int lastIndex2 = newList.FindLastIndex(MyPredicate);
Console.WriteLine("lastIndex2 is: {0}", lastIndex2);
int firstIndex2 = newList.Find(MyPredicate);
Console.WriteLine("firstIndex2 is: {0}", firstIndex2);

//now the neatest (IMO) way: the predicate class way (verbose but
easy to understand and flexible)

int lastIndex3 = newList.FindLastIndex(new
PredicateClass(y).MyPredicate);
Console.WriteLine("lastIndex3 is: {0}", lastIndex3);
int firstIndex3 = newList.Find(new
PredicateClass(y).MyPredicate);
Console.WriteLine("firstIndex3 is: {0}", firstIndex3);

}
private static bool MyPredicate(int value)
{
if (value >= 5) //value “hard coded” here –compare with
other versions
{ return true ; }
else
{ return false; }

}

//third way: construct a predicate class, so you can pick any y easily

public class PredicateClass
{
private int _y;
public PredicateClass(int y)
{
_y = y;
}
public bool MyPredicate (int value)
{
if (value >= _y)
{return true;}
else
{return false;}
}

}

Sep 10 '08 #1
0 1295

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Thong Nguyen | last post: by
reply views Thread by dayzman | last post: by
4 posts views Thread by Arun Srinivasan | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.