[Javascript] Improve Composition with the Compose Combinator
To make our composition more readable and easier to name we are going to ceate a compose function we can use to avoid having to manually nest our transducer calls.
We'll also go over semantically naming your compose functions for extra readability.
Nested style:
import {doubleTheNumber, evenOnly} from "../utils";
const map = xf => reducer => {
return (accumulation, value) => {
return reducer(accumulation, xf(value));
};
};
const filter = predicate => reducer => {
return (accumulation, value) => {
if (predicate(value)) return reducer(accumulation, value);
return accumulation;
};
};
const isEvenFilter = filter(evenOnly);
const isNot2Filter = filter(val => val !== 2);
const doubleMap = map(doubleTheNumber);
const pushReducer = (accumulation, value) => {
accumulation.push(value);
return accumulation;
};
[1,2,3,4].reduce(isNot2Filter(isEvenFilter(doubleMap(pushReducer))), []);
Compose function:
import {filter, map, evenOnly, doubleTheNumber} from "../utils";
const doubleMap = map(doubleTheNumber);
const isEvenFilter = filter(evenOnly);
const isNot2Filter = filter(val => val !== 2);
const pushReducer = (accumulation, value) => {
accumulation.push(value);
return accumulation;
};
[1, 2, 3, 4].reduce(isNot2Filter(isEvenFilter(doubleMap(pushReducer))), []);
// compose(f,g)(x) === f(g(x));
//
// compose(isNot2Filter, isEvenFilter, doubleMap)(pushReducer) ===
// isNot2Filter(isEvenFilter(doubleMap(pushReducer)));
const compose = (...functions) =>
functions.reduce((accumulation, fn) =>
(...args) => accumulation(fn(...args)), x => x);
[1, 2, 3, 4].reduce(
compose(isNot2Filter, isEvenFilter, doubleMap)(pushReducer),
[],
); /*?*/
[Javascript] Improve Composition with the Compose Combinator的更多相关文章
- [Javascript] Understand Function Composition By Building Compose and ComposeAll Utility Functions
Function composition allows us to build up powerful functions from smaller, more focused functions. ...
- 每个JavaScript开发人员应该知道的33个概念
每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...
- [JavaScript] canvas 合成图片和文字
Canvas Canvas 是 HTML5 新增的组件,就像一个画板,用 js 这杆笔,在上面乱涂乱画 创建一个 canvas <canvas id="stockGraph" ...
- Currying 及应用
Currying,中文多翻译为柯里化,感觉这个音译还没有达到类似 Humor 之于幽默的传神地步,后面直接使用 Currying. 什么是 Currying Currying 是这么一种机制,它将一个 ...
- Vue3.0常用代码片段和开发插件
Vue3 Snippets for Visual Studio Code Vue3 Snippets源码 Vue3 Snippets下载 This extension adds Vue3 Code S ...
- Frontend Development
原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...
- 高阶组件 HOC
一. A higher-order component (HOC) is an advanced technique in React for reusing component logic. a h ...
- [Ramda] Lens in Depth
In this post, we are going to see how to use Ramda Lens. For example, we have data: const {log} = re ...
- [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN
Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...
随机推荐
- UNIX基础【UNIX入门经典】
最早在学校很流行.学生毕业以后就会为公司购买操作系统.导致UNIX流行 UNIX内核: Shell:sh csh ksh 其他组件:
- HDU 2512
水题 #include <iostream> #include <cstdio> #include <algorithm> #define LL __int64 # ...
- Windows下面使用curl
Windows下面使用curl 学习了:https://www.cnblogs.com/xing901022/p/4652624.html 下载地址:https://curl.haxx.se/down ...
- [ReactVR] Add Lighting Using Light Components in React VR
In order to illuminate a scene containing 3D objects a lighting setup is required. In this lesson we ...
- 实现 jstl标签foreach 功能
jsp 页面 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEn ...
- Java生成验证码_转
为了防止用户恶意,或者使用软件外挂提交一些内容,就得用验证码来阻止,虽然这个会影响用户体验,但为了避免一些问题很多网站都使用了验证码;今天下午参考文档弄了一个验证码,这里分享一下;这是一个web工程, ...
- 软工视频总结Part Three
软件需求分析 任务 解决目标系统"做什么"问题 深入描写叙述软件的功能和性能 确定软件涉及到的约束和软件接口 定义软件的其它有效需求 特点 一致性.完整性.限时性.有效性.可验证性 ...
- UVALive 4222 /HDU 2961 Dance 大模拟
Dance Problem Description For a dance to be proper in the Altered Culture of Machinema, it must abid ...
- Linux下处理JSON的命令行工具:jq---安装
转自:https://blog.csdn.net/Sunny_much/article/details/50668871 JSON是前端编程经常用到的格式.Linux下也有处理处理JSON的 ...
- mysql实战45讲读书笔记(二) 一条SQL更新语句是如何执行的 极客时间
前面我们系统了解了一个查询语句的执行流程,并介绍了执行过程中涉及的处理模块.相信你还记得,一条查询语句的执行过程一般是经过连接器.分析器.优化器.执行器等功能模块,最后到达存储引擎. 那么,一条更新语 ...