[
let y = x + 1
z = 2y
-z
end
for x in 1:5
] >>> [(y:=x+1, z:= 2*y, -z)[-1] for x in range(1, 6)]
[-4, -6, -8, -10, -12] def _(x): y = x+1; z = 2*y; return -z
[ _(x) for x in range(1,6) ]e.g. with your example above,
for xs in xss:
for x in xs:
x
What's confusing about the syntax is that `x` in your example above comes first, whereas in actual nested loops it comes last.
So maybe: [for xs in xss for x in xs : x]
Could be alternative syntax that moves `x` to the end? [for xs in xss do for x in xs -> x]
(where -> is a shorthand for do yield)
in practice (after 3 years of professional F#), when writing functional code i'd actually rather have the result first rather than last. I got used to reading code right to left with Haskell so that's what feels most natural to meThat's probably so it's more in-line with do-notation:
[x | xs <- xss, x <- xs]
is the same as: do { xs <- xss; x <- xs; return x }
Maybe writing statements backwards was deemed more confusing.Personally, I don't really use list comprehension syntax, but maybe I would if they were backwards like you describe. It would make more sense with the position of the return value being to the left.
I personally prefer math-like notation because I find it much more readable and composable.
x, such as x is in xs, xs is in xss
x | x ∈ xs, xs ∈ xss # my math is rusty
[x | x <- xs, xs <- xss] # haskell, erlang is isimilar
[ x for xs in xss for x in xs ] # python, julia, order might be different
As soon as you add conditions/filters on data, math-like notation wins hands downEdit:
Since I don't know Haskell, apparently in Haskell the order is wrong. So, only Erlang gets it right (variables are upper case in Erlang):
[ X || X <- Xs, Xs <- Xss]I don't have much experience with Erlang, but that also seems wrong:
1> Xss = [[1]].
[[1]]
2> [ X || X <- Xs, Xs <- Xss].
* 1:13: variable 'Xs' is unbound
3> [ X || Xs <- Xss, X <- Xs].
[1]So, no language does it "right".
[ x for xs ∈ xss for x ∈ xs ]The "for" side mimics nested for loops structure, but then the output statement goes first (?!).
It's the same feel as MM/DD/YYYY date format: very widespread, yet absolutely asinine to anyone seeing it for the first time :)
[ z for x in range(1,6) for y in [x+1] for z in [2*y] for z in [-z] ]
Obviously not the kind of style you'd want to see in written code, but ...
Another gripe I have is the order of the for clauses:
should, in my opinion, be instead written as Also Haskell gets the order of ‘<-‘ clauses wrong, so probably mine is a minority opinion.