javascript - `Either` 类型可以转换为 `Task` 类型吗?

标签 javascript functional-programming folktale

如果我有一个 Task,它有一个 Either err b 作为正确的(成功)值,我如何组合/合并/转换它们,以便成功值为可直接在 .fork() 中使用,而不是包裹在 Either 中?

const Task = require('data.task'); // folktale
const Either = require('data.either');

// eitherYayNay :: Bool → Either String String
const eitherYayNay = bool =>
  bool ? Either.Right('yay') : Either.Left('nay');

// theTask :: Bool → Task Either a b
const theTask = yn =>
  new Task((reject, resolve) => {
    resolve(eitherYayNay(yn));
    // reject();
  });

// niceTask :: Bool → Task a b
// ???

// the desired result...
niceTask(something).fork(
  err => { 
    // err could be the left value of the Task, or of the Either
  },
  val => { 
    console.log(val); // a string, not an Either
  }
);

最佳答案

这是我会做的:

var Task   = require("data.task");
var Either = require("data.either");

   // eitherYayNay :: Bool -> Either String String
const eitherYayNay = bool =>
    bool ?
        Either.Right("yay") :
        Either.Left("nay");

   // theTask :: Bool -> Task a (Either String String)
const theTask = bool => Task.of(eitherYayNay(bool));

   // niceTask :: Bool -> Task String String
const niceTask = bool => theTask(bool).chain(makeNice);

   // makeNice :: Either String String -> Task String String
const makeNice = either =>
    either.isRight ?
        Task.of(either.value) :
        Task.rejected(either.value);

const fork = bool => niceTask(bool).fork(onError, onValue);

const onError = error => console.log("Error: " + error);

const onValue = value => console.log("Value: " + value);

fork(true);  // Value: yay
fork(false); // Error: nay

查看演示:

var eitherYayNay = function (bool) {
    return bool ?
        Either.Right("yay") :
        Either.Left("nay");
};

var theTask = function (bool) {
    return Task.of(eitherYayNay(bool));
};

var niceTask = function (bool) {
    return theTask(bool).chain(makeNice);
};

var makeNice = function (either) {
    return either.isRight ?
        Task.of(either.value) :
        Task.rejected(either.value);
};

var fork = function (bool) {
    return niceTask(bool).fork(onError, onValue);
};

var onError = function (error) {
    alert("Error: " + error);
}

var onValue = function (value) {
    alert("Value: " + value);
}

fork(true);  // Value: yay
fork(false); // Error: nay
<script src="https://cdn.rawgit.com/aaditmshah/0b27bf3abfaba225b479/raw/f9c6af5e548d27c0d1932b80a9af7e0568c4a89e/task.js"></script>
<script src="https://cdn.rawgit.com/aaditmshah/5bf5e66c37663f3777ee/raw/3110fa24652ed42f005ebc40a39b5138db0063f9/either.js"></script>

希望对您有所帮助。

关于javascript - `Either` 类型可以转换为 `Task` 类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097266/

相关文章:

javascript - 延迟后提交隐藏表单,或者如果单击链接则立即提交

javascript - 全局导入js文件到vuejs 3

functional-programming - 在不使用 case 语句的情况下“解压”SML DataType 中的数据

haskell - "associates to the right"相当于左联想还是右联想?

javascript - folktale 有 IO monad 吗?

javascript - 如何通过 txID 查找交易 (node.js)

javascript - 如何使用 jquery 将字符串数据替换为另一个字符串数据?

javascript - 在所有可迭代对象上懒惰地调用过滤器、查找(数组方法)

javascript - 通过在 folktale2 中使用函数式编程 javascript,如何优雅地访问先前任务的结果?