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?
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'?
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?
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
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.
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.
# 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
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
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.
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.
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.
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).
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.
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.
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.
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.
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
javascript:
python: