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. LeetCode 第 165 场周赛

    LeetCode 第 165 场周赛 5275. 找出井字棋的获胜者 5276. 不浪费原料的汉堡制作方案 5277. 统计全为 1 的正方形子矩阵 5278. 分割回文串 III C 暴力做的,只能 ...

  2. 真理胜于一切 JAVA模拟表单提交

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  3. Kubernetes组件-CronJob(定时任务)

    ⒈简介: Kubernetes的Job资源在创建时会立即运行pod.但是许多批处理任务需要在特定的时间运行,或者在指定的时间间隔内重复运行.在Linux和类UNIX操作系统中,这些任务通常被称为cro ...

  4. redis集群(多机)分布

    一.实现原理 一致性哈希算法(Consistent Hashing): http://www.zsythink.net/archives/1182 二.配置两个redis服务,端口号要不一致 三.代码 ...

  5. C++编写DLL文件

    动态链接库DLL文件与EXE文件一样也是可执行文件,但是DLL也被称为库,因为里面封装了各种类.函数之类的东西,就像一个库一样,存着很多东西,主要是用来调用的.调用方式主要分为两种:隐式(通过lib文 ...

  6. ffmpeg解码音视频过程(附代码)

    0. 引言 最近一直在使用和学习ffmpeg. 工作中需要拉流解码, 获取音频和视频数据. 这些都是使用ffmpeg处理. 因为对ffmpeg接触不多, 用的不深, 在使用的过程中经常遇到不太懂的地方 ...

  7. python+selenium+webdriver+BeautifulSoup实现自动登录

    from selenium import webdriverimport timefrom bs4 import BeautifulSoupfrom urllib import requestimpo ...

  8. echarts 根据经纬度坐标在地图上描点

    var mapData = [ {'latitude':30.67, 'longitude':104.07}, {'latitude':34.76, 'longitude':113.65}, {'la ...

  9. net core体系-web应用程序-4asp.net core2.0 项目实战(CMS)-第二章 入门篇-快速入门ASP.NET Core看这篇就够了

    .NET Core实战项目之CMS 第二章 入门篇-快速入门ASP.NET Core看这篇就够了   原文链接:https://www.cnblogs.com/yilezhu/p/9985451.ht ...

  10. CN丶Moti-个人博客

    欢迎访问我的个人博客,获取更多有用的东西 链接一 链接二 也可以关注我的微信订阅号:CN丶Moti