Hacker News new | ask | show | jobs
by jond3k 3594 days ago
To solve the readability issue, why not use Babel to transpile JS expressions into the equivalent syntax?

To generate the first example in the readme:

  import {solve} from 'logic'
  
  function father(x,y) {
      return x =='mcbob' && y == 'bob' ||
          x =='bob' && y == 'bill';
  }
  
  solve(father) // a noop function used as a marker by babel
3 comments

Yeah, the transformation itself shouldn't be bad.

Made a Babel transform real quick for this particular example:

http://astexplorer.net/#/ZT6HYai08w

If you're going ES6, why not just:

  import {solve} from 'logic'
  
  solve((x,y) => x === "mcbob" && y === "bob" || x === "bob" && y === "bill");
Just because you can use the shorthand syntax, doesn't mean you always should.
I wouldn't say it's that bad. A curly brace would make it a bit clearer though.
I prefer my code to be more than "it's not that bad" when it comes to readability.
I feel like that just obfuscates it. You can no longer dynamically use it, and it's completely unobvious what javascript expressions are supported or not.
Babel has to turn it into valid JS for it to work so you'd still have the 'and' and 'or' functions. In the bottom right corner of Hzoo's example you can see what I mean (http://astexplorer.net/#/ZT6HYai08w)

It should be more obvious what expressions are supported when it's validated by a Javascript parser. This is in contrast to the 'dynamic' approach which has no problem with the following until runtime:

    and(gherkin(or()))