Live data from Hacker News

'Do' More with 'Run'

maxgreenwald.me

1–10 of 33 posts

Re: 'Do' More with 'Run'

#2
In the "Use as a `do` expression" section, the example which uses `run` does not need the `else` cases and could be simplified:

  function doWork() {
    const x = run(() => {
      if (foo()) return f();
      if (bar()) return g();
      return h();
    });
  
    return x * 10;
  }

Re: 'Do' More with 'Run'

#3
Feels like code golf. The two run examples are basically the same, but now I have to reason about what ‘run’ is and I’ve made my stack trace more complicated.

I am in love with “everything is an expression” from my time with Rust. I regularly use a ‘let’ and wish I could just have the entire conditional feed into a ‘const’ given it’s never going to change after the block of code responsible for assignment.

I wish there were more generations of ‘use strict’ so that we could make bolder, more breaking changes to the language without breaking stuff or dying in a horrible fire of “just roll your own dialect using compiler plugins.”

Re: 'Do' More with 'Run'

#4
post #2

In the "Use as a `do` expression" section, the example which uses `run` does not need the `else` cases and could be simplified: function doWork() { const x = run(() => { if (foo()) return f(); if (bar()) return g(); return h(); }); return x * 10; }

Can also get rid of the `run` and move parens to simplify even further:

  function doWork() {
    const x = () => {
      if (foo()) return f();
      if (bar()) return g();
      return h();
    };
  
    return x() * 10;
  }

Re: 'Do' More with 'Run'

#6
post #2

In the "Use as a `do` expression" section, the example which uses `run` does not need the `else` cases and could be simplified: function doWork() { const x = run(() => { if (foo()) return f(); if (bar()) return g(); return h(); }); return x * 10; }

Can also get rid of the `run` and move parens to simplify even further: function doWork() { const x = () => { if (foo()) return f(); if (bar()) return g(); return h(); }; return x() * 10; }

And a step further into the past, no need for a lambda - I find this clearer:

  function doWork() {
    function calcX() {
      if (foo()) return f();
      if (bar()) return g();
      return h();
    }

    return calcX() * 10;
  }
Where "calc" can be "gen[erate]" or "find" or at least more descriptive.

Re: 'Do' More with 'Run'

#8

Feels like code golf. The two run examples are basically the same, but now I have to reason about what ‘run’ is and I’ve made my stack trace more complicated. I am in love with “everything is an expression” from my time with Rust. I regularly use a ‘let’ and wish I could just have the entire conditional feed into a ‘const’ given it’s never going to change after the block of code responsible for assignment. I wish the…

Not even code golf given that the example is a single character longer. Naming it "r" would be code golf (and "r" is definitely worse than "run" for reasoning).

Re: 'Do' More with 'Run'

#9
post #2

In the "Use as a `do` expression" section, the example which uses `run` does not need the `else` cases and could be simplified: function doWork() { const x = run(() => { if (foo()) return f(); if (bar()) return g(); return h(); }); return x * 10; }

Can also get rid of the `run` and move parens to simplify even further: function doWork() { const x = () => { if (foo()) return f(); if (bar()) return g(); return h(); }; return x() * 10; }

Presumably this is a simplified example and "x" is intended to be used more than once?

Re: 'Do' More with 'Run'

#10
I like it! Another one-liner I'm constantly adding is

    const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
Whatever runs the main function should probably do more, like handling rejections.
Post reply on HN