Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea: Simplify syntax with infinite currying #13

Open
philer opened this issue Mar 11, 2022 · 2 comments
Open

Idea: Simplify syntax with infinite currying #13

philer opened this issue Mar 11, 2022 · 2 comments

Comments

@philer
Copy link

philer commented Mar 11, 2022

This is just an idea, I haven't actually tried it.

Basically the point of the for comprehension is to reduce syntax clutter, i.e. to avoid deeply nested closures etc. However there are still quite a lot of special characters floating around in this example:

For._("dividend", success(42))
   ._("divisor", success(2))
   ._("divisorVerified", ({divisor}) => divisor != 0 ? success(divisor) : failure("Divisor must not be zero!"))
   .yield(({dividend, divisorVerified}) => dividend / divisorVerified)

That ._ doesn't really tell you much, it's just a way of starting a new line. So my idea is to remove the need for ._ on every line and instead turn For into a callable that returns another instance of itself. It could then be used like this:

For
  ("dividend", success(42))
  ("divisor", success(2))
  ("divisorVerified", ({divisor}) => divisor != 0 ? success(divisor) : failure("Divisor must not be zero!"))
  .yield(({dividend, divisorVerified}) => dividend / divisorVerified)

Advantages:

  • Less Syntax 💯

Disadvantages:

  • Might look even more like black magic.
  • I'm not sure if formatters like prettier will deal well with this.

Like I said, it's just an idea, I'd be curious what your thoughts are. :)

@philer
Copy link
Author

philer commented Mar 11, 2022

Another thought I had is that object parameters could be used to combine multiple steps, although I'm not sure if that makes readability better or worse:

For({
  dividend: success(42),
  divisor: success(2),
  divisorVerified: ({divisor}) => divisor != 0 ? success(divisor) : failure("Divisor must not be zero!"),
}).yield(({dividend, divisorVerified}) => dividend / divisorVerified)

In this case the implementation would rely on (1) each step having a unique name and (2) JS iterating object keys in insertion order (which is guaranteed since ES5 iirc).

@philer philer changed the title Idea: Remove syntax clutter with infinite currying Idea: Simplify syntax with infinite currying Mar 18, 2022
@p3et
Copy link
Owner

p3et commented Mar 21, 2022

Thanks for your feedback @philer! I totally agree that both versions look nicer than the current one. However, can they be implemented in a type-safe way? For example, it must be guaranteed that every arrow function parameter has the same type as the inner monad value at definition. For example, ({divisor}) => ... must be a number.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants