
How to write X in both Python 3 and JavaScript - chibicode
https://sayazamurai.github.io/python-vs-javascript/
======
alyandon
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]

~~~
52-6F-62
It depends on whether or not you pass it a basic comparator function, eg:

    
    
        someArray.sort((a,b) => a - b);
    

But yeah the example would have needed this for the less complex example as
well to be accurate.

~~~
orf
Shouldn't that be `a < b`?

~~~
Ajedi32
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.

~~~
kristianp
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?

~~~
jwilk
(I'm assuming you meant "a >= b", not "a => b".)

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

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

~~~
derimagia
My go-to site is usually
[https://learnxinyminutes.com/](https://learnxinyminutes.com/)

------
aninteger
How to write using X in both languages:

Python: [https://github.com/python-xlib/python-
xlib](https://github.com/python-xlib/python-xlib)

Node.js:
[https://github.com/sidorares/node-x11](https://github.com/sidorares/node-x11)

~~~
donio
A couple more native X11 client implementations:

Common Lisp:
[https://github.com/sharplispers/clx](https://github.com/sharplispers/clx)

Go: [https://github.com/BurntSushi/xgb](https://github.com/BurntSushi/xgb)

Any others?

(Looking for implementations of the X11 protocol in $LANGUAGE, not bindings to
the C Xlib)

~~~
donio
One more that I use:

Emacs Lisp: [https://github.com/ch11ng/xelb](https://github.com/ch11ng/xelb)

------
marsrover
When I read the headline I was expecting the X windowing system. When I
clicked the article and saw “junior programmer” I thought this is about to be
impressive.

Cool site :P

------
fabiomaia
Very cool! Reminds me of
[http://youmightnotneedjquery.com/](http://youmightnotneedjquery.com/)

I particularly enjoyed seeing that you used snake case in Python and camel
case in JavaScript. Great attention to detail.

------
sayazamurai
Creator here! Let me know if you have any suggestions.

~~~
mschuetz
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.

~~~
tormeh
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.

~~~
symlinkk
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.

------
mvilim
[http://hyperpolyglot.org/](http://hyperpolyglot.org/) is a great website with
a similar idea.

~~~
neillyons
[https://learnxinyminutes.com/](https://learnxinyminutes.com/) is another
website with a similar aim.

~~~
agumonkey
I quite like this as super thin intro. So far I always fonud it motivating
enough to jump into deeper articles/books.

------
ramses0
No mention of [http://rosettacode.org](http://rosettacode.org), tsk tsk.
(specifically:
[http://rosettacode.org/wiki/Category:Programming_Tasks](http://rosettacode.org/wiki/Category:Programming_Tasks))

~~~
arendtio
Hehe, I didn't know that but the page reminded me of of the Arch Linux Rosetta
Stone page[1] instantly ;-)

[1]:
[https://wiki.archlinux.org/index.php/Pacman/Rosetta](https://wiki.archlinux.org/index.php/Pacman/Rosetta)

------
thomasfedb
A weird reference imo. Perhaps useful to build as a learning experience but I
wouldn't expect who needs if/else included in a cheat sheet to be ready to
learn to languages.

~~~
fabiomaia
The value is in finding what is the most idiomatic way to do something in
either language. For example if you're coming from Python and you're used to
sum() it is useful to check this reference for the most idiomatic way to do it
in JavaScript before doing it by hand with anonymous functions and reduce or
looping through the list (as is the case unfortunately).

~~~
LyndsySimon
Agreed. The value isn’t in it being a tutorial, it’s in it being a quick
overview of a language’s idioms as they apply to common tasks.

------
dkonieczek
Do people actually use cheat sheets like this? IMO, the first stackoverflow
result in google is generally what I'm looking for and quicker - especially
for common things and common languages such as the things on this page. Even
if you have the page bookmarked, you still need to find/search/scroll for the
specific thing you're looking for. Also, with Alfred you can google anything
even quicker by not having to cmd+tab to the browser.

~~~
cirgue
Maybe not as a reference, but it's certainly helpful to browse if you're going
from one language to another (I know python and I'm learning Javascript, this
probably saved me a few google searches)

------
feniv
Reminds me of this OneLang HN post from several months ago -
[https://news.ycombinator.com/item?id=16415051](https://news.ycombinator.com/item?id=16415051)

It's an IDE that lets you code simultaneously in several different languages
[https://ide.onelang.io/](https://ide.onelang.io/)

------
tobiasz-cudnik
Reminds me of PLEAC - Perl Cookbook translated to >10 langs
[http://pleac.sourceforge.net](http://pleac.sourceforge.net)

------
nayuki
When I publish code about algorithms and data structures to my web site, I
usually offer multiple language versions. Why? Because understanding the
computer science theory and math proofs is a big effort. Writing and debugging
my first implementation in any language is a big effort. But porting that code
to a bunch of languages is comparatively easy and is almost a thoughtless
mechanical process.

------
basilgohar
I thought this was going to be a polyglot[1] demonstration of writing code for
X Windows. However, I was not disappointed in what I found!

[1]
[https://en.wikipedia.org/wiki/Polyglot_(computing)](https://en.wikipedia.org/wiki/Polyglot_\(computing\))

------
anticensor
I expected a Python/JS polyglot :)

------
Too
Impressive to see almost all JS examples being as short as the python ones.
Python is normally valued for being short and concise but modern JS is really
catching up here.

------
aavotins
I had such high hopes when I opened the arsenal. I anticipated it would an
article of how to write code that's both JavaScript and Python syntactically
correct.

------
another-cuppa
What's the point of having print or console.log around everything? Just have
one box with "print to standard output" and those two at the top.

------
sheikheddy
I love it! Reminds me of
[https://www.interviewbit.com](https://www.interviewbit.com)

------
ingen0s
Excellent educational tool - will be passing this around to colleagues who
need it. Thanks!

~~~
nanna
Same here!

------
undershirt
this is very good. i had it in my head that there was no "extend" function in
JS, and didn't realize "push(...array)" was possible. thank you for the super
detailed guide

------
tracker1
FizzBuzz example isn't correct

------
enitihas
I was expecting to see a leftpad comparison too :D

