[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. In this lesson we'll demystify how function composition works by building our own compose
and composeAll
functions.
// __test__ import {add, inc, dbl, addInc, addIncDbl} from '../function/custom-compose'; describe('basic fns', () => {
"use strict";
test('add', () => {
const res = add(1,2);
const expected = 3;
expect(res).toBe(expected);
}); test('inc', () => {
const res = inc(2);
const expected = 3;
expect(res).toBe(expected);
}); test('dbl', () => {
const res = dbl(2);
const expected = 4;
expect(res).toBe(expected);
});
}); describe('compose', () => {
"use strict";
test('add then inc', () => {
const res = addInc(4, 2);
const expected = 7;
expect(res).toBe(expected);
});
}); describe('composeAll', () => {
"use strict";
test('add, inc then dbl', () => {
const res = addIncDbl(2, 3);
const expected = 12;
expect(res).toBe(expected);
}) ;
});
/*
* Utils
* */
const compose = (f, g) => (...args) => f(g(...args)); const composeAll = (...fns) => fns.reduce(compose);
/*
* Libs
* */
export const add = (a, b) => a + b; export const inc = (a) => a + 1; export const dbl = (a) => a * 2; export const addInc = compose(inc, add); export const addIncDbl = composeAll(dbl, inc, add);
[Javascript] Understand Function Composition By Building Compose and ComposeAll Utility Functions的更多相关文章
- [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
In this lesson we'll use a handful of Ramda's utility functions to take a queryString full of name/v ...
- Function Composition vs Object Composition
In functional programming, we create large functions by composing small functions; in object-oriente ...
- [Ramda] Refactor a Promise Chain to Function Composition using Ramda
Promise chains can be a powerful way to handle a series of transformations to the results of an asyn ...
- JavaScript笔记 Function
在JavaScript中方法由两部分组成: 方法名和方法体. JavaScript中的方法跟其他传统面向对象语言不同,它跟普通的变量没有区别,唯一不同点是它是Function对象,因此它会有一些Fun ...
- (转)深入理解javascript的function
原文:http://www.cnblogs.com/sharpxiajun/archive/2011/09/16/2179323.html javascript笔记:深入理解javascript的fu ...
- javascript的Function 和其 Arguments
http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...
- JavaScript之Function函数深入总结
整理了JavaScript中函数Function的各种,感觉函数就是一大对象啊,各种知识点都能牵扯进来,不单单是 Function 这个本身原生的引用类型的各种用法,还包含执行环境,作用域,闭包,上下 ...
- JavaScript Nested Function 的时空和身份属性
JavaScript 的function 不仅仅是一等公民,简直就是特殊公民.它有许多独特的特征: 1) 它是object,可以存储,传递,附加属性. 2) 它可以有lexical closure, ...
- Javascript中Function,Object,Prototypes,__proto__等概念详解
http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...
随机推荐
- 模板 Fail树
fail树就是将Trie图的Fail指针反指,从而生成一棵树,这个树的性质是:子节点对应字符串为以当前串为后缀,而子节点为原串的前缀,前缀的后缀就是嵌套在原串中的子串. 模板:BZOJ3172 Des ...
- C++中引用传递与指针传递区别
C++中引用传递与指针传递区别 在C++中,指针和引用经常用于函数的参数传递,然而,指针传递参数和引用传递参数是有本质上的不同的: 指针传递参数本质上是值传递的方式,它所传递的是一个地址值.值传递过程 ...
- node.js服务器核心http和文件读写
使用htpp给客服端的数据,把数据交给浏览器渲染.利用 http创建服务器,如客户端请求为:127.0.0.1:3000或127.0.0.1:3000/xxx.html时 ,判断www文件夹中,文件 ...
- nuxt配置sass
没有比这更简单的了 只需要安装 node-sass sass-loader 就可以了 npm i node-sass sass-loader -D 然后就可以直接使用了: <style lang ...
- robotframework Selenium2+RFS自动化测试
支持浏览器版本:Google Chrome (64位) 52.0.2743.82 正式版 52.0.2743.6_chrome_installer 64位 下载地址:http://www.online ...
- HTTP详解--请求、响应、缓存
1. HTTP请求格式 做过Socket编程的人都知道,当我们设计一个通信协议时,“消息头/消息体”的分割方式是很常用的,消息头告诉对方这个消息是干什么的,消息体告诉对方怎么干.HTTP协议传输的消息 ...
- LoadRunner--HTML与URL录制方式区别
Recording录制选项 这里提供了两个大类的录制方式: 1. HTML-based script基于HTML的脚本 这种方式录制出来的脚本是基于HTML基础的,为每个用户操作生成单独的步骤,这种脚 ...
- 微服务实战(二):使用API Gateway - DockOne.io
原文:微服务实战(二):使用API Gateway - DockOne.io [编者的话]本系列的第一篇介绍了微服务架构模式.它讨论了采用微服务的优点和缺点,除了一些复杂的微服务,这种模式还是复杂应用 ...
- (转)Bash 快捷键 完整版
转自:http://www.opsers.org/linux-home/base/full-version-of-bash-keyboard-shortcuts.html#toc-3 生活在 Bash ...
- Mac怎么设置wifi热点
苹果 Mac 系统中要把无线当作 Wifi 热点来用的话,需要电脑有其它网络接入才可以,也就是说它需要一个可以用于上网的网络,比如有线网络.尤其是对于使用 MacBook Pro 或 MacBook ...