js currying function All In One

js 实现 (5).add(3).minus(2) 功能

例: 5 + 3 - 2,结果为 6



https://stackoverflow.com/questions/36314/what-is-currying

1. normal function

function add (a, b) {
return a + b;
} add(3, 4);
// 7

2. currying function

function add (a) {
return function (b) {
return a + b;
}
} add(3)(4);
// 7 // OR
var add3 = add(3);
// f()
add3(4);
// 7

自适应参数长度的 curry 函数

https://javascript.info/currying-partials


function curry(func) {
return function curried(...args) {
if (args.length >= func.length) {
return func.apply(this, args);
} else {
return function(...args2) {
return curried.apply(this, args.concat(args2));
}
}
};
}

demos

const log = console.log;

function sum(a, b, c) {
return a + b + c;
} const curriedSum = curry(sum); log( curriedSum(1, 2, 3) );
// 6, still callable normally log( curriedSum(1)(2,3) );
// 6, currying of 1st arg log( curriedSum(1)(2)(3) );
// 6, full currying

refs



xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


js currying function All In One的更多相关文章

  1. js 深入原理讲解系列-currying function

    js 深入原理讲解系列-currying function 能看懂这一题你就掌握了 js 科里函数的核心原理 不要专业的术语,说人话,讲明白! Q: 实现 sum 函数使得以下表达式的值正确 cons ...

  2. js currying & js 科里化

    js currying & js 科里化 var test = ( function (a){ console.log(`a2 =`, a);// 1 // console.log(`b2 = ...

  3. JS 关于(function( window, undefined ) {})(window)写法的理解

    JS 关于(function( window, undefined ) {})(window)写法的理解 [网络整理] (function( window, undefined ) {})(windo ...

  4. js小记 function 的 length 属性

    原文:js小记 function 的 length 属性 [1,2,3]., ,这个略懂js的都知道. 但是  eval.length,RegExp.length,"".toStr ...

  5. js中function的与众不同

    js中function的与众不同在于可以被调用

  6. JS里面function和Function的区别

    js里Function 与 function的不一样的,不仅仅是大小写的问题. 简单点说:大写的Function是一个类 ,而小写的function是一个对象. Function是一个构造器,func ...

  7. js arrow function return object

    js arrow function return object bug filterData: { type: Object, default: () => {}, required: true ...

  8. how to remove duplicates of an array by using js reduce function

    how to remove duplicates of an array by using js reduce function ??? arr = ["a", ["b& ...

  9. js currying All In One

    js currying All In One 柯里化 refs https://juejin.im/post/6844903603266650125 xgqfrms 2012-2020 www.cnb ...

随机推荐

  1. Git:.gitignore和.gitkeep文件的使用 让空文件夹被跟踪

    Git:.gitignore和.gitkeep文件的使用 Git:.gitignore和.gitkeep文件的使用 https://majing.io/posts/10000001781172 .gi ...

  2. Hugo 博客中文指南(基础教程)

    1. 安装 Hugo 从 Hugo 项目主页下载 Releases 文件,解压 hugo.exe 文件到 C:\Windows\System32 目录下. 2. 创建站点 hugo new site ...

  3. JVM调优 jdk版本 机器配置 建议jvm参数 备注

    https://juejin.im/post/5b091ee35188253892389683 大型跨境电商JVM调优经历 前提:某大型跨境电商业务发展非常快,线上机器扩容也很频繁,但是对于线上机器的 ...

  4. 后台故障&性能分析常用工具

    说明 本文是一个归纳总结,把常用的一些指令,及它们常用的option简单记录了一下,目的是当我们需要工具去定位问题的时候,能够从中找到合适的工具,具体的用法网上有很多博文了,当然还有man手册.参考了 ...

  5. .axios的特点有哪些

    从浏览器中创建XMLHttpRequests:node.js创建http请求:支持Promise API:拦截请求和响应:转换请求数据和响应数据:取消请求:自动换成json.axios中的发送字段的参 ...

  6. Java Socket InetAddress类 Socket DatagramPacket TCP、UDP示例

    java.net :为实现网络应用程序提供类. InetAddress类 方法摘要 方法摘要 boolean equals(Object obj) : 将此对象与指定对象比较. byte[] getA ...

  7. php中一种单引号逃逸造成的注入

    demo如下: $post = $_POST; $sql=''; $array['name'] = $post['name']; $array['age'] = 18; $array['addr'] ...

  8. GeoMesa Spark

    GeoMesa Spark 一.Spark JTS 1.1 示例 1.2配置 1.3 地理空间用户定义的类型和功能 1.4 geojson输出 1.5 Building 二.Spark Core 2. ...

  9. ICMP&&PING

    ICMP 1.定位:互联网控制报文协议(Internet Control Message Protocol),是TCP/IP协议族的一个子协议,位于网络层.它被IP用于提供许多不同的服务.ICMP是一 ...

  10. Codeforces Global Round 9 E. Inversion SwapSort

    题目链接:https://codeforces.com/contest/1375/problem/E 题意 给出一个大小为 $n$ 的数组 $a$,对数组中的所有逆序对进行排序,要求按照排序后的顺序交 ...