Anonymous Functions, Func and .Where

When you mess around with lists and IEnumerable, looking at all the options which are available, you find seemingly useful methods such as “Where”, but they come with scary parameters such as Func<T, Boolean><t, boolean=””>. Until recently I just ignored these methods as crazy, complex methods for elite programmers, but having worked at SEED I realise they are actually pretty simple.

So let’s look at where “Where” would be really useful. Below is some code you could use to cap the speed of fast sprites.

 foreach (Sprite s in SpriteList)  
 {  
    if (s.Speed >= 5)  
      s.Speed=5;  
 }  

You probably have very similar methods or loops in your code, I know I do. Now this is the perfect example of where we can use Where to speed this whole thing up. Func<t, boolean=””> means a function with T where T is part of a boolean statement.  Essentially this means we can use boolean statements (if statements) inside the foreach loop.

The Where  option only appears in objects which have an enumerable type, a type which defines the type of objects in the collection and which is usually set in a “<(type)>” initially. Objects which fit this description include List and IEnumerable.

So in the above example, we can use the Where method in the foreach loop to cut out the Sprites we don’t want to look at. All we have to do is decide on our boolean statement. Fortunately we already have this inside in our loop; “if(s.Speed >= 5)”. So we can move this into our foreach loop like this:

 foreach (Sprite s in SpriteList.Where(sp => sp.Speed >= 5))  
 {   
      s.Speed=5;  
 }  

This means that the only Sprites which get passed into the loop are those which fit the if statement. The other new thing we see in the code is “=>”, known as a Lambda Operator, because the code inside the brackets (namely “sp => sp.Speed >= 5”) is called a Lambda Expression or an Anonymous Function, which is a function with no home. They are often used in delegates and LINQ queries,  but those are words for another tutorial. Basically it means we can assign a function without making a method, so in this instance we can assign the function to the list as it where so we can pull out the Sprites we want.

You can use this to limit the objects you use in a variety of ways; limit those you update, those you define, those you move etc. I used this a lot in tripwire to speed up loops and make them look better than using lots of if statements. I hope you find some places to use this handy method.

Adam

Advertisements

One thought on “Anonymous Functions, Func and .Where

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Powered by WordPress.com.

Up ↑

%d bloggers like this: