不纯计算和惰性的描述中是否可以包含副作用?

问题描述

FP仅通过描述不正确的计算并在以后运行它们,尝试将纯净与程序的不正确领域分开。我一直想知道这是否意味着副作用的有害影响是否也被推迟了。

Aff的类型通过将不纯数组计算包装到thunk中来创建描述。曾经进行过一次Aff计算的结果是共享的,即Aff具有惰性语义:

// product type constructor

const record = (type,o) =>
  (o[type.name || type] = type.name || type,o);

// Arr type

const Arr = thunk => record(
  Arr,{get run() {delete this.run; this.run = thunk(); return this.run}});

// Functor

const arrMap = f => tx =>
  Arr(() => f(tx.run));

// Applicative

const arrAp = tf => tx =>
  Arr(() => tf.run(tx.run));

const arrOf = xs => Arr(() => xs);

// impure functions

const arrPush = x => xs => (xs.push(x),xs);
const arrShift = xs => (xs.shift(),xs);
const arrGet = i => xs => xs[i];

const arrAppend = xs => ys =>
  (xs.push.apply(xs,ys),xs);

// MAIN

const ta = arrOf([1,1]); // [1,1]
const tb = arrOf([2,2]); // [2,2]
const tc = arrMap(arrPush(10)) (ta); // [1,1,10]
const td = arrMap(arrPush(20)) (tb); // [2,2,20]

const te = arrAp(
  arrMap(arrAppend) (tc)) (td); // [1,10,20]

const tf = arrMap(arrShift) (te); // [1,20]
const tg = arrMap(arrGet(1)) (tf); // 10
//                            ^^ A
const th = arrMap(arrGet(4)) (tf); // 20
//                            ^^ A

const main = arrAp(
  arrMap(x => y => x + y) (tg)) (th);

console.log(
main.run); // 10 + 20

即使tf运行两次(A),副作用也只会发生一次。如果我没记错的话,Aff可以阻止副作用影响幂等。

但是关联或交换属性呢?副作用会在代码中引入时间,因此,两个以上组合操作的顺序或操作数的顺序应该很重要,对吧?

不幸的是,我找不到一个示例来证明即使使用Aff突变也是有害的。我很确定有一些,这只是实现盲目的一种情况。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...