結果:getFile(filename).map(parseJson).map(doOtherThings)...
IOとリニアフローを実装する方法と関数型プログラミングでjavascriptを使用する方法
のような直線流Iが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
問題がある行うことができます
いいと簡単です。 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は、あまり純粋でない関数から純粋な関数を分離するためのものですか?
私は、ファイルの読み込みに失敗したファイルと、ファイルのエラー処理を分離したいと思います。これは可能ですか?
IOモナドの中でEithersを使用するときに線形フローを持つ方法はありますか?
このための関数型プログラミングのパターンはありますか?このため
readFile(filename).map(JSON.parse).map(doSomethingElse)....
をmodyfingずに
unpure
関数自体にIO
外try catch
ロジックを取ることは、ここにhttps://github.com/fantasyland/fantasy-land顔をしているかもしれません –