How to replace for-loops with a functional statement in C#?

前端 未结 18 1743
不思量自难忘°
不思量自难忘° 2021-01-31 08:59

A colleague once said that God is killing a kitten every time I write a for-loop.

When asked how to avoid for-loops, his answer was to use a functional language. However

相关标签:
18条回答
  • 2021-01-31 09:03

    There's nothing wrong with for loops but here are some of the reasons people might prefer functional/declarative approaches like LINQ where you declare what you want rather than how you get it:-

    1. Functional approaches are potentially easier to parallelize either manually using PLINQ or by the compiler. As CPUs move to even more cores this may become more important.

    2. Functional approaches make it easier to achieve lazy evaluation in multi-step processes because you can pass the intermediate results to the next step as a simple variable which hasn't been evaluated fully yet rather than evaluating the first step entirely and then passing a collection to the next step (or without using a separate method and a yield statement to achieve the same procedurally).

    3. Functional approaches are often shorter and easier to read.

    4. Functional approaches often eliminate complex conditional bodies within for loops (e.g. if statements and 'continue' statements) because you can break the for loop down into logical steps - selecting all the elements that match, doing an operation on them, ...

    0 讨论(0)
  • 2021-01-31 09:06
    1. For loops are not bad. There are many very valid reasons to keep a for loop.
    2. You can often "avoid" a for loop by reworking it using LINQ in C#, which provides a more declarative syntax. This can be good or bad depending on the situation:

    Compare the following:

    var collection = GetMyCollection();
    for(int i=0;i<collection.Count;++i)
    {
         if(collection[i].MyValue == someValue)
              return collection[i];
    }
    

    vs foreach:

    var collection = GetMyCollection();
    foreach(var item in collection)
    {
         if(item.MyValue == someValue)
              return item;
    }
    

    vs. LINQ:

    var collection = GetMyCollection();
    return collection.FirstOrDefault(item => item.MyValue == someValue);
    

    Personally, all three options have their place, and I use them all. It's a matter of using the most appropriate option for your scenario.

    0 讨论(0)
  • 2021-01-31 09:06

    Sometime you don't kill just one kitten.

    for (int i = 0; i < kittens.Length; i++) { kittens[i].Kill(); }

    Sometimes you kill them all.

    0 讨论(0)
  • 2021-01-31 09:09

    Any construct in any language is there for a reason. It's a tool to be used to accomplish a task. Means to an end. In every case, there are manners in which to use it appropriately, that is, in a clear and concise way and within the spirit of the language AND manners to abuse it. This applies to the much-misaligned goto statement as well as to your for loop conundrum, as well as while, do-while, switch/case, if-then-else, etc. If the for loop is the right tool for what you're doing, USE IT and your colleague will need to come to terms with your design decision.

    0 讨论(0)
  • 2021-01-31 09:11

    For loops don't kill people (or kittens, or puppies, or tribbles). People kill people. For loops, in and of themselves, are not bad. However, like anything else, it's how you use them that can be bad.

    0 讨论(0)
  • 2021-01-31 09:11

    A for loop can always be replaced by a recursive function that doesn't involve the use of a loop. A recursive function is a more functional stye of programming.

    But if you blindly replace for loops with recursive functions, then kittens and puppies will both die by the millions, and you will be done in by a velocirapter.

    OK, here's an example. But please keep in mind that I do not advocate making this change!

    The for loop

    for (int index = 0; index < args.Length; ++index)
        Console.WriteLine(args[index]);
    

    Can be changed to this recursive function call

    WriteValuesToTheConsole(args, 0);
    
    
    static void WriteValuesToTheConsole<T>(T[] values, int startingIndex)
    {
        if (startingIndex < values.Length)
        {
            Console.WriteLine(values[startingIndex]);
            WriteValuesToTheConsole<T>(values, startingIndex + 1);
        }
    }
    

    This should work just the same for most values, but it is far less clear, less effecient, and could exhaust the stack if the array is too large.

    0 讨论(0)
提交回复
热议问题