Live data from Hacker News

Leaving Python for JavaScript

hire.jonasgalvez.com.br

31–40 of 112 posts

Re: Leaving Python for JavaScript

#31
post #18

> there's no acceptable way to pass a function body to another in Python. There are sentences which are telling a huge incompetence about the person using these. This sounds the same story to me as "We used tech X, but X is shit/can't do something so we switched to Y, and Y is awesome and fast." Where the person switching was just incompetent with X but it would have been perfectly solvable and they just do a totally…

I really like chaining Promises in JavaScript. The Python way to do this doesn't seem as powerful (what if you want to inject a parallel set of async operations at some point in the chain?): https://stackoverflow.com/questions/43325501/how-do-i-write-... - JS also supports async/await but the advantage of JS is that you can return promises (or parallel combinations of multiple promises) from inside promises so any link in the promise chain can essentially inject new links in the chain in its own place (so the chain can change dynamically based on the results of previous async operations).

You can build really complex dynamic async chains in JS very easily because of this. It's hard to explain why it's so nice until you try it.

In JS, I can do something like:

Promise.resolve().then(() => {

  let innerPromiseChain = serialOperation1()
  .then(serialOperation2);

  if (somethingIsTrue) {
    // Only inject serialOperation3 at the end of the
    // innerPromiseChain if somethingIsTrue
    return innerPromiseChain.then(serialOperation3);
  }
  // else return the inner chain without serialOperation3
  return innerPromiseChain;
}) .then((resultOfLastSerialOperation) => {

  // resultOfLastSerialOperation could be the result of
  // either serialOperation2 or serialOperation3 depending
  // on the value of somethingIsTrue

  // parallelOperation1 and parallelOperation2 will be
  // executed in parallel.
  return Promise.all(parallelOperation1(), parallelOperation2());
}) .then((parallelOp1Result, parallelOp2Result) => {

  // This will be called once the previous parallel
  // operations have both finished (therefore the
  // whole chain has been processed).
});

Re: Leaving Python for JavaScript

#32
post #18

> there's no acceptable way to pass a function body to another in Python. There are sentences which are telling a huge incompetence about the person using these. This sounds the same story to me as "We used tech X, but X is shit/can't do something so we switched to Y, and Y is awesome and fast." Where the person switching was just incompetent with X but it would have been perfectly solvable and they just do a totally…

No one likes callback hell but I believe they meant that you can just pass a function expression directly. No need to define a named function. It's the difference between: let doubled = nums.map(x => x * 2) and this: def double(x): return x * 2 doubled = map(double, nums) (my Python is pretty rusty so maybe there's a better way of doing that)

You can do the exact same thing in Python, and this is not any new feature:

    doubled = map(lambda x: x * 2, nums)
JS it's just not as explicit with what you're exactly doing.

Re: Leaving Python for JavaScript

#33
post #18

> there's no acceptable way to pass a function body to another in Python. There are sentences which are telling a huge incompetence about the person using these. This sounds the same story to me as "We used tech X, but X is shit/can't do something so we switched to Y, and Y is awesome and fast." Where the person switching was just incompetent with X but it would have been perfectly solvable and they just do a totally…

No one likes callback hell but I believe they meant that you can just pass a function expression directly. No need to define a named function. It's the difference between: let doubled = nums.map(x => x * 2) and this: def double(x): return x * 2 doubled = map(double, nums) (my Python is pretty rusty so maybe there's a better way of doing that)

> (my Python is pretty rusty so maybe there's a better way of doing that)

    return [x*2 for x in nums]

Re: Leaving Python for JavaScript

#34
post #18

> there's no acceptable way to pass a function body to another in Python. There are sentences which are telling a huge incompetence about the person using these. This sounds the same story to me as "We used tech X, but X is shit/can't do something so we switched to Y, and Y is awesome and fast." Where the person switching was just incompetent with X but it would have been perfectly solvable and they just do a totally…

No one likes callback hell but I believe they meant that you can just pass a function expression directly. No need to define a named function. It's the difference between: let doubled = nums.map(x => x * 2) and this: def double(x): return x * 2 doubled = map(double, nums) (my Python is pretty rusty so maybe there's a better way of doing that)

[deleted]

Re: Leaving Python for JavaScript

#35

Just in case the author is present in the thread: Your blog is extremely difficult to read on wide screen monitors. The entire left 50% is basically just blank space. My suggestion is to make the left side maybe 20% width on desktop or less. Also, regarding the article content: There really aren't any good reasons presented here about why you moved from Python to Javascript for back-end web development. It seems that…

> Functions are first-class in python and can be passed as arguments to other functions. I'm not sure I understand what you mean with the above statement.

I believe the author is referring to inline definition:

    setTimeout(function(){alert("Timed out!");}, 5000)
You can do something similar with lambdas in Python, but the allowed syntax is relatively limited compared to a full function definition. You could also define an inline function, but it'd be in a separate statement from the call you make with it. Unfortunately Python doesn't allow for creating and returning a first-class function in a single expression.

Re: Leaving Python for JavaScript

#36

I want to like JavaScript, but the type coercion land mines and the lack of a real numeric tower make me a little queasy. When I have to use JS I stick to the transpile-to-js languages mostly for those reasons.

Is TypeScript one of your goto "transpile-to-js" languages? I'm a Python developer who is learning to love TypeScript. With TypeScript I feel like I can write maintainable, collaborateable JS.

Re: Leaving Python for JavaScript

#37
post #18

> there's no acceptable way to pass a function body to another in Python. There are sentences which are telling a huge incompetence about the person using these. This sounds the same story to me as "We used tech X, but X is shit/can't do something so we switched to Y, and Y is awesome and fast." Where the person switching was just incompetent with X but it would have been perfectly solvable and they just do a totally…

No one likes callback hell but I believe they meant that you can just pass a function expression directly. No need to define a named function. It's the difference between: let doubled = nums.map(x => x * 2) and this: def double(x): return x * 2 doubled = map(double, nums) (my Python is pretty rusty so maybe there's a better way of doing that)

    doubled = [x * 2 for x in nums]

Re: Leaving Python for JavaScript

#38

Earlier quoted context omitted.

I'm guessing, since the author used the arrow shorthand notation in Javascript as an example, that the limitations of Python's lambdas are the chief complaint

It's more of a feature than an arbitrary limitation I think. Python philosophy, 'explicit over implicit', and that stuff.

An intentional limitation, then :P

I tend to agree with the Python philosophy, that it's usually better to extract and name a function that to declare it inline. The `asyncio` module and `await` keywords go a long way in eliminating most use cases where I'd still prefer an anonymous function, for arbitrary callbacks.

Re: Leaving Python for JavaScript

#39

Just in case the author is present in the thread: Your blog is extremely difficult to read on wide screen monitors. The entire left 50% is basically just blank space. My suggestion is to make the left side maybe 20% width on desktop or less. Also, regarding the article content: There really aren't any good reasons presented here about why you moved from Python to Javascript for back-end web development. It seems that…

Python has lambda but they are one liners.

There is no way to do write this in Python:

   someFunc( _ => {
       // this is 
       // a function 
       // with multiple lines
   })
Having to define a named function to use it as a callback is a pain in the ass.

Python has an excellent standard library but the language itself is pretty mediocre IMHO. Classes are an afterthought thus verboses, as so is "functional programming" in Python.

JS biggest issue is that it is not strongly typed enough. But ES2015 makes it really pleasant to use.

Re: Leaving Python for JavaScript

#40

I started programming in JS then moved to Python. It seems like JS has become very, very complicated. I look at his post and I am lost... I remember Knockout.js and Node in the v0.8 days. I mean look at this: dotenv (lets you load the environment from an .env file), axios (HTTP client library), cheerio (HTML parsing library), bcrypt (password hashing), co-body (HTTP body parser), co-busboy (HTTP multipart parser), js…

> What the hell is all that? Libraries of code. > I am not saying he is wrong... Good, because they are right. > I am just saying that I don't have the capacity to play in that playground Good, because you are right. > I am also asking if any of those libraries will be around in a year? Yes. Some have been around for years already. Listen, it's fine that you are using Python. It's fine that the OP is using JavaScript…

I suspect you are being downvoted for presuming to know what the parent's capacity is.
Post reply on HN