2016-01-30 65 views
3

如果我有一個Task對於成功值爲Either err b,我如何組合/合併/轉換它們,以便成功值可以直接在.fork()中使用,而不是包含在Either中? 可以將`Either`類型轉換爲`Task`類型嗎?

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

回答

5

這是我會做:

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>

希望有所幫助。

相關問題