[Compose] 14. Build curried functions
We see what it means to curry a function, then walk through several examples of curried functions and their use cases.
For example we have an 'add' function:
const add = (x, y) => x + y;
const inc = y => add(, y);
inc(2)
//
We want to conver it to using curry function, the way we do it is by function another function inside add function:
const add = x => y => x + y;
SO the first time we call add(), it will remember the value we passed in:
const inc = add(); // now x = 1
But the function won't be run until we pass in second param:
const res = inc(); // now y = 2
console.log(res) //
----------
Of course, previous example is not that useful, there is another example:
const modulo = dvr => dvd => dvd % dvr;
const isOdd = modulo(); // dvr = 2;
const res1 = isOdd(); //dvd = 7
const res2 = isOdd(); //dvd = 4
console.log(res1) //
console.log(res2) //
Exmaple2:
const modulo = dvr => dvd => dvd % dvr;
const isOdd = modulo(); // dvr = 2;
const filter = pred => ary => ary.filter(pred);
const getAllOdds = filter(isOdd);
const res = getAllOdds([,,,,]);
console.log(res) //[1, 3, 5]
Example3:
const replace = regex => replaceWith => str =>
str.replace(regex, replaceWith); const censor = replace(/[aeiou]/ig)('*'); // [aeiou] --> regex, replaceWith --> *
const res = censor('Hello World');
console.log(res); //"H*ll* W*rld"
Example 4:
const map = fn => ary => ary.map(fn);
const replace = regex => replaceWith => str =>
str.replace(regex, replaceWith);
const censor = replace(/[aeiou]/ig)('*'); // [aeiou] --> regex, replaceWith --> *
const censorAll = map(censor);
const res = censorAll(["Hello", "World"]);
console.log(res); //["H*ll*", "W*rld"]
[Compose] 14. Build curried functions的更多相关文章
- Instance Methods are Curried Functions in Swift
An instance method in Swift is just a type method that takes the instance as an argument and returns ...
- [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. ...
- python3.4 build in functions from 官方文档 翻译中
2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...
- [Javascript] Compose multiple functions for new behavior in JavaScript
In this lesson you will create a utility function that allows you to quickly compose behavior of mul ...
- Docker三剑客之Docker Compose
一.什么是Docker Compose Compose 项目是Docker官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在https://github.com/docker/comp ...
- Docker Compose 基本使用
Dockercompose v3官网文档: https://docs.docker.com/compose/compose-file/ Dockercompose中文: http://www.d ...
- Android & CM build basics
[CM source code folders] bootable/Among other things, the source for ClockworkMod recovery is in her ...
- centos6升级glibc-2.14没有报错,但是验证没有升级成功的问题解决
一.下载 cd /usr/local/srcwget http://ftp.gnu.org/gnu/glibc/glibc-2.14.tar.gz 二.安装 tar -xzvf glibc-2.14. ...
- [Functional Programming] Create Reusable Functions with Partial Application in JavaScript
This lesson teaches you how arguments passed to a curried function allow us to store data in closure ...
随机推荐
- 23. Node.Js Buffer类(缓冲区)-(三)文件读取实例
转自:https://blog.csdn.net/u011127019/article/details/52513109
- Objective-C基础笔记(8)Foundation常用类NSString
一.创建字符串的方法 void stringCreate(){ //方法1 NSString *str1 = @"A String!"; //方法2 NSString *str2 ...
- Android 用Socket实现PC和手机的文件传输
PC服务器端代码: /* * PC与<a href="http://lib.csdn.net/base/android" class='replace_word' title ...
- 3、Task.Factory属性
3.Task.Factory属性 Task类提供了一个Factory静态属性,这个属性返回一个TaskFactory对象. Task task = Task.Factory.StartNew(Task ...
- JavaScript学习总结(5)——Javascript面向(基于)对象编程
一.澄清概念 1.JS中"基于对象=面向对象" 2.JS中没有类(Class),但是它取了一个新的名字叫"原型对象",因此"类=原型对象" ...
- 洛谷 P2026 求一次函数解析式
P2026 求一次函数解析式 题目背景 做数学寒假作业的怨念…… 题目描述 给定两个整点的坐标,求它们所在直线的函数解析式(一次函数). 输入输出格式 输入格式: 输入共两行. 第一行有两个整数x1, ...
- 使用knockout.js 完毕template binding
//1.template <script id="txn-details-template" type="text/html"> <!--St ...
- openGLES(二)
顶点和着色器 我们使用独立的点集合构建物体,都是使用顶点,之后会使用着色绘制图性,以及告诉OpenGLES如何绘制的小程序. 片段着色器,即每个小的像素的渲染, 顶点着色器确定所绘制图像的 ...
- Android 获取联系人手机号码、姓名、地址、公司、邮箱、生日
public void testGetAllContact() throws Throwable { //获取联系人信息的Uri Uri uri = ContactsContract.Contacts ...
- ejs模板引擎的使用
引入ejs.min.js 创建模板,以<%=jsCode%>包裹起来其余的html和html结构一样 focusTemplateData是模板使用的数据,使用$.each()方法遍历绑定数 ...