metarhia / metasync

Asynchronous Programming Library for JavaScript & Node.js
https://metarhia.com
MIT License
206 stars 35 forks source link

Alternative syntax #373

Open tshemsedinov opened 6 years ago

tshemsedinov commented 6 years ago

Syntax idea by @primeare, modifications by @tshemsedinov

const { μ } = require('metasync');
const f = μ(f1)(f2, μ(f3)(f4), f5)(f6);
f((err, result) => {
  console.dir(err, result);
});

Known problems:

lundibundi commented 6 years ago

I don't like the idea of using utf8 symbol you can't easily write from the keyboard and have to copy paste (μ). As for the syntax, I find just using arrays much clearer as to what's going on. This syntax is cluttered with parentheses that hide the actual meaning too much imo. Perhaps at least:

const { seq, par } = require('metasync');
const f = seq(f1, par(f2, seq(f3, f4), f5), f6);
f((err, result) => {
  console.dir(err, result);
});
tshemsedinov commented 6 years ago

I saved proposal from @primeare here just for notes, @lundibundi We have no solution to distinguish sequential composition and composed function call, because f have a same contract with f6, so should we compose or start execution on next (). But maybe it will be used in future or will give inspiration to someone.

tshemsedinov commented 6 years ago

@lundibundi I like arrays too, because it all other syntax will finally save calls to data structures (most likely those arrays) and then will iterate executing seq/par. Arrays are enough expressive so no need to build arrays implicitly.

aqrln commented 6 years ago

@lundibundi

I don't like the idea of using utf8 symbol you can't easily write from the keyboard and have to copy paste (μ).

FWIW, even if you don't setup AltGr or whatever it's called on your system (which you probably do, because you can't even enter em-dashes (—) from the keyboard otherwise; on Macs, for example, it is set up by default, so µ is just Option+m), you can enable Greek input method and enter µ as switch to Greek, m, switch back to English.

I agree that a non-ASCII name shouldn't be the only one for a function, but a convenient Unicode alias does sound like a good idea to me.

lundibundi commented 6 years ago

Well, if it's an alias I'm fine. I don't want a unicode symbol to be the only way of accessing the function.

tshemsedinov commented 6 years ago

@primeare why [2, 3] ? What does it mean? All returning from μ functions will have callback-last contract and all callbacks will have error-first.

tshemsedinov commented 6 years ago

Do you mead μ(f1) will return single-argument function (callback) => () like this?

tshemsedinov commented 6 years ago

@primeare please prepare PR so we can test it in CI and compare with current implementation syntactically and by performance.