Sort example doesn't take into account Javascript does lexicographical sorting? javascript: someList = [ 40, 2, 1, 3, 7, 99] someList.sort() Array(6) [ 1, 2, 3, 40, 7, 99 ] python: someList = [ 40, 2, 1, 3, 7, 99] sorted(someList) [1, 2, 3, 7, 40, 99]
How to write X in both Python 3 and JavaScript
41–50 of 84 posts
Re: How to write X in both Python 3 and JavaScript
#42Re: How to write X in both Python 3 and JavaScript
#43Earlier quoted context omitted.
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.
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.
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
# ==> 15Re: How to write X in both Python 3 and JavaScript
#44Earlier quoted context omitted.
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.
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
#45Creator here! Let me know if you have any suggestions.
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
#46Creator here! Let me know if you have any suggestions.
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() # FalseRe: How to write X in both Python 3 and JavaScript
#47Creator here! Let me know if you have any suggestions.
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
#48It's an IDE that lets you code simultaneously in several different languages https://ide.onelang.io/