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

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

  2. Function Composition vs Object Composition

    In functional programming, we create large functions by composing small functions; in object-oriente ...

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

  4. JavaScript笔记 Function

    在JavaScript中方法由两部分组成: 方法名和方法体. JavaScript中的方法跟其他传统面向对象语言不同,它跟普通的变量没有区别,唯一不同点是它是Function对象,因此它会有一些Fun ...

  5. (转)深入理解javascript的function

    原文:http://www.cnblogs.com/sharpxiajun/archive/2011/09/16/2179323.html javascript笔记:深入理解javascript的fu ...

  6. javascript的Function 和其 Arguments

    http://shengren-wang.iteye.com/blog/1343256 javascript的Function属性:1.Arguments对象2.caller 对调用单前函数的Func ...

  7. JavaScript之Function函数深入总结

    整理了JavaScript中函数Function的各种,感觉函数就是一大对象啊,各种知识点都能牵扯进来,不单单是 Function 这个本身原生的引用类型的各种用法,还包含执行环境,作用域,闭包,上下 ...

  8. JavaScript Nested Function 的时空和身份属性

    JavaScript 的function 不仅仅是一等公民,简直就是特殊公民.它有许多独特的特征: 1) 它是object,可以存储,传递,附加属性. 2) 它可以有lexical closure, ...

  9. Javascript中Function,Object,Prototypes,__proto__等概念详解

    http://anykoro.sinaapp.com/2012/01/31/javascript%E4%B8%ADfunctionobjectprototypes__proto__%E7%AD%89% ...

随机推荐

  1. BZOJ4817: [Sdoi2017]树点涂色(LCT)

    Description Bob有一棵n个点的有根树,其中1号点是根节点.Bob在每个点上涂了颜色,并且每个点上的颜色不同.定义一条路 径的权值是:这条路径上的点(包括起点和终点)共有多少种不同的颜色. ...

  2. CISP/CISA 每日一题 18

    CISSP 每日一题(答)What is the purpose of an access review and audit? Checkto ensure that users do not hav ...

  3. Jszip的使用和打包下载图片

    因为canvas总结到后面又想到了jszip的一些事情,那就索性也回去看看吧.试过,至少谷歌和火狐都是支持jszip的. 1.  jszip的使用 官方文档说的很清楚了,而且也有读取zip文件.生成z ...

  4. React-Native_02:语法篇

    1.简单介绍 ECMAScript 6.0(以下简称ES6)是JavaScript语言的下一代标准,已经在2015年6月正式公布了.它的目标.是使得JavaScript语言能够用来编写复杂的大型应用程 ...

  5. Codeforces 145A-Lucky Conversion(规律)

    A. Lucky Conversion time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. LoaderManager使用具体解释(一)---没有Loader之前的世界

    来源: http://www.androiddesignpatterns.com/2012/07/loaders-and-loadermanager-background.html 感谢作者Alex ...

  7. C#无符号右移

    /// <summary>         /// 无符号右移,与JS中的>>>等价         /// </summary>         /// & ...

  8. c#之mysql四种带事务批量插入

    前言 对于像我这样的业务程序员开发一些表单内容是家常便饭的事情,说道表单 我们都避免不了多行内容的提交,多行内容保存,自然要用到数据库,如果循环打扰我数据库,数据库也会觉得很累,从而增加数据库服务器压 ...

  9. 【19.77%】【codeforces 570D】Tree Requests

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  10. [PReact] Create a Hello World App with Preact

    By creating a simple ‘hello world’ example application first in vanilla Javascript, and then in Prea ...