4 ms·
A better question is whose going to maintain all those for-loops that are buggy because human error means even a basic thing like a loop will get screwed up. Ma
by fleitz 16y ago
A better question is whose going to maintain all those for-loops that are buggy because human error means even a basic thing like a loop will get screwed up. Managers are always talking about code reuse but as soon as you pass a function, they are like "Woah thats crazy how can we maintain that"
Only managers would think that
int accum;
for(int i = 0; i < arr.length; i++){
accum += arr[i];
}
is more readable and maintainable than...
arr.sum(x => x)
Oh look and even though I've written a million for loops depending on the language there is a bug because i didn't intialize accum.
- fauigerzigerk 16y agoAre you explaining the difference between code you have to write and debug and code that someone else has already written and debugged?
- fleitz 16y agoWhat I'm saying is that passing functions (or functional programming) allows you to reuse a lot more code than typical imperative programs with a lot less effort. I personally find the functional style to be much more readable and don't understand why everyone has such a problem with the maintainability of code written in a functional style even if it's a mostly imperative language.
- DougBTX 16y agoI'm not sure where managers come into the discussion, but the second example is good, but even better like this: arr.sum()
- siika2000 16y agoIn what language are arrays of numbers their own class?
- siika2000 16y agoTo answer my own question: C++ http://www.cplusplus.com/reference/std/valarray/valarray/sum/ http://www.cplusplus.com/reference/std/valarray/valarray/sum...
- sophacles 16y agoIn dynamic languages where arrays can be homogenous, the array can still have a sum method, it just assumes decent coercion rules and overloaded + (or it barfs...).
- IDisposableHero 16y agoThat would be legal in C# 3.5 or later. Though technically it's not that "array of numbers" is a class, but that array of T implements the interface IEnumerable<T>, and Sum() is extension method defined for IEnumerable<int> It's even in the standard libs, you don't have to build it. Documentation is here: http://msdn.microsoft.com/en-us/library/system.linq.enumerable.sum.aspx http://msdn.microsoft.com/en-us/library/system.linq.enumerab...
- IDisposableHero 16y agoIt looks like there's two directions for a language's type system to go: 1) strict typing, but with lots of constructs (e.g. inheritance, interfaces, generics, extension methods) to work around that strictness so that you can do what you want, if you understand the rules and syntax to get it to compile. 2) loose, "duck typing" so you just do what you want, but without compiler checking that it's possible, and with the posibility that it fails at runtime if the right method isn't found. C# is in the first direction.
- thomasz 16y agoThis was written in May 2007. C# 3.0 was released in August. We are talking about c# 2.0 here. No Lambda syntax, no Linq, no Type Inference, no nothing. Anonymous methods were clearly intended to handle events, and even that was discouraged in favor of On_SomethingHappend(...) methods in the MSDN docs. His code must have looked like this: public delegate TResult Func<T,TResult>(T input); public delegate TResult Func<T,U,TResult>(T inputA, U inputB); public static TResult Fold<TAcc,T>(IEnumerable<T> collection, TAcc state, Func<TAcc,T,TAcc> f) { TAcc acc = state; foreach(T item in collection) { acc = f(acc,item); } return acc; } //[...] public int FunctionalMasturbation() { int totalWithdrawed = Fold<int,int>(0,_withdrawals, delegate(int acc, int value) { return value+acc; } ); //... } This is an abomination. Right, for developers with a background in FP, than it's pretty clear what's going on, even if it looks like puke. But in 2007 you could hold a conference for all .net devs with knowledge of Functional Concepts in a telephone booth.