6 ms·
How to write X in both Python 3 and JavaScript
- analpaper 8y agobut but no need to write both, just one and compile the other (http://piter.io/projects/js2py http://piter.io/projects/js2py, http://pyjs.org/ http://pyjs.org/, etc..)
- ChrisSD 8y agoAs 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.
- chooseaname 8y agoMe 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.
- abstractbeliefs 8y agoYou 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! https://www.rosettacode.org/wiki/Rosetta_Code 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 https://www.rosettacode.org/wiki/Dijkstra%27s_algorithm
- derimagia 8y agoMy go-to site is usually https://learnxinyminutes.com/ https://learnxinyminutes.com/
- sayazamurai 8y agoCreator here! Let me know if you have any suggestions.
- Sir_Cmpwn 8y agoConsider renaming to "How to write $X in both Python 3 and JavaScript" to avoid the confusion with X11 that others have pointed out? Nice article!
- sharemywin 8y agoVery cool reference.
- zitterbewegung 8y agoAdd Python 2 and or another language that has a static type system like C.
- laumars 8y agoI 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.
- nanna 8y agoEither you forgot to mention that this is a personal todo, or you're trolling the author?
- laumars 8y agoI genuinely love the design of the page. It has personality without compromising readability. This is something a lot of people struggle to get right.
- mschuetz 8y agoNice 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.
- thomasfedb 8y agoA 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 8y agoThe 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 8y agoAgreed. 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.
- marsrover 8y agoWhen 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
- aninteger 8y agoHow 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
- de_Selby 8y agoThis is what I actually thought the article was going to be about!
- ISL 8y agoThis what I expected from the title.
- donio 8y agoA 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 8y agoOne more that I use: Emacs Lisp: https://github.com/ch11ng/xelb https://github.com/ch11ng/xelb
- 52-6F-62 8y agoHaha, 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.
- bateller 8y agoAt least I wasn't the only one lol
- fabiomaia 8y agoVery 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.
- mvilim 8y agohttp://hyperpolyglot.org/ http://hyperpolyglot.org/ is a great website with a similar idea.
- ingen0s 8y agoExcellent educational tool - will be passing this around to colleagues who need it. Thanks!
- nanna 8y agoSame here!
- deleted 8y ago[deleted]
- enitihas 8y agoI was expecting to see a leftpad comparison too :D
- deleted 8y ago[deleted]
- sheikheddy 8y agoI love it! Reminds me of https://www.interviewbit.com https://www.interviewbit.com
- ramses0 8y agoNo 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 8y agoHehe, 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
- alyandon 8y agoSort 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]
- fogetti 8y agoThat's a good point. Although there is another sorting example which is using the correct format. But yes, you are indeed right.
- 52-6F-62 8y agoIt 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 8y agoShouldn't that be `a < b`?
- 52-6F-62 8y agoThat will give you a sequence of decreasing numbers. `a-b` will work just as `a > b`. 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.
- jwilk 8y agoa > b is not a consistent comparison function. The behavior of sort() with such a function is implementation-defined.
- 52-6F-62 8y agoAgreed. I wouldn't use it the way shown here. I would make sure to return a hard negative, zero, or one in any case. I was surprised when I read into how many different ways `Array.prototype.sort()` is implemented across environments.
- basilgohar 8y agoI 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)
- feniv 8y agoReminds 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/
- dkonieczek 8y agoDo 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 8y agoMaybe 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)
- andyfleming 8y agoIn 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.
- jamesgeck0 8y agoI 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.
- undershirt 8y agothis 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
- aavotins 8y agoI 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.
- deleted 8y ago[deleted]
- tobiasz-cudnik 8y agoReminds me of PLEAC - Perl Cookbook translated to >10 langs http://pleac.sourceforge.net http://pleac.sourceforge.net
- anticensor 8y agoI expected a Python/JS polyglot :)
- deleted 8y ago[deleted]
- nayuki 8y agoWhen 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.
- another-cuppa 8y agoWhat'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.
- tracker1 8y agoFizzBuzz example isn't correct
- Too 8y agoImpressive 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.