[Functional Programming] Reader with Async ADT
Posted answer1215
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Functional Programming] Reader with Async ADT相关的知识,希望对你有一定的参考价值。
ReaderT
is aMonad Transformer
that wraps a givenMonad
with aReader
. This allows the interface of aReader
that enables the composition of computations that depend on a shared environment(e -> a)
, but provides a way to abstract a means theReader
portion, when combiningReaderT
s of the same type. AllReaderT
s must provide the constructor of the targetMonad
that is being wrapped.
The task we want to do is read from a "data.json" file:
"name": "App", "config": "prod": "config.prod.json", "dev": "config.dev.json", "test": "config.test.json"
According to the ‘env‘ variable we pass in, it will pick different config file:
config.test.json:
"port": 5200
In the end, it will output a json file combine the result.
const readJSON, writeJSON, fork = require("./helper"); const Async, ReaderT, omit, pipeK, assign = require("crocks"); const ReaderAsync = ReaderT(Async); const env = input: "data.json", output: "output.json" ; const input = env => ReaderAsync(( input ) => readJSON(input).map(assign( env ))); const config = data => ReaderAsync(() => readJSON(data.config[data.env]) .map(assign(data)) .map(omit(["config"])) ); const output = inputData => ReaderAsync(( output ) => writeJSON(output, inputData)); const flow = pipeK( ReaderAsync.of, input, config, output ); fork(flow("test").runWith(env));
output.json file:
"port": 5200, "name": "App", "env": "test"
以上是关于[Functional Programming] Reader with Async ADT的主要内容,如果未能解决你的问题,请参考以下文章
[Functional Programming ADT] Debug a Functional JavaScript composeK Flow