[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% ...
随机推荐
- 趣闻|Python之禅(The Zen of Python)
在Python解释器中输入“import this”会发生什么?如果你不知道这个彩蛋,推荐继续阅读这篇文章. 2001年秋,Foretec(一家会议组织公司)正在准备召开第十届Internationa ...
- 百度地图API 添加标签
1.手动创建数据,实际项目则是接受GPS信息 /建立坐标点: // lng:经度 lat:纬度 var points = [ {"lng":112.58,"lat&quo ...
- 【2017"百度之星"程序设计大赛 - 初赛(B)】小小粉丝度度熊
[链接]http://acm.hdu.edu.cn/showproblem.php?pid=6119 [题意] 在这里写题意 [题解] 先把相交的部分合成一个区间. 这个可以用排序,加个简单的处理就能 ...
- 洛谷——P1316 丢瓶盖
https://www.luogu.org/problem/show?pid=1316 题目描述 陶陶是个贪玩的孩子,他在地上丢了A个瓶盖,为了简化问题,我们可以当作这A个瓶盖丢在一条直线上,现在他想 ...
- animation-list -帧动画
帧动画实现起来比较简单,今天接触到使用xml来创建帧动画,记录下来. 它说白了,其实就是动态的展示图片而已 1.在xml中定义帧动画,如下 <?xml version="1.0&quo ...
- android.mk-编译文件学习(转载)
工作了那么久,都是使用大神们写的脚本机械的编译,对于android.mk根本没去了解过.今天趁着这个机会,在网上搜索了下.虽然依然不是很名白,留做记录,以后真用到了,再深入研究 转载自 http:// ...
- 1. 初识ZooKeeper。
转自:https://blog.csdn.net/en_joker/article/details/78661466 Apache ZooKeeper是由 Apache Hadoop的子项目发展而来, ...
- 【COGS1672】【SPOJ375】QTREE
这是我的第一个边权链剖 COGS上和SPOJ有点不一样就是没有多组数据了本质还是一样的 我写的是COGS那个事实上改一改就能够去SPOJ AC了= -= (但是我如今上不去SPOJ卧槽(╯‵□′)╯︵ ...
- JDBC高级特性(二)事务、并发控制和行集
一.事务 事务是指一个工作单元,它包括了一组加入,删除,改动等数据操作命令,这组命令作为一个总体向系统提交运行,要么都运行成功,要么所有恢复 在JDBC中使用事务 1)con.setAutoCommi ...
- amazeui学习笔记--css(HTML元素1)--按钮Button
amazeui学习笔记--css(HTML元素1)--按钮Button 一.总结 1.button的基本使用:a.am-btn 在要应用按钮样式的元素上添加 .am-btn,b.颜色 再设置相应的颜色 ...