3
結果:像getFile(filename).map(parseJson).map(doOtherThings)...
如何在JavaScript中使用IO和Either仿函數實現線性流?
流線性。當我使用Either
本身一切都是好的,易於
話,我可以做以下
safeUnsureFunction().map((result)=>{
// result is just result from doSomethingCrazyHere function
// everything is linear now - I can map all along
return result;
})
.map()
.map()
.map()
.map();
// linear flow
問題是,當我m使用IO如:
function safeReadFile(){
try{
return Right(fs.readFileSync(someFile,'utf-8'));
}catch(e){
return Left(error);
}
}
let pure=IO.from(safeReadFile).map((result)=>{
// result is now Either
// so when I want to be linear I must stay here
// code from now on is not linear and I must generate here another chain
return result.map(IdontWant).map(ToGo).map(ThisWay).map(ToTheRightSideOfTheScreen);
})
.map((result)=>{
return result.map(This).map(Is).map(Wrong).map(Way);
})
.map(IwantToBeLienearAgain)
.map(AndDoSomeWorkHere)
.map(ButMapFromIOreturnsIOallOverAgain);
let unpure=function(){
return pure.run();
}
IO是爲了將純粹的功能分離出來嗎?
所以我想分開unpure文件讀取與任何文件錯誤處理。這可能嗎?
如何在IO單元內使用Eithers時具有線性流動?
函數式編程有沒有任何模式呢?
readFile(filename).map(JSON.parse).map(doSomethingElse)....
可能有一個看看這裏https://github.com/fantasyland/fantasy-land –