Re:How to write X in both Python 3 and JavaScript
by sayazamurai on Friday September 14, 2018 @10:16AM
(#17987314)
Creator here! Let me know if you have any suggestions.
[ Parent
| on HN ]
Re:How to write X in both Python 3 and JavaScript
by floatrock on Friday September 14, 2018 @01:01PM
(#17988888)
Truthiness!
Although this is a case where idiomatic python is different from idiomatic js.
In js:
// same:
!! "" // false
!! 0 // false
!! 1 // true
!! null // false
// different:
!! {} // true
!! [] // true
while in python: # same:
not not "" # False
not not 0 # False
not not 1 # True
not not None # False
# different:
not not {} # False
not not [] # False
not not tuple() # False
Re:How to write X in both Python 3 and JavaScript
by mschuetz on Friday September 14, 2018 @10:47AM
(#17987554)
Nice list.
Perhabs just personal preference but I'd use
for(let element of someList){
console.log(element);
}
instead of someList.forEach(element => {
console.log(element)
})
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.
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
Re:How to write X in both Python 3 and JavaScript
by saghm on Friday September 14, 2018 @07:18PM
(#17991869)
That sounds a lot like Python list comprehensions
Re:How to write X in both Python 3 and JavaScript
by laumars on Friday September 14, 2018 @10:45AM
(#17987536)
I genuinely love the design of the page. It has personality without compromising readability. This is something a lot of people struggle to get right.
Re:How to write X in both Python 3 and JavaScript
by andyfleming on Friday September 14, 2018 @12:44PM
(#17988731)
Instead of...
Object.entries(newDict).forEach(element => {
console.log(`${element[0]} ${element[1]}`)
})
... you could write ... for (const key in newDict) {
console.log(`${key} ${newDict[key]}`)
}
... or ... for (const [key, value] of Object.entries(newDict)) {
console.log(`${key} ${value}`)
}
Re:How to write X in both Python 3 and JavaScript
by settler4 on Friday September 14, 2018 @11:35AM
(#17987979)
It would be nice to have a sectioin with things that work differently in both languages. For example in python `if []:` evaluates to False while in javascript `if ([]) {` evaluates to True.
Re:How to write X in both Python 3 and JavaScript
by armandososa on Friday September 14, 2018 @01:05PM
(#17988915)
First: congratulations, this is very well done.
Second: I don't like seeing JavaScript code without semicolons. Maybe I am old, but it makes me nervous.
Re:How to write X in both Python 3 and JavaScript
by FlyingLawnmower on Friday September 14, 2018 @11:24AM
(#17987864)
Thanks for the site! Do you have thoughts on removing all the print/console.logs calls on every line? It feels like there's a lot of visual noise there, that can still be met with the comments showing state/output.
Re:How to write X in both Python 3 and JavaScript
by plopz on Friday September 14, 2018 @11:27AM
(#17987896)
I might suggest replacing javascript's parseInt() with Number()
Re:How to write X in both Python 3 and JavaScript
by Sir_Cmpwn on Friday September 14, 2018 @10:46AM
(#17987550)
Consider renaming to "How to write $X in both Python 3 and JavaScript" to avoid the confusion with X11 that others have pointed out? Nice article!
Re:How to write X in both Python 3 and JavaScript
by fogetti on Friday September 14, 2018 @11:34AM
(#17987968)
I really like the page. I really enjoyed going through the list. One suggestion would be to add python/javascript generator functions to the list.
Re:How to write X in both Python 3 and JavaScript
by sharemywin on Friday September 14, 2018 @10:36AM
(#17987482)
Very cool reference.
Re:How to write X in both Python 3 and JavaScript
by zitterbewegung on Friday September 14, 2018 @10:37AM
(#17987488)
Add Python 2 and or another language that has a static type system like C.
Re:How to write X in both Python 3 and JavaScript
by laumars on Friday September 14, 2018 @10:42AM
(#17987515)
I think anyone who hasn't learned to do X in Python already really should be better off learning it in Python 3 rather than a version of the language which is in the process of deprecation.