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. 1.23 Python知识进阶 - 面向对象编程

    一.编程方法 1.函数式编程:"函数式编程"是一种"编程范式"(programming paradigm),也就是如何编写程序的方法论.它属于"结构化 ...

  2. HTTP浅谈

    HTTP浅谈 1···什么是HTTP? HTTP协议就是超文本传输协议(HyperText Transfer Protocol),通俗理解是浏览器和web服务器传输数据格式的协议,HTTP协议是一个应 ...

  3. COGS 163 [USACO Mat07] 牛语

    COGS 163 [USACO Mat07] 牛语 输入文件:latin.in   输出文件:latin.out   简单对比 时间限制:1 s   内存限制:128 MB 奶牛们听说猪发明了一种秘密 ...

  4. Native开发与JNI机制详解

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 文/李森 博主导读:博主不是代码大师,研究什么都很深,Java除外,所以无论什么问题,方案可以提供, ...

  5. 洛谷——P1012 拼数

    https://www.luogu.org/problem/show?pid=1012#sub 题目描述 设有n个正整数(n≤20),将它们联接成一排,组成一个最大的多位整数. 例如:n=3时,3个整 ...

  6. 洛谷 P2694 接金币

    P2694 接金币 题目描述 在二维坐标系里,有N个金币,编号0至N-1.初始时,第i个金币的坐标是(Xi,Yi).所有的金币每秒向下垂直下降一个单位高度,例如有个金币当前坐标是(xf, yf),那么 ...

  7. Jquery获取select选中的option的文本信息

    注意:以下用的$(this)代表当前选中的select框 第一种: $(this).children("option:selec... ...查看全文

  8. hello word-python 入门

    今天正式开始学习python,先写一个最今经典的例子 helloword #!/usr/bin/python3.2 print("hello work!") 知识点: #!usr/ ...

  9. Rotation--控件位置旋转

    今天想要完成一个按钮的动画,也就是随着手势在屏幕上的滑动,让按钮图片跟着旋转.刚开始的思路是,先把图片旋转以后,在把这个图片设置为imagebutton的背景.不过,会发现这个图片经过处理以后一直变形 ...

  10. leetcode 113. Path Sum II (路径和) 解题思路和方法

    Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given su ...