【问题标题】:Is there a better way to mimic do notation in JS?有没有更好的方法来模仿 JS 中的 do 表示法?
【发布时间】:2020-05-30 06:31:07
【问题描述】:

一元计算在 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。所以我一直在寻找一个替代方案,最近想出了一种单子应用程序,以便稍微解开嵌套计算:

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]));

涂抹器在中缀位置模仿链条,因此得名。它基于局部延续,因此提升函数需要一对分别由边界值和延续组成的参数。如果未应用延续,则计算短路。它看起来很复杂,但实际上是一个机械过程。

将显式版本与抽象版本进行比较,我认为这是在可读性方面的改进:

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 表示法的语法?

【问题讨论】:

标签: javascript functional-programming monads do-notation


【解决方案1】:

您可以使用immutagen 库使用生成器编写一元代码。

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。但是,它有两个缺点。

  1. 效率低下,因为需要创建和重放多个生成器,导致时间复杂度呈二次增长。
  2. 它仅适用于纯单子和纯计算,因为需要创建和重放多个生成器。因此,副作用会被错误地执行多次。

尽管如此,即使您不使用生成器,在 JavaScript 中编写一元代码也不是那么糟糕。

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 等预处理器。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    • 2010-12-27
    相关资源
    最近更新 更多