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的更多相关文章

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

  2. 每个JavaScript开发人员应该知道的33个概念

    每个JavaScript开发人员应该知道的33个概念 介绍 创建此存储库的目的是帮助开发人员在JavaScript中掌握他们的概念.这不是一项要求,而是未来研究的指南.它基于Stephen Curti ...

  3. [JavaScript] canvas 合成图片和文字

    Canvas Canvas 是 HTML5 新增的组件,就像一个画板,用 js 这杆笔,在上面乱涂乱画 创建一个 canvas <canvas id="stockGraph" ...

  4. Currying 及应用

    Currying,中文多翻译为柯里化,感觉这个音译还没有达到类似 Humor 之于幽默的传神地步,后面直接使用 Currying. 什么是 Currying Currying 是这么一种机制,它将一个 ...

  5. Vue3.0常用代码片段和开发插件

    Vue3 Snippets for Visual Studio Code Vue3 Snippets源码 Vue3 Snippets下载 This extension adds Vue3 Code S ...

  6. Frontend Development

    原文链接: https://github.com/dypsilon/frontend-dev-bookmarks Frontend Development Looking for something ...

  7. 高阶组件 HOC

    一. A higher-order component (HOC) is an advanced technique in React for reusing component logic. a h ...

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

  9. [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, ...

随机推荐

  1. Python 绘图与可视化 matplotlib 制作Gif动图

    参考链接:https://blog.csdn.net/theonegis/article/details/51037850 官方文档:https://matplotlib.org/3.1.0/api/ ...

  2. JavaScript div 上下运动实例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  3. C/C++ 名正则言顺

    本系列文章由 @yhl_leo 出品,转载请注明出处. 文章链接: http://blog.csdn.net/yhl_leo/article/details/50532701 名称所表达的含义极其丰富 ...

  4. Oracle sql 子字符串长度判断

    Oracle sql 子字符串长度判断 select t.* from d_table t ,) ,instr(t.col,; 字符串的前两位都是数字: select * from d_table t ...

  5. Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字

    题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...

  6. MyEclipse中安装Testng插件

    下载testng.eclipse插件 Testng相应jar包,这里使用的是5.12的版本号 直接找到myeclipse的安装文件夹,将org.testng.eclipse_5.12.0.6.jar ...

  7. Android生命周期里你也许不知道的事

    Android生命周期预计连刚開始学习的人都再熟悉只是的东西了,但这里我抛出几个问题.也许大家曾经没有想过或者可能认识的有些错误. 一.当A启动B时,A和B生命周期方法运行的先后顺序是如何的?当按返回 ...

  8. Linux就该这么学 20181002(第二章基础命令)

    参考链接https://www.linuxprobe.com/ 忘记密码操作 启动页面 默认按e 在linux16行后空格 rd.break ctrl + x mount -o remount,rw ...

  9. [JZOJ NOIP2018模拟10.19]

    T1写炸了今天,期望70却落了个20...连链上的都没有写对 T3什么什么线段树分治套AC自动机,表示我完全自闭了,幸好考场上没有杠T3 总体比赛还是比较舒服,暴力分给的蛮足的,不像昨天那样 T1:林 ...

  10. Parquet学习总结

    深入分析Parquet列式存储格式 Parquet是面向分析型业务的列式存储格式,由Twitter和Cloudera合作开发,2015年5月从Apache的孵化器里毕业成为Apache顶级项目,最新的 ...