Click here to Skip to main content
Click here to Skip to main content

Call Functions Until One Meets Condition

By , 19 Jun 2012
 

Introduction

This is an alternative tip to the original tip[^].

It basically wraps the original tip into one function. I prefer wrapping such functionality into a function that tells about the intent of the construct (even some purists will argue that function calls cost execution time...).

Here comes the code:

public static void RunToFirstMatch<T>(Func<T, bool> sentry, params Func<T>[] functions)
{
    functions.Any(f => sentry(f()));
}

The code is called as follows (see the original functions[^]):

RunToFirstMatch(v => (v >= 5), Step1, ()=>Step2(1,1), Step3, Step4, ()=>0+1);

As said, the content is the same, I simply added some wrapping.

History

  • V1.0, 2012-06-19: Initial version
  • V1.1, 2012-06-19: Fixed code

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Andreas Gieriet
eXternSoft GmbH
Switzerland Switzerland
Member
I feel comfortable on a variety of systems (UNIX, Windows, cross-compiled embedded systems, etc.) in a variety of languages, environments, and tools.
I have a particular affinity to computer language analysis, testing, as well as quality management.
 
More information about what I do for a living can be found at my LinkedIn Profile and on my company's web page (German only).

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
SuggestionTypo / NotesprotectorAspDotNetDev19 Jun '12 - 18:34 
A few notes...
 
You have a variable "match" and a variable "sentry". I think they are supposed to be the same variable.
 
As you have written it, you can't pass parameters to the functions (supposing they all have the same signature). You'd have to create a new function for each signature, which defeats most of the purpose of having a function. The only way I can think of to put this into a single function would require reflection or expression tree manipulation.
 
That being said, if somebody did need to repeat this pattern a few times using the same function signature, your approach does have some merit.

GeneralRe: Typo / NotesmemberAndreas Gieriet19 Jun '12 - 20:46 
Oops! That was a late minute change without re-compiling the change... Frown | :( I did fix the code - change is pending.
Thanks for your hint!
 
What concerns the function signatures: Yes, it only defines the delegate that return a given type. You do not need to define all possible function signatures - you can always call a lambda expression that calls your specific function of any signature, as long as it returns the given generic type T. Reflection or expression trees are not needed for that. Delegates are sufficient as shown in the form of Func<T> (see the example where ..., ()=>Step2(1,1), ... is called).
 
If you had other signatures, then you must define arguments to be passed to these individual functions - which is a bit difficult. You better go by lambda expressions to handle that, I guess.
 
Cheers
Andi

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 20 Jun 2012
Article Copyright 2012 by Andreas Gieriet
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid