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.
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
# ==> 15
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.
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.
# 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
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.
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).
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.
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.
Me too. On any given day, I can be using any of a half dozen languages (C#, powershell, javascript, bash, perl, python, c++, java - our codebase is all over the place). It gets hard to keep the syntax straight on all of them.
You might find Rosetta Code to be useful - it's a wiki site with hundreds of examples, all with solutions in dozens of languages for comparison.
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!
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
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.
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:
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'?
Common mistake when writing a sorting function, a-b is not safe in languages with integer overflow because it will give you incorrect sorting for large numbers. In javascript where everything is double you're probably safe from that kind of error but maybe NaN can give you problems instead?
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?
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.
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)
In the past I've enjoyed using cheatsheets to get a quick feel for the syntax of a language. It's nice to see a less familiar language next to a more familiar one.
I used a resource similar to this to get started with Javascript almost a decade ago. Reading through the examples start to finish is a decent intro to the basics.
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
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.
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.
83 comments
[ 4.9 ms ] story [ 159 ms ] threadPerhabs just personal preference but I'd use
instead of 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:
I'm not a big fan of it honestly, the function style seems more consistent and easier to read to me.Although this is a case where idiomatic python is different from idiomatic js.
In js:
while in python:Second: I don't like seeing JavaScript code without semicolons. Maybe I am old, but it makes me nervous.
Cool site :P
Python: https://github.com/python-xlib/python-xlib
Node.js: https://github.com/sidorares/node-x11
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.
I particularly enjoyed seeing that you used snake case in Python and camel case in JavaScript. Great attention to detail.
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
[1]: https://wiki.archlinux.org/index.php/Pacman/Rosetta
javascript:
python: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.
`a - b` produces the same results, so equally valid?
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.:
‘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.
[1] https://en.wikipedia.org/wiki/Polyglot_(computing)
It's an IDE that lets you code simultaneously in several different languages https://ide.onelang.io/