在函数式编程中,为什么 IO 的 join 方法要运行 unsafePerformIO 两次?

随心所欲

在 DrBoolean 的 Gitbook 中,有几个例子解释了 monad,对于 Maybe 来说:

Maybe.prototype.join = function() {
  return this.isNothing() ? Maybe.of(null) : this.__value;
}

对于 IO:

IO.prototype.join = function() {
  var thiz = this;
  return new IO(function() {
    return thiz.unsafePerformIO().unsafePerformIO();
  });
};

我想知道为什么 IO 应该运行 unsafePerformIO 两次来返回一个新的 IO 而不仅仅是return this.unsafePerformIO()

谢谢

在我这么说之前没有 IO

在 IO 的情况下,重要的是我们在需要之前不要执行任何 IO – 在下面的示例中,请特别注意输出行的顺序

// IO
const IO = function (f) {
  this.unsafePerformIO = f
}

IO.of = function (x) {
  return new IO(() => x)
}

IO.prototype.join = function () {
  return this.unsafePerformIO()
}

// your main program
const main = function (m) {
  console.log('you should not see anything above this line')
  console.log('program result is:', m.unsafePerformIO())
}

// IO (IO (something))
const m = new IO(() => {
  console.log('joining...')
  return IO.of(5)
})

// run it
main(m.join())

上面,joining...出现的时间比我们预期/期望的要——现在将其与正确的IO.join实现进行比较——所有效果都被推迟,直到unsafePerformIO在最外面的 IO 上被调用。


再装箱,拆箱两次

一般来说,所有 IO 操作都会在延迟计算周围添加一个新框。对于join具体而言,我们还是要添加一个新的框,但操作拆箱的两倍,所以我们还是有效地从2层嵌套到1的去。

// IO
const IO = function (f) {
  this.unsafePerformIO = f
}

IO.of = function (x) {
  return new IO(() => x)
}

IO.prototype.join = function () {
  return new IO(() => this.unsafePerformIO().unsafePerformIO())
}

// your main program
const main = function (m) {
  console.log('you should not see anything above this line')
  console.log('program result is:', m.unsafePerformIO())
}

// IO (IO (something))
const m = new IO(() => {
  console.log('joining...')
  return IO.of(5)
})

// run it
main(m.join())


不仅仅是IO

有争议的是,这种重新装箱的方法join也适用于其他 monad

function Maybe (x) {
  this.value = x
}

Maybe.of = function (x) {
  return new Maybe(x)
}

Maybe.prototype.join = function () {
  // assumes that this.value is a Maybe
  // but what if it's not?
  return this.value;
}

Maybe.prototype.toString = function () {
  return `Maybe(${this.value})`
}

const m = Maybe.of(Maybe.of(5))
console.log("m               == %s", m)
console.log("m.join()        == %s", m.join())

// hmm... now it seems `.join` can return a non-Maybe??
console.log("m.join().join() == %s", m.join().join())

上面,看起来Maybe.join有时会返回一个Maybe,而其他时候它可以简单地返回装箱的值。因为它不能保证返回一个 Maybe ,所以更难依赖它的行为

现在,将其与下面的 box-again-unbox-twice 方法进行比较

function Maybe (x) {
  this.value = x
}

Maybe.of = function (x) {
  return new Maybe(x)
}

Maybe.prototype.join = function () {
  // box again, unbox twice
  // guaranteed to return a Maybe
  return Maybe.of(this.value.value)
}

Maybe.prototype.toString = function () {
  return `Maybe(${this.value})`
}

const m = Maybe.of(Maybe.of(5))
console.log("m               == %s", m)

// this still works as intended
console.log("m.join()        == %s", m.join())

// here join still returns a Maybe as expected,
// but the inner value `undefined` reveals a different kind of problem
console.log("m.join().join() == %s", m.join().join())


弱类型 JavaScript

在上面的例子中,我们Maybe(Maybe(Number))转换成Maybe(Maybe(undefined))which 会导致强类型语言的错误。但是,在 JavaScript 的情况下,除非您尝试undefined在您实际期望的地方工作,否则不会出现此类错误5- 这是一种不同的问题,但我个人更喜欢已知的 codomain(返回类型)而不是我必须稍后进行类型检查。

当然,我们可以通过在 join 本身内部进行类型检查来解决这个问题,但现在 Maybe 是不纯的,可能会在运行时抛出错误。

Maybe.prototype.join = function () {
  if (this.value instanceof Maybe)
    return this.value
  else
    throw TypeError ('non-Maybe cannot be joined')
}

可悲的是,这是 JavaScript 在函数式编程的某些方面崩溃的地方。Maybe.join此处的每个实现都需要权衡取舍,因此您必须选择最适合您的方式。


某种幂等性

也许你甚至可以Maybe.join像幂等函数那样如果可以,它会加入,否则它只会返回自己——现在你得到了保证的Maybe返回类型并且不可能出现运行时错误

Maybe.prototype.join = function () {
  if (this.value instanceof Maybe)
    return this.value
  else
    return this
}

但是,以下程序现在已通过此实现进行验证

// should this be allowed?
Maybe.of(Maybe.of(5)).join().join().join().join().join() // => Maybe(5)

取舍,取舍,取舍。选择你的毒药或选择 PureScript ^_^

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章