4 ms·
That sounds trivial? I mean, you can do that in C, what’s surprising about it? I have never found Python as nice to use as you seem to have, but Ruby always fi
by KerrAvon 3y ago
That sounds trivial? I mean, you can do that in C, what’s surprising about it?
I have never found Python as nice to use as you seem to have, but Ruby always fit like a glove. I’ve written Python since 1999 and I still would rather use almost anything else. It’s so labored to do anything complex. You can build worlds in Ruby in the time it takes you to align indentation properly for a single class in Python.
- kstrauser 3y ago> That sounds trivial? I mean, you can do that in C, what’s surprising about it? Here's the C version of it: #include <stdio.h> int addFive(int num) { return num + 5; } int call(int(*func)(int), int value) { return func(value); } int main() { printf("%d\n", call(addFive, 10)); return 0; } It's still manageable, but not nearly so simple. Even Perl doesn't let you escape having to consider pointers and references: sub addFive { return shift() + 5; } sub call { my($func, $value) = @_; return $func->($value); } print(call(\&addFive, 10)); Why do I have to remember to write `\&addFive` in Perl, when Python doesn't require it? Why do I have to write `$func->($value)` instead of the normal `func($value)` in this case? Why do I have to write `$value` in the first place? That's rhetorical: I know the answers. Still, this was the kind of thing that instantly won me over from Perl to Python. It's not that I could somehow write things in Python that were unwriteable in Perl (or C, or assembler, or ...), but that Python let me concentrate on what I was trying to say instead of how to say it. In fairness, the equivalent Python code with optional typing is also more verbose than my original, minus the pointer stuff: from collections.abc import Callable def add_five(num: int) -> int: return num + 5 def call(func: Callable[[int], int], value: int) -> int: return func(value) print(call(add_five, 10)) The key word there being "optional". Python lets you add that if you want to, but you don't have to. And note that the underlying syntax is identical if you delete the annotations. If you opt in to using them, you don't have to alter the code you're annotating.
- cutler 3y agoYou don't have to shift() or unroll @ any longer in recent Perls.
- Izkata 3y agoint call(int(*func)(int), int value) { return func(value); } At least in gcc, you don't need to think about pointers. Just use the function signature: int call(int func(int), int value) { return func(value); } I don't know if this is an extension or what, but it's something I "just tried" on a hunch with no guide in 2009 and it worked.