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"
}

[Functional Programming] Reader with Async ADT的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [Functional Programming] Use Task/Async for Asynchronous Actions

    We refactor a standard node callback style workflow into a composed task-based workflow. Original Co ...

  5. [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 ...

  6. [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 ...

  7. Functional Programming without Lambda - Part 2 Lifting, Functor, Monad

    Lifting Now, let's review map from another perspective. map :: (T -> R) -> [T] -> [R] accep ...

  8. JavaScript Functional Programming

    JavaScript Functional Programming JavaScript 函数式编程 anonymous function https://en.wikipedia.org/wiki/ ...

  9. Beginning Scala study note(4) Functional Programming in Scala

    1. Functional programming treats computation as the evaluation of mathematical and avoids state and ...

随机推荐

  1. kindeditor的配置jsp版

    1.将kindeditor资源下载下来,点击这里下载: 2.将资源解压,因为是jsp版本所以只需要保留jsp的文件即可,最终目录为下图 3.在所给的jsp的demo中做配置 注意:demo.jsp中引 ...

  2. Ural 1201 Which Day Is It? 题解

    目录 Ural 1201 Which Day Is It? 题解 题意 输入 输出 题解 程序 Ural 1201 Which Day Is It? 题解 题意 打印一个月历. 输入 输入日\((1\ ...

  3. kubernetes 健康检查和初始化容器

    Pod-hook:postStart:1.$ $ vim preStart-hook.yaml---apiVersion: v1kind: Podmetadata:  name: hook-demo1 ...

  4. Python 语言简介与入门

    Python 的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承.Py ...

  5. k8s之资源指标API部署metrics-server

    1.部署metrics-server 从v1.8开始,引入了新的功能,即把资源指标引入api,资源指标:metrics-server,自定义指标:prometheus,k8s-prometheus-a ...

  6. GAN——ModeCollapse

    GAN——ModeCollapse 2017年05月21日 13:54:31 LiuSpark 阅读数 6821更多 分类专栏: 机器学习   版权声明:本文为博主原创文章,遵循CC 4.0 BY-S ...

  7. gdb-example-ncurses

    gdb-example-ncurses http://www.brendangregg.com/blog/2016-08-09/gdb-example-ncurses.html 1. The Prob ...

  8. PS常识及技巧

    常用格式 JPG:压缩 PNG:透明 GIF:动图 PSD:分层 分辨率 UI选择像素,印刷选择厘米 UI设计:72px    印刷分辨率必须为300 颜色模式UI网页设计:RGB     印刷类设计 ...

  9. js之数据类型(对象类型——构造器对象——正则)

    正则(regular expression)描述了一种字符串的匹配式.一般应用在一些方法中,用一些特殊的符号去代表一些特定的内容,对字符串中的信息实现查找,替换,和提取的操作.js中的正则表达式用Re ...

  10. multer使用

    使用multer 1.      在项目中下载multer操作模块 Npm install multer  --save 前端代码: <form class="layui-form&q ...