[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 ...
随机推荐
- IAR FOR STM8 学习笔记 固件库 GPIO
经过一番挣扎,还是决定使用官方的固件库了.. 从网上下一个STM8S的固件库,记得是FOR IAR的. 找到里面的IAR模板就可以开始用了. 这些都是直接写好的库函数,可以直接调用,但首先得先读懂,先 ...
- 轻松学习Linux之详解系统引导过程
轻松学习Linux之详解系统引导过程-1 轻松学习Linux之详解系统引导过程-2 本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- django 简单会议室预约(4)
基本的配置已经完成了,来看看最重要的views.py 先看看简单的注册登录功能,在django里有一个专门的模块用来验证用户信息 :所以只需要调用就好了: #-*-coding:utf-8 -*- f ...
- CISP/CISA 每日一题 18
CISSP 每日一题(答)What is the purpose of an access review and audit? Checkto ensure that users do not hav ...
- session应用二
从session中获取mapper对象,利用mapper对象进行增删改查 Date now = new Date(); SqlSession session = this.yangchebaoDbMa ...
- C# 实现Ajax的方式总结
1JavaScript实现AJAX效果 2.AjaxPro实现AJAX应用 3.微软AJAX控件库开发AJAX 比如ScriptManager,updatePanel,timer等 4.jquery ...
- 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field
| [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...
- Java基础学习总结(52)——Liunx系统Centos上搭建Java开发环境
一.安装jdk 1.查看Linux自带的JDK是否已安装 [plain] view plain copy print? java –version 如果出现openjdk,最好还是先卸载掉openjd ...
- Request、Response 之 Http 请求
今天说些什么呢? 说说Request吧! Request是什么: 请求(Request):一个从客户端到服务器的请求信息包括应用于资源的方法.资源的标识符和协议的版本号 request这个对象不用事先 ...
- 机器学习算法中怎样选取超參数:学习速率、正则项系数、minibatch size
本文是<Neural networks and deep learning>概览 中第三章的一部分,讲机器学习算法中,怎样选取初始的超參数的值.(本文会不断补充) 学习速率(learnin ...