Live data from Hacker News

How to write X in both Python 3 and JavaScript

sayazamurai.github.io

41–50 of 84 posts

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

#41

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]

That's a good point. Although there is another sorting example which is using the correct format. But yes, you are indeed right.

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

#42
post #11

http://hyperpolyglot.org/ is a great website with a similar idea.

https://learnxinyminutes.com/ is another website with a similar aim.

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

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

#43
post #17

Earlier quoted context omitted.

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.

https://www.pyret.org/ does it like this:

  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

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

#44
post #29

Earlier quoted context omitted.

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.

You can also ‘return’ the outer function from within a ‘for...of’ which is pretty useful imo.

And you can use await in the context of an outer async function, which is a huge advantage in reducing code complexity.

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

#45

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

Instead of...

    Object.entries(newDict).forEach(element => {
      console.log(`${element[0]} ${element[1]}`)
    })
... you could write ...

    for (const key in newDict) {
      console.log(`${key} ${newDict[key]}`)
    }
... or ...

    for (const [key, value] of Object.entries(newDict)) {
      console.log(`${key} ${value}`)
    }

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

#46

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

Truthiness!

Although this is a case where idiomatic python is different from idiomatic js.

In js:

  // same:
  !! ""   // false
  !! 0    // false
  !! 1    // true
  !! null // false  

  // different:
  !! {}  // true
  !! []  // true
while in python:

  # 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

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

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