[Functional Programming] Reader with Async ADT
2024-10-06 16:31:09
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"
}
最新文章
- Leetcode 给一个数a和一个向量b,找出该向量b中的2个数相加等于a,并输出这两个数在向量中的位置
- 决策树 -- ID3算法小结
- 导出多级表头表格到Excel
- DSP\BIOS调试Heaps are enabled,but not set correctly
- 学习Swift -- 可选链
- (原)mkl用到的函数
- C/C++面试之算法系列--去除数组中的重复数字
- Java读取文件存储到mysql
- python网络爬虫-中国大学排名定向爬虫
- HTML导出excel
- 获取验证码倒计时60s
- dvi文件和将dvi文件转换成pdf格式
- MT4 取K线
- .NET微信开发Charles突破微信授权,获取任意微信网页源代码(含Https)
- Node.js中实现套接字服务
- Elasticsearch5.5 多机集群配置和x-pack安装配置
- 带下拉列表的TextBox
- python自学第四天,字符串用法
- FoxMail提示:请求的名称有效,但是找不到请求的类型的数据
- PAT甲题题解-1005. Spell It Right (20)-数位求和,水