by stavros · 5y ago · 0 comments
The classic example being the arbitrary restrictions on the Python lambda syntax.
Ruby is more orthogonal and systematic. Everything is an object, and you call methods on those objects. And sending a block as a method parameter gives you something that very much resembles functional programming (minus the immutability).
There are a few other constructs (it's not quite so dogmatic about object orientation as Smalltalk), but Ruby is very regular and so you can understand how everything works with a small number of syntactic constructs.
At first I disliked this, but it does nudge you towards readability, which winds up to be a good thing. And if you look at it, there is very little space difference between an unnamed lambda and an embedded function:
def lambda_version():
a = lambda x:
line one
line two # note: this sort-of requires the idea of implicit return, which Python does not have outside of lambda
a()
def named_version():
def lambda_replacement():
line one
return line two
lambda_replacement()That's actually because Guido thinks multi-line expressions are messy to parse, rather than because they can't be done. Consider:
f = lambda x:
return x + 5
Or, perhaps: print(sum(map(lambda n, b:
while n:
b.append(n)
n -= 1
return b
, range(3), [[], [], []]), []))
You can see why this was never added to the language, though.Python, however, does indeed preclude one from using additional signifiers of code structure beyond indentation.
And in practice you don’t type out parentheses. You put in a single opening parentheses and your editor will add the closing parentheses and add the appropriate indentation (and if desired, newlines) for you. In practice the only difference in typing is typing a single ( character vs a single <tab> character.
All that being said, this is the very definition of bike shedding, so it’s really not a factor when picking one language over another.
By "parentheses" do you mean "brackets"? If so, sure, you can use indentation along with brackets, but why am I now using two things? With Python, all I need to do is backspace every once in a while, to signify the end of indentation, which is very easy to do.
I can copy code marked up with parens and brackets from basically any source (no matter how it's indented or how whitespace is handled) and my linter will automatically understand and format it nicely.
In an indentation language - If I copy a chunk of code that's nested more or less deeply than the section I'm currently working on, I have to manually replace whitespace everywhere. Sometimes the linter is smart enough, sometimes it's not.
I actually find Python easier in this regard, even in editors where I have to re-select everything I just pasted.
I choose to believe that this means S-expressions.
This came up nearly 100+ times in those 3 months, just in the classes I TA'd for.
Making invisible characters carry significant meaning really throws non-programmers for a loop at times: It's so easy, right until they break it and don't understand how.
Black can also diagnose/fix some issues.