ReaderT is a Monad Transformer that wraps a given Monad with a Reader. This allows the interface of a Reader that enables the composition of computations that depend on a shared environment (e -> a), but provides a way to abstract a means the Reader portion, when combining ReaderTs of the same type. All ReaderTs must provide the constructor of the target Monad 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"
}

最新文章

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

热门文章

  1. Win10修改字体
  2. pymysql连接和操作Mysql数据库
  3. 利用Python进行数据分析_Numpy_基础_1
  4. Python基础 第三章 使用字符串(3)字符串方法&本章小结
  5. python 基础例子 双色球 查询天气 查询电话
  6. Django-djangorestframework-渲染模块
  7. Python学习3——列表和元组
  8. PB各对象常用事件
  9. python入门pk小游戏
  10. 怎样限制第三方Cookie