[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 ...
随机推荐
- HanLP-朴素贝叶斯分类预测缺陷
文章整理自 baiziyu 的知乎专栏,感兴趣的朋友可以去关注下这位大神的专栏,很多关于自然语言处理的文章写的很不错.昨天看到他的分享的两篇关于朴素贝叶斯分类预测的文章,整理了一下分享给给大家,文章已 ...
- Ajax的使用及后台如何传参
Ajax的使用(此处为表单序列化方式) $.ajax({ type: 'post', data: $('#newPriorityForm').serialize(), dataType: 'json' ...
- 什么是云解析DNS?
产品概述 云解析DNS(Alibaba Cloud DNS)是一种安全.快速.稳定.可扩展的权威DNS服务,云解析DNS为企业和开发者将易于管理识别的域名转换为计算机用于互连通信的数字IP地址,从而将 ...
- java微信token校验
1.微信验证接口 package com.park.utils.wechatUtil; import org.springframework.web.bind.annotation.RequestMa ...
- 超简单的js实现提示效果弹出以及延迟隐藏的功能
自动登录勾选提示效果 要求:鼠标移入显示提示信息框:鼠标离开,信息框消失,消失的效果延迟 <!DOCTYPE html> <html lang="en"> ...
- Pygame小游戏练习五
@Python编程从入门到实践 Python项目练习 十一.显示游戏得分及最高分 创建新类Scoreboard,用以显示得分和最高分. # scoreboard.py import pygame.fo ...
- k8s-PV和PVC使用
上节课我们学习了 PV 的使用,但是在我们真正使用的时候是使用的 PVC,就类似于我们的服务是通过 Pod 来运行的,而不是 Node,只是 Pod 跑在 Node 上而已,所以这节课我们就来给大家讲 ...
- win10系统查看激活状态及是否永久激活
查看windows系统是否激活 找到“此电脑”,右击“属性” 查看windows系统是否永久激活 第一种方法 win+r 进入运行,输入slmgr.vbs -xpr 如图,再点击确定. 弹出一个对话 ...
- Java建造者模式(思维导图)
图1 建造者模式[点击查看大图] 基本的知识点已在思维导图中,下面是demo 1,Builder 为创建一个产品对象的各个部件指定抽象接口 public interface PersonBuilder ...
- Oracle---PL/SQL的学习
PL/SQL程序 一.定义 declare 说明部分 begin 语句序列(DML语句) exception 例外处理语句 end; 二. 变量和常量说明 a) 说明变量(char,varchar2, ...