单子(monad)计算在 JS 中很快变得困惑:
const chain = fm => xs =>
xs.reduce((acc, x) => acc.concat(fm(x)), []);
const of = x => [x];
const main = xs => ys => zs =>
chain(x =>
x === 0
? []
: chain(y =>
chain(z => [[x, y, z]]) (zs)) (ys)) (xs);
console.log("run to completion",
main([1, 2]) (["a", "b"]) ([true, false]));
console.log("short circuiting",
main([0, 2]) (["a", "b"]) ([true, false]));
在 Haskell 中,
do
符号可用于隐藏嵌套的函数调用。然而,do
是一种 Javascript 缺乏的编译时技术。生成器函数似乎很合适,但它们不适用于提供优先选择的 monad。所以我一直在寻找替代方案很长一段时间,最近想出了一种 monadic applicator 来解开嵌套计算:
const chain = fm => xs =>
xs.reduce((acc, x) => acc.concat(fm(x)), []);
const of = x => [x];
const id = x => x;
const infixM3 = (w, f, x, g, y, h, z) =>
f(x_ =>
w(x_, w_ => g(y_ =>
w_(y_, w__ => h(z_ =>
w__(z_, id)) (z))) (y))) (x);
const mainApp = xs => ys => zs => infixM3(
(x, k) =>
x === 0
? []
: k((y, k) =>
k((z, k) => [[x, y, z]])),
chain, xs,
chain, ys,
chain, zs);
console.log("run to completion",
mainApp([1, 2]) (["a", "b"]) ([true, false]));
console.log("short circuiting",
mainApp([0, 2]) (["a", "b"]) ([true, false]));
涂抹器在中缀位置模仿链条,因此得名。它基于局部延续,因此提升的函数需要一对分别由绑定(bind)值和延续组成的参数。如果不应用延续,则计算短路。它看起来很复杂,但实际上是一个机械过程。
将显式版本与抽象版本进行比较,我认为这是在可读性方面的改进:
chain(x =>
x === 0
? []
: chain(y =>
chain(z => [[x, y, z]]) (zs)) (ys)) (xs);
infixM3(
(x, k) =>
x === 0
? []
: k((y, k) =>
k((z, k) => [[x, y, z]])),
chain, xs,
chain, ys,
chain, zs);
但是,提升功能中的延续性以及涂抹器具有 arity 意识这一事实令人烦恼。此外,它看起来根本不像 do-notation。我们能否接近类似于
do
表示法的语法? 最佳答案
您可以使用 immutagen 库使用生成器编写 monadic 代码。
const monad = bind => regen => (...args) => function loop({ value, next }) {
return next ? bind(value, val => loop(next(val))) : value;
}(immutagen.default(regen)(...args));
const flatMap = (array, callback) => array.flatMap(callback);
const list = monad(flatMap);
const main = list(function* (xs, ys, zs) {
const x = yield xs;
if (x === 0) return [];
const y = yield ys;
const z = yield zs;
return [[x, y, z]];
});
console.log("run to completion", main([1, 2], ["a", "b"], [true, false]));
console.log("short circuiting", main([0, 2], ["a", "b"], [true, false]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://unpkg.com/immutagen@1.0.9/immutagen.js"></script>
如您所见,这也适用于非确定性 monad。但是,它有两个缺点。
尽管如此,即使您不使用生成器,用 JavaScript 编写 monadic 代码也不错。
const flatMap = (array, callback) => array.flatMap(callback);
const main = (xs, ys, zs) =>
flatMap(xs, x =>
x === 0 ? [] :
flatMap(ys, y =>
flatMap(zs, z =>
[[x, y, z]])));
console.log("run to completion", main([1, 2], ["a", "b"], [true, false]));
console.log("short circuiting", main([0, 2], ["a", "b"], [true, false]));
.as-console-wrapper { max-height: 100% !important; top: 0; }
归根结底,如果你想要两全其美,那么你要么需要使用一种编译为 JavaScript 的语言,要么使用像 Babel 或 sweet.js 这样的预处理器。
关于javascript - 有没有更好的方法来模仿 JS 中的 do 符号?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/60226639/