[Functional Programming] Reader with Async ADT
ReaderTis aMonad Transformerthat wraps a givenMonadwith aReader. This allows the interface of aReaderthat enables the composition of computations that depend on a shared environment(e -> a), but provides a way to abstract a means theReaderportion, when combiningReaderTs of the same type. AllReaderTs must provide the constructor of the targetMonadthat 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] Combine Multiple State ADT Instances with the Same Input (converge(liftA2(constant)))
When combining multiple State ADT instances that depend on the same input, using chain can become qu ...
- [Functional Programming] mapReduce over Async operations and fanout results in Pair(rejected, resolved) (fanout, flip, mapReduce)
This post is similar to previous post. The difference is in this post, we are going to see how to ha ...
- [Functional Programming] mapReduce over Async operations with first success prediction (fromNode, alt, mapReduce, maybeToAsync)
Let's say we are going to read some files, return the first file which pass the prediction method, t ...
- [Functional Programming] Use Task/Async for Asynchronous Actions
We refactor a standard node callback style workflow into a composed task-based workflow. Original Co ...
- [Functional Programming] Compose Simple State ADT Transitions into One Complex Transaction
State is a lazy datatype and as such we can combine many simple transitions into one very complex on ...
- [React + Functional Programming ADT] Create Redux Middleware to Dispatch Actions with the Async ADT
We would like the ability to group a series of actions to be dispatched with single dispatching func ...
- Functional Programming without Lambda - Part 2 Lifting, Functor, Monad
Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...
- JavaScript Functional Programming
JavaScript Functional Programming JavaScript 函数式编程 anonymous function https://en.wikipedia.org/wiki/ ...
- Beginning Scala study note(4) Functional Programming in Scala
1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...
随机推荐
- 小记------phoenix安装搭建
1.下载与hbase对应版本的phoenix http://phoenix.apache.org/download.html 2.解压 tar -zxvf apache-pho ...
- HttpServletRequest对象(转)
HttpServletRequest介绍 HttpServletRequest对象代表客户端的请求,当客户端通过HTTP协议访问服务器时,HTTP请求头中的所有信息都封装在这个对象中,通过这个对象提供 ...
- 阿里巴巴矢量图标库(iconfont)批量全选的方法
阿里巴巴矢量图标库: https://www.iconfont.cn/ 浏览器打开调试面板,进入 console 调试面板(Google浏览器快捷键F12)或者在页面空白处,点击右键->审查元素 ...
- oracle基本查询01
/*数据库-----> 数据库实例----->表空间[逻辑单位]------>数据文件[物理单位] 通常情况下oracle数据库只会有一个实例ORCL 新建一个项目: MYSQL:创 ...
- POJ 1789 Prim
给定N个字符串,某个字符串转为另一个字符串的花费为他们每一位不相同的字符数. 求最小花费Q. Input 多组输入,以0结束. 保证N不超过2000. Output 每组输出"The hig ...
- 旋转动画(RotateTransform)
Silverlight的基础动画包括偏移.旋转.缩放.倾斜和翻转动画,这些基础动画毫无疑问是在Silverlight中使用得最多的动画效果,其使用也是非常简单的.相信看过上一篇<偏移动画(Tra ...
- C# 中使用正则表达式 Regex.Matches方法的几个应用[转]
用于正则表达式的 Regex.Matches静态方法的几种用法: //①正则表达式 = > 匹配字符串 string Text = @"This is a book , this is ...
- eventFlow 系列 <一> 入门
var exampleId = ExampleId.New; var commandBus = resolver.Resolve<ICommandBus>(); ,) var execut ...
- DPDK latencystats库使用方案
初始化 注意务必调用 rte_metrics_init /* init latency stats */ /* @TODO should we remove this in product env? ...
- sketch最强切图工具Sketch Measure
https://www.inpandora.com/sketch-measure.html https://www.jianshu.com/p/c11ae88e6b1d