4 ms·
To have an honest comparison, don't compare imperative code in an imperative language with functional code in an imperative language. Compare with functional co
by andrepd 7y ago
To have an honest comparison, don't compare imperative code in an imperative language with functional code in an imperative language. Compare with functional code in a functional language, e.g. ocaml:
let list_min l =
reduce min l
I find this crushingly simple to write and to reason about, much more than the convoluted and inefficient imperative code.
EDIT: for completeness, here's the definition of `reduce` and `min`:
let reduce op l =
match l with
| [] -> invalid_arg "reduce: empty list"
| [x] -> x
| hd::tl -> op (hd) (reduce op tl)
let min a b =
if a < b then a else b
- athenot 7y agoWhile I agree with the sentiment with comparing apples to apples, I happened to notice this can be expressed nearly as elegantly in CoffeeScript: list_min = (l) -> reduce min, l With the definitions being: min = (a, b) -> if a < b then a else b reduce = (op, l) => switch when l.length is 0 then throw "reduce: empty list" when l.length is 1 then l[0] else reduce op, [(op l[0], l[1]), l[2..]].flat()
- rovolo 7y agoHere's how it would look in python: from functools import reduce def min2(a, b): if a < b: return a return b def min(list): return reduce(min2, list, None)
- saagarjha 7y ago> from functools import reduce Oh, Python…
- scrollaway 7y agoreduce() used to be in the builtins in python 2.x. It was removed due to low usage. Keeping the builtins list tiny is a core principle of the language.
- threatofrain 7y agoGuido van Rossum on reduce: > So now reduce(). This is actually the one I've always hated most, because, apart from a few examples involving + or *, almost every time I see a reduce() call with a non-trivial function argument, I need to grab pen and paper to diagram what's actually being fed into that function before I understand what the reduce() is supposed to do. So in my mind, the applicability of reduce() is pretty much limited to associative operators, and in all other cases it's better to write out the accumulation loop explicitly. https://www.artima.com/weblogs/viewpost.jsp?thread=98196 https://www.artima.com/weblogs/viewpost.jsp?thread=98196
- saagarjha 7y agoLow usage because Python makes it annoying to use…
- bobbylarrybobby 7y agoProbably low usage because it sort of relies on lambdas (not exactly, you can define one-off functions, but most people don’t like to) and python lambdas are terrible.
- kazinator 7y agoYou mean, breaking users is the principle of the language.
- scrollaway 7y agoYour post doesn't mean anything; but even the most charitable meaning I can assign to it is pretty bad. Stuff like this is no better than "php bad!!1".
- yuliyp 7y agoWhat is inefficient about the for loop? It does the same amount of comparisons, the same amount of list accesses, and the same amount of assignments to temporaries.
- abjKT26nO8 7y agoI think the OP is referring to the imperative version using recursion as inefficient. It isn't tail-recursive, so the stack will grow to infinity, even though we only need a constant amount of memory (plus the memory to store the list) to calculate the result. Most likely, they will cause the program to crash, when invoked on moderately big input. Most implementations of "reduce" are tail-recursive (and if they're not, that's a bug), so they're more efficient.