简体   繁体   English

如何使用javascript在函数式编程中使用IO和Either函数实现线性流?

[英]How to implement linear flow with IO and Either functors in functional programming with javascript?

Result: linear flow like getFile(filename).map(parseJson).map(doOtherThings)... 结果:线性流如getFile(filename).map(parseJson).map(doOtherThings)...

When I'm using Either itself everything is nice and easy 当我使用Either本身时,一切都很简单

function doSomethingCrazyHere(){
  return "something crazy";
}

function safeUnsureFunction(){
    try{
       return Right(doSomethingCrazyHere());
    }catch(e){
       return Left(e);
    }
}

then I can just do the following 然后我可以做以下事情

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

problem is when I'm using IO like: 问题是当我使用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 is for separating pure from not so pure functions right? IO是用于分离纯粹而不是那么纯粹的功能吗?

So I want to separate unpure file read with also Either file error handling. 因此,我想将不可见的文件读取与文件错误处理分开。 Is this possible? 这可能吗?

How to have linear flow when using Eithers inside IO monads? 在IO monad中使用Eithers时如何获得线性流?

Is there any pattern in functional programming for this? 函数式编程中是否有任何模式?

readFile(filename).map(JSON.parse).map(doSomethingElse)....

Only way for this could be to add safeRun method to the IO so at the end we will have Either and we will gracefully recover from error 唯一的办法就是将safeRun方法添加到IO所以最后我们将拥有Either ,我们将优雅地从错误中恢复

class safeIO {
  // ...

  safeRun(){
    try{
      return Right(this.run());
    }catch(e){
      return Left(e);
    }
  }

  //...
}

Instead of safeReadFile that returns Either we must use normal readFile 返回Either而不是safeReadFile ,我们必须使用普通的readFile

function readFile(){
    return fs.readFileSync(someFile,'utf-8');
}

let pure = safeIO.from(readFile)
.map((result)=>{
  // result is now file content if there was no error at the reading stage
  // so we can map like in normal IO
  return result;
})
.map(JSON.parse)
.map(OtherLogic)
.map(InLinearFashion);

let unpure = function(){
  return pure.safeRun(); // -> Either Left or Right
}

or take the try catch logic outside an IO to the unpure function itself without modyfing any IO 或者将IO外部的try catch逻辑带到unpure函数本身,而不需要修改任何IO

let unpure = function(){
  try{
    return Right(pure.run());
  }catch(e){
    return Left(e);
  }
}
unpure(); // -> Either

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM