xs.filter(x => x & 1).sort().join(", ") // JavaScript
xs & filter odd & sort & map show & intercalate ", " -- Haskell
Python seems to be the odd one out. Imo, its list comprehensions are confusing as hell to "newcomers". For example, when a list comprehension has multiple `for`s, what order are they nested in?For Python, you don't really have to use list comprehensions in the place of multiple for loops, you can sacrifice the brevity afforded to write the same thing in a more easily understandable fashion.
I get that this is just a rhetorical question to make a point about newcomers, and I do agree it's not immediately obvious, but for the record: you can imagine any "if"s and "for"s in a list comprehension are nested statements in the same order. So, for example, this:
l = [
y.foo()
for x in my_list
if x.blah() > 7
for y in x.ys()
]
Is equivalent to this: l = []
for x in my_list:
if x.blah() > 7:
for y in x.ys():
l.append(y.foo())
So the least comprehension is basically in left to right order with the one exception of the actual expression to be added to the list (like y.foo() in the example above).The order is mentioned right in the docs, for Python 3:
https://docs.python.org/3/tutorial/datastructures.html#list-...
>Note how the order of the for and if statements is the same in both these snippets.
And it is mentioned even more clearly in corresponding section in the Python 2 docs - last I checked, years ago,
Update: IOW, the nested list comprehension syntax is confusing only to newcomers or even experienced devs who are lazy or stupid enough to not read the docs for the language feature they want to use, before using it, IOW, "winging it", whether to try to seem cool or due to peer pressure or other reasons, all of which are stupid ones, even in the short term, because the cost is higher than the benefit in most cases.
The hostility in your response to "lazy or stupid" devs is really funny given what a bad response it is.
Questions such as
> why does it not have parentheses around it but the `", "` passed to `join` does?
would be exactly the same for JavaScript, Go or D. Ruby has the best syntax with regards to blocks/lambdas/closures.
A bit of Smalltalk shining through Ruby.
I don't understand this argument. You are a beginner only for a tiny fraction of your time using a given programming language. Why are we optimizing a programming language for people who don't know it, instead of optimizing it for people who actually program in it?
Everyone who actually programs in a language was once a person who didn't know it. Languages which optimize for succinct terseness might be powerful once you master them, but they will never enter mainstream use, which I guess is not necessarily the aim.