Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / Visual Basic
Alternative
Tip/Trick

Call Functions Until One Meets Condition

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
24 Jun 2011CPOL 6.3K   2
I do think the approach is over complicated, but in some cases there may be (a small) benefit. As an alternative I would offer:First version (no common test):((Action)(() =>{ if (!Step1()) return; if (!Step2()) return;}))();Second version (common...
I do think the approach is over complicated, but in some cases there may be (a small) benefit. As an alternative I would offer:

First version (no common test):
C#
((Action)(() =>
{
    if (!Step1())
        return;
    if (!Step2())
        return;
}))();


Second version (common test):
C#
((Action)(() =>
{
    Func<int, bool> Test = (itemToTest) =>
    {
        return itemToTest > 0;
    };
    if (!Test(Step1()))
        return;
    if (!Test(Step2()))
        return;
}))();


After giving it some more thought, I come to the conclusion that there is (in my opinion) a still more readable way of doing things. The problem (I think) is that it needs the function pointer array (and I do not like that).
C#
foreach (Func<int> Step in new Func<int>[] { Step0, Step1, Step2, Step3 })
    if (Step() > 1)
        break;


After giving it even more thought, I come to the conclusion that there is (in my opinion) a still more readable way of doing things. The problem (I think) is that it also needs the function pointer array (and I still do not like that). And this one can be implemented using a extension method (this may be the way to do it if you use this kind of code more often).
C#
static class Extensions
{
    public static void InvokeUntilFalse<T>(
        this IEnumerable<Func<T>> Steps, Func<T, bool> Test)
    {
        foreach (Func<T> Step in Steps)
            if (!Test(Step()))
                break;
    }
}

(new Func<int>[] { Step0, Step1, Step2, Step3 })
    .InvokeUntilFalse<int>((ToTest) => { return ToTest < 2; });


And this is almost like the one that was proposed by the OP. Mmmm time for my vote of 5 :)
C#
new Func<bool>[]
{
    Step1,
    // You can use a lambda to wrap a function with a different signature.
    () => Step2(1, 1),
    Step3,
    Step4,
    // You can use a lambda to avoid the use of a function.
    () => 0 == 1
}.Any((step) => !step());

License

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


Written By
FDW
Netherlands Netherlands
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralThat is what I forgot about, or, I thought that one of the t... Pin
FDW23-Jun-11 19:51
FDW23-Jun-11 19:51 
GeneralDoesn't really seem like an alternate. For one, I don't see ... Pin
AspDotNetDev23-Jun-11 7:26
protectorAspDotNetDev23-Jun-11 7:26 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.