Created
November 26, 2012 23:08
-
-
Save peterc/4151275 to your computer and use it in GitHub Desktop.
Reading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm a Rubyist with a lot of admiration for Python. Both languages are | |
similar (in the grand scheme of things) and each has huge pros and cons. | |
Python does not click for me in the same way as Ruby does not | |
click for perhaps the majority of programmer-kind. German doesn't click | |
for me either and 100m+ people speak that ;-) But I recently saw an | |
example of WHY Python taxes my Ruby brain a little. | |
I found some code at http://lukeplant.me.uk/blog/posts/why-learning-haskell-python-makes-you-a-worse-programmer/: | |
"\n".join(foo.description() for foo in mylist if foo.description() != "") | |
In order to understand this, I have to read the code in a way that's | |
unnatural to me. If I start from the beginning, I'm looking at a new line | |
character in a string. What am I doing with it? I'm asking it to join | |
a big expression together. An expression made up of foo.description()s? | |
Ah, foo is a list that can be enumerated. Oh, and then only include those | |
if foo.description() is not an empty string. Got it. | |
The equivalent(ish) Ruby is (and there are many ways to do this): | |
mylist.reject { |foo| foo.description.empty? }.map(&:description).join("\n") | |
It's not as short but we start with the actual subject, the list, rather than | |
a miscellaneous bit player. So we have our hero, the list, reject any of its | |
elements whose descriptions are empty, 'convert' that inti a list of all the | |
associated descriptions, then join them together with newline characters. | |
It reads from left to right, like (punctuation-heavy) English prose. | |
There is nothing wrong with Python but the literary part of my brain has | |
become... spoiled to reading things in a certain order. I should surely | |
get used to other approaches with practice, but this is explains the | |
situation in the here and now. | |
(A key point here isn't that Ruby is absolutely right or wrong. Spoken | |
languages differ significantly in things like subject, verb, and object order. | |
Ruby's approach fits more naturally with my current way of thinking and, | |
I believe, more in line with an English style approach, a la subject.verb(object) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I will concede when I first started out this took my brain for a loop. Its now second nature to me now, I would also argue I am better for it knowing it.