Re:How to write X in both Python 3 and JavaScript
by tuesdayrain on Friday September 14, 2018 @11:02AM
(#17987652)
Personally I find the method-style syntax significantly more readable.
Re:How to write X in both Python 3 and JavaScript
by jsh7 on Friday September 14, 2018 @11:08AM
(#17987730)
You can also ‘return’ the outer function from within a ‘for...of’ which is pretty useful imo.
Re:How to write X in both Python 3 and JavaScript
by andyfleming on Friday September 14, 2018 @12:40PM
(#17988695)
And you can use await in the context of an outer async function, which is a huge advantage in reducing code complexity.
Re:How to write X in both Python 3 and JavaScript
by tormeh on Friday September 14, 2018 @10:52AM
(#17987582)
Makes me wonder if maybe the syntax for mapping, folding and other collection operations should have a similar syntax to for instead of the current method-style syntax popular in most modern languages.
Re:How to write X in both Python 3 and JavaScript
by symlinkk on Friday September 14, 2018 @11:16AM
(#17987795)
C# has a syntax called Linq Query Syntax that provides keywords in the language to do things like map, filter, and reduce.
Example:
var queryLondonCustomers = from cust in customers
where cust.City == "London"
select cust;
I'm not a big fan of it honestly, the function style seems more consistent and easier to read to me.
Re:How to write X in both Python 3 and JavaScript
by hnlauncher on Friday September 14, 2018 @12:18PM
(#17988471)
https://www.pyret.org/ does it like this:
for each(str from [list: "Ahoy", "world!"]):
print(str)
end
for map(n from [list: 1,2,3]): n * n end
# ==> [list: 1, 4, 9]
for filter(n from [list: 1, 2, 3]):
n >= 2
end
# ==> [list: 2, 3]
for fold(sum from 0, n from [list: 4, 5, 6]):
sum + n
end
# ==> 15
Perhabs just personal preference but I'd use
instead of for...of is easier to read and recognize as a loop construct at first glance, and as far as I recall it's also faster nowadays since the body of the loop is inline, whereas forEach requires the runtime to deal with a function object.