[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, ...
随机推荐
- django-10-中间件和上下文管理器
<<<中间件的引入>>> 用户<->中间件<->url->视图 在app目录里面 middleware.py (1)中间件就是一个 ...
- 【【henuacm2016级暑期训练】动态规划专题 M】Little Pony and Harmony Chest
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每一位显然只要取1..60这些数字. 然后需要保证每个这些数字里面,每个数字所用到的质因子都它所唯一拥有的.别人不能用 因为如果别人 ...
- Java基础学习总结(55)——java8新特性:stream
java作为开发语言中的元老已经度过了很多年,最新的java8为我们带来了一些新特性,这些特性可以在以后的工作中为我们的开发提供更多的便捷,现在就让我们看看最新的函数式编程风格怎么在实际的开发中使用. ...
- C#-入门思维导图
C#-入门思维导图 百度云盘 链接:http://pan.baidu.com/s/1jI5zMS2 密码:0ypc 如有错误,请告知我
- POJ-1785-Binary Search Heap Construction(笛卡尔树)
Description Read the statement of problem G for the definitions concerning trees. In the following w ...
- 纯粹的K12精髓 - 名师指导整理《20以内加法口诀表》
纯粹的K12精髓 - 名师指导整理<20以内加法口诀表> 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一 ...
- Gradle 编译多个project(包括多Library库project依赖)指导
Gradle Android最新自己主动化编译脚本教程(提供demo源代码) 这篇文章我简单写了基于Gradle2.1 进行的android project和android library的编译实例, ...
- m_Orchestrate learning system---五、学的越多,做的越快
m_Orchestrate learning system---五.学的越多,做的越快 一.总结 一句话总结: 1.上传的图像文件用input('post.')方法取不到是为什么? 图片不来就这样取不 ...
- ORA-01950: 表空间'USERS'中无权限的2种解决办法
在创建了一个新的表空间和一个新的用户,当用这个新用户创建表时, 却出现:ORA-01950: 表空 间'USERS'中无权限. 我已经把创建表的权限赋给了此用户,怎么还会缺少权限呢?解决办法 ...
- java web项目中资源国际化
有一些网站会有语言栏选项: 选择英文,内容就显示为英文: 选择中文,内容就显示文中文. 这里就用到了国际化资源. 先看效果图: 步骤: 1.建立资源包: mess_en_US.properties ( ...