Live data from Hacker News

How to write X in both Python 3 and JavaScript

sayazamurai.github.io

71–80 of 84 posts

Re: How to write X in both Python 3 and JavaScript

#72

How to write using X in both languages: Python: https://github.com/python-xlib/python-xlib Node.js: https://github.com/sidorares/node-x11

Haha, I also expected this. 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.

At least I wasn't the only one lol

Re: How to write X in both Python 3 and JavaScript

#74
post #17

Earlier 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.

That sounds a lot like Python list comprehensions

Re: How to write X in both Python 3 and JavaScript

#75
post #13

As someone who is often switching languages, I find these sorts of cheatsheets useful. 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.

My go-to site is usually https://learnxinyminutes.com/

Re: How to write X in both Python 3 and JavaScript

#76
post #62
post #58

Earlier quoted context omitted.

Shouldn't that be `a < b`?

No, the compare function needs to be able to indicate when two values are equal, not just when one is smaller/larger than the other. Otherwise your sort function will return inconsistent results for compare(a, b) and compare(b, a) when both values are equal.

So

    someList.sort((a,b) => a => b)
Would be better, because it produces a stable sort?

`a - b` produces the same results, so equally valid?

Re: How to write X in both Python 3 and JavaScript

#77

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]

I swear this language gets worse every time I learn something new about it.

Re: How to write X in both Python 3 and JavaScript

#78

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]

To me this seems like a bug, or at best an incomplete implementation. Let's fix it in the next version of JavaScript, okay? I suppose someone's code will break, but was it worth preserving?

Re: How to write X in both Python 3 and JavaScript

#79
post #62

Earlier quoted context omitted.

No, the compare function needs to be able to indicate when two values are equal, not just when one is smaller/larger than the other. Otherwise your sort function will return inconsistent results for compare(a, b) and compare(b, a) when both values are equal.

So someList.sort((a,b) => a => b) Would be better, because it produces a stable sort? `a - b` produces the same results, so equally valid?

No. `(a,b) => a => b` cannot produce a stable sort in all scenarios. Would theoretically work fine for integers since there's no difference between any two instances of the same number, but consider:

  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?

Re: How to write X in both Python 3 and JavaScript

#80
post #33

How to write using X in both languages: Python: https://github.com/python-xlib/python-xlib Node.js: https://github.com/sidorares/node-x11

A couple more native X11 client implementations: 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)

One more that I use:

Emacs Lisp: https://github.com/ch11ng/xelb

Post reply on HN