[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 ...
随机推荐
- 初步使用RecyclerView实现瀑布流
先看效果 关于RecyclerView,真的是很强大. 个人觉得主要方便的地方是 1.直接可以设置条目布局,通过setLayoutManager LinearLayoutManager:线性布局,横向 ...
- 7.Maven之(七)pom.xml配置文件详解
转自:https://blog.csdn.net/qq_33363618/article/details/79438044 setting.xml主要用于配置maven的运行环境等一系列通用的属性,是 ...
- 47.__if_not_exists语句
#include <iostream> using namespace std; template<class T> class myclass { public: T t; ...
- Red Hat Enterprise Linux Server 5.5新增功能简介
一.概况 昨日对刚发布的redhat新产品RHEL5.5进行了小范围的测试,红帽企业版 Linux 5.5 发行本中的亮点包括针对 Intel Boxboro-EX 平台.AMD Magny-Cour ...
- ProgressBar-style属性分析
首先我们看下framework下关于进度条的style定义,如下 <style name="Widget.ProgressBar"> <item name=&qu ...
- 24.C语言最全排序方法小结(不断更新)
希尔排序: 该方法的基本思想是:先将整个待排元素序列切割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直接插入排序,然后依次缩减增量再进行排序,待整个序列中的元素基本有序(增量足够小)时,再 ...
- Redis-消费模式
一 . 两种模式简介 发布消息通常有两种模式:队列模式(queuing)和发布订阅模式(qublish-subscribe).队列模式中,consumers可以同时从服务端读取消息,每个消息纸杯其中一 ...
- 1.1 Introduction中 Kafka as a Storage System官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ Kafka as a Storage System kafka作为一个存储系统 An ...
- TabControl里面添加From
private void dynamicDll() { string dllName = "dll"; Assembly ass = Assembly.Load(dllName); ...
- 【例题 7-3 UVA - 10976】Fractions Again?!
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] x>=y => \(\frac{1}{x}<=\frac{1}{y}\) => \(\frac{1}{x}= ...