5 ms·
I've learned to code methods in OO languages without using loops after I've learned Erlang. No loops means less bugs.
by darkhorn 8y ago
I've learned to code methods in OO languages without using loops after I've learned Erlang. No loops means less bugs.
- platz 8y agoAnd none of those OO langs can be relied on to have tail recursion so now you a new problem
- yomly 8y agoMost non-functional languages will implement map/filter/reduce as loops under the hood, but realizing that there is an abstraction for general sequence processing frees you up to focus more on the core business logic at hand and reduces the surface area of your code for bugs. Sure, handrolling a loop is a trivial activity that novice programmers can figure out, so you should _never_ get it wrong because you _never_ ever program tired or hurriedly under deadlines or make typos or input off by ones...
- AnimalMuppet 8y agoI don't recall exactly how many handrolled loops I've messed up in the last 10 years, but the number is either 1 or 2. Could I mess up one or two map/filter/reduces in 10 years? Is it impossible to mess them up? What about the cases they don't cover, so you use your own recursion?
- jodrellblank 8y agoOK you can write a handrolled loop almost perfectly. /Why/ are you writing ten years worth of loops - something a computer can do for you?
- AnimalMuppet 8y agoCan it? I'm working on embedded systems, in C++, with C++11-or-earlier compilers. Sometimes I need control of the exact order things happen in. Sometimes I need an action to occur either zero or one times, on the first available entry. Always I need to write code that my coworkers can read. How much of that can a C++11 compiler do for me?
- zcw100 8y agoI can’t tell if this is sarcasm but if it isn’t it just proves the point. Your teammates must just love working with you. I’d love to see the look on their faces when you committed that code. “I just learned Erlang. Check out what I did without telling anyone. Suck it. Boom!”
- darkhorn 8y agoIt is not sarcasm. Yes, they love working with me, they learn new things from me and they use it. Generally their face is like "o_O I wish I've learned it before". I use build-in functions of PHP and Java in way without loops, there is nothing unknown. I don't write Erlang code. Here is an example what I mean; <?php declare(strict_types=1); function fibonacci_nth(int $n): int { if($n === 0) return 0; if($n === 1) return 1; else return fibonacci_nth($n - 2) + fibonacci_nth($n - 1); } function fibonacci_series(int $n): array { return array_map('fibonacci_nth', range(0, $n)); } And here in Python class Fibonacci: def fibonacci_nth(self, n): if(n == 0): return 0 if(n == 1): return 1 else: return self.fibonacci_nth(n-1) + self.fibonacci_nth(n-2) def fibonacci_series(self, n): return [self.fibonacci_nth(x) for x in range(n+1)]
- jodrellblank 8y ago"no loops means less bugs" But the loops will be clear and explicit. People can check if you have an off-by-one error. Python does not support many recursive calls, after "n" gets beyond a certain number, the code will crash with a RecursionError. That is a bug which is much harder to see. The code will crash if N < 0 for the n_th() method, which is probably a bug.