How to write X in both Python 3 and JavaScript
sayazamurai.github.io
sayazamurai.github.io
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] someArray.sort((a,b) => a - b);
But yeah the example would have needed this for the less complex example as well to be accurate.If `a-b` returns a negative number, the result will be treated as -1. If the numbers are the same, then you end up with 0, and so on.
I don't know what's faster in that case.
I was surprised when I read into how many different ways `Array.prototype.sort()` is implemented across environments.
someList.sort((a,b) => a => b)
Would be better, because it produces a stable sort?`a - b` produces the same results, so equally valid?
let arr = [
{number: 2, name: 'a'},
{number: 1, name: 'b'},
{number: 1, name: 'c'},
]
Now assume we want to sort arr with a compare function, and our sort function expects the compare function to return a boolean, not an integer. let compare = (a,b) => a.number => b.number
compare(arr[0], arr[1]) //=> true, so the object named 'a' comes after 'b'
compare(arr[1], arr[2]) //=> true, so the object named 'b' comes after 'c'?
See the problem?That's still not a consistent comparison function, so the results are implementation-defined.
To be consistent, it's required, among others, that if cmp(a, b) == 0 then cmp(b, a) == 0. For "a >= b", this is not always true, e.g.:
» cmp(0, 1) == 0
true
» cmp(1, 0) == 0
false‘a-b’ is safe enough in JS if you always know the input will be within a safe range regarding integer sizes and types.
NaN can give you all kinds of trouble so I’d usually test for that in a real case, either before the sort or in the comparator—if necessary.
Edit: I'm actually truly grateful, I'm sure this bit of knowledge saved me some of my hair in the future.
I know all the syntax in them but I'll be damned if I can ever remember which language uses which until I've settled back in to things.
ikewise, I felt a little let down. On the flipside, it's an excellent opportunity to bring Rosetta Code to people's attention - a wiki with hundreds of example problems, each solved in many languages for comparison!
https://www.rosettacode.org/wiki/Rosetta_Code
I find it invaluable in that sort of mid-learning level of a new language, where I have the syntax sorted, but I need lots of programs small enough to hold in my head, but also large enough to show off all the features, to learn and read. Here's Dijkstra, for example: https://www.rosettacode.org/wiki/Dijkstra%27s_algorithm
Common Lisp: https://github.com/sharplispers/clx
Go: https://github.com/BurntSushi/xgb
Any others?
(Looking for implementations of the X11 protocol in $LANGUAGE, not bindings to the C Xlib)
Emacs Lisp: https://github.com/ch11ng/xelb
Now I'm kind of glad you responded in kind because I'd seen python-xlib but not the node one. That sounds like a bit of fun and punishment at the same time.
Cool site :P
I particularly enjoyed seeing that you used snake case in Python and camel case in JavaScript. Great attention to detail.
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.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. 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 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}`)
}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() # FalseSecond: I don't like seeing JavaScript code without semicolons. Maybe I am old, but it makes me nervous.
It's an IDE that lets you code simultaneously in several different languages https://ide.onelang.io/