Thomas
Thomas

Reputation: 34218

Call a Functions Until One Meets Condition in C#

i got a code for calling a function until meet the condition but i am not in very advance stage so code which i got not at all clear to me. here i will submit the code i got...please some one discuss about how the code works in detail.

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

The code is called as follows

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

RunToFirstMatch function take 2 argument but when this function is getting called then many argument is passing....i just do not understand about the calling of the function. please discuss thanks.

Upvotes: 3

Views: 1100

Answers (3)

Jamiec
Jamiec

Reputation: 136239

There are a few parts to this, and I can see why a novice might find this code a little hard to understand.

Firstly, the method does not take 2 arguments, the second is marked params which means that any number of parameters are passed to this method, with all but the first one being sent as an array to the argument functions (Further reading on the C# params keyword).

Next, the method uses the IEnumerable<T> extension method Any which enumerates any enumerable (such as the argument functions) until one of the methods returns true.

I think you then have some parameters mixed up, I think you've changed sentry to match, and making that assumption what happens is that each function from the array functions is executed, and the result passed to the match function (which will return true or false). The first one to return true, the method exits.

So by way of an example, roughly based on your own:

RunToFirstMatch(v => (v >= 5), // match
                  () => 1, () => 4, () => 9, () => 20, () => 40); //function(s)

Step 1, 2 & 3 will execute (3 is the first to return false), however 4 & 5 will not.

Upvotes: 5

Random Dev
Random Dev

Reputation: 52300

I think there is a slight error in the code as I think it should be

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

but let's go step by step:

sentry is a predicate that decides based on it's input (of type T) if something is true - this is the first thing you give the function - in your example this is the v >= 5 - so I guess it will take an int (not clear from your example - could for example be a double also) and check if it's greater 5

Next you got a param array of functions each giving you such a T The param array is why you can give more than one function.

The Any is a nice trick: this will go through your provided functions (in the param array), evaluates them and then check with the sentry the result. If the sentry says true any stops so the function does exactly what it says.

Upvotes: 3

Rawling
Rawling

Reputation: 50214

The second argument, params Func<T>[] functions, actually means "allow many Func<T>s to be passed as subsequent arguments, and present them in an array called functions." This is caused by the params keyword.

Thus when you call

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

you get

sentry = v => (v >= 5); /* or should this be called match? */
functions = new Func<T>[]
    {
        Step1,
        ()=>Step2(1,1),
        Step3,
        Step4,
        ()=>0+1
    };

Upvotes: 2

Related Questions