Live data from Hacker News

How to write X in both Python 3 and JavaScript

sayazamurai.github.io

11–20 of 84 posts

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

#15

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

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.

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

#17

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

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.

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

#18

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).
Post reply on HN