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

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

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

  3. python3.4 build in functions from 官方文档 翻译中

    2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python i ...

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

  5. Docker三剑客之Docker Compose

    一.什么是Docker Compose Compose 项目是Docker官方的开源项目,负责实现Docker容器集群的快速编排,开源代码在https://github.com/docker/comp ...

  6. Docker Compose 基本使用

    Dockercompose v3官网文档:  https://docs.docker.com/compose/compose-file/   Dockercompose中文: http://www.d ...

  7. Android & CM build basics

    [CM source code folders] bootable/Among other things, the source for ClockworkMod recovery is in her ...

  8. centos6升级glibc-2.14没有报错,但是验证没有升级成功的问题解决

    一.下载 cd /usr/local/srcwget http://ftp.gnu.org/gnu/glibc/glibc-2.14.tar.gz 二.安装 tar -xzvf glibc-2.14. ...

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

随机推荐

  1. jodd-cache集锦

    Jodd cache提供了一组cache的实现,其层次如下: 其中, AbstractCacheMap是一个具有计时和大小的缓存map的默认实现,它的实现类必须: 创建一个新的缓存map. 实现自己的 ...

  2. Fedora 10下应用网络模拟器NS心得

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  3. Android 使用AIDL实现进程间的通信

    在Android中,如果我们需要在不同进程间实现通信,就需要用到AIDL技术去完成. AIDL(android Interface Definition Language)是一种接口定义语言,编译器通 ...

  4. POJ 3009 Curling 2.0 {深度优先搜索}

    原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...

  5. SICP 习题 (2.10)解题总结: 区间除法中除于零的问题

    SICP 习题 2.10 要求我们处理区间除法运算中除于零的问题. 题中讲到一个专业程序猿Ben Bitdiddle看了Alyssa的工作后提出了除于零的问题,大家留意一下这个叫Ben的人,后面会不断 ...

  6. vue <input type="file">上传图片、预览、删除

    使用原生<input type="file">上传图片.预览.删除:multiple实现可上传多张 参数名 类型 说明 fileTypes Array 文件类型, 默认 ...

  7. Flask项目之手机端租房网站的实战开发(十一)

    说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...

  8. Linux中top命令参数详解

    此文摘自(https://www.cnblogs.com/ggjucheng/archive/2012/01/08/2316399.html) 简介 top命令是Linux下常用的性能分析工具,能够实 ...

  9. 【Codeforces Round #446 (Div. 2) B】Wrath

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 倒着来,维护一个最小的点就可以了. [代码] #include <bits/stdc++.h> using namesp ...

  10. 51Nod——N1082 与7无关的数

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1082 题目来源: 有道难题 基准时间限制:1 秒 空间限制:13107 ...