Okay, in python one can do this:
def foo(monkeys):
def bar(monkey):
#process and return new monkey
processed_monkeys = list()
for monkey in monkeys:
processed_monkeys += bar(monkey)
return processed_monkeys
(This is just a stupid example) I sometimes miss this functionality of declaring a method inside another method in c#. But today I had the following idea for accomplishing this:
List<Monkey> foo(List<Monkey> monkeys)
{
Func<Monkey, Monkey> bar = delegate(Monkey monkey)
{
//process and return new monkey
}
List<Monkey> processed_monkeys = new List<Monkey>(monkeys.Count);
foreach(Monkey monkey in monkeys)
processed_monkeys.Append(bar(monkey));
return processed_monkeys;
}
Obviously the workaround does not provide exactly the same functionality as the original as the Func or Action Delegates are limited to 4 parameters, but more than 4 parameters are rarely needed...
What are your opinions on this?
Is this evil and should be avoided for all but very special situations?
How is the performance of this? Will a new bar function be created each time the function is called, or will the compiler optimize this somehow?