You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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. :)
The text was updated successfully, but these errors were encountered:
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
changed the title
Idea: Remove syntax clutter with infinite currying
Idea: Simplify syntax with infinite currying
Mar 18, 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.
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:
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 turnFor
into a callable that returns another instance of itself. It could then be used like this:Advantages:
Disadvantages:
Like I said, it's just an idea, I'd be curious what your thoughts are. :)
The text was updated successfully, but these errors were encountered: