Currying allows you to easily create custom functions by partially invoking an existing function. Here’s a simple example:

var add = function(a,b) {
return a + b;
} var addTen = add.curry(10); //create function that returns 10 + argument
addTen(20); //

Generally, curry returns a copy of the invoking function, with its first n arguments pre-assigned with the arguments passed by the curry invocation.

The curry function does not exist in native JavaScript, but it’s easy to write your own. Here I’m augmenting function’s prototype with an implementation based on the Prototype framework. (Notice I’m also throwing in a toArray function for convenience. This is because function’s arguments property is not a true array, and we need it to work with array’s concat function)

function toArray(enum) {
return Array.prototype.slice.call(enum);
} Function.prototype.curry = function() {
if (arguments.length<1) {
return this; //nothing to curry with - return function
}
var __method = this;
var args = toArray(arguments);
return function() {
return __method.apply(this, args.concat(toArray(arguments)));
}
}

The returned function expects to be invoked with additional argument(s) which it will concatenate with the argument(s) it got from the curry function.

A CURRY FUNCTION IN JAVASCRIPT

function curry (fn, scope) {

    var scope = scope || window;

    var args = [];

    for (var i=2, len = arguments.length; i < len; ++i) {

        args.push(arguments[i]);

    };

    return function() {

        fn.apply(scope, args);

    };

}

Quote From:

Curry: cooking up tastier functions

JavaScript currying

JavaScript-Curry的更多相关文章

  1. javascript curry 柯里化函数 仿lodash的curry

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 详解JavaScript函数模式

    JavaScript设计模式的作用是提高代码的重用性,可读性,使代码更容易的维护和扩展.在javascript中,函数是一类对象,这表示他可以作为参数传递给其他函数:此外,函数还可以提供作用域. 创建 ...

  3. 初涉JavaScript模式 (10) : 函数 【进阶用法】

    写在前面 不知不觉写到第10篇了.这篇写起来很忐忑,终于和高级搭上边了(呵呵),这篇我们 主要 说一下 JS 方法的部分高级用法(我知道的),笔者水平有限,难免有错.废话不多少,进入正文. 初始化 我 ...

  4. JavaScript Patterns 4.10 Curry

    Function Application apply() takes two parameters: the first one is an object to bind to this inside ...

  5. Javascript函数柯里化(curry)

    函数柯里化currying,是函数式编程非常重要的一个标志.它的实现需要满足以下条件,首先就是函数可以作为参数进行传递,然后就是函数可以作为返回值return出去.我们依靠这个特性编写很多优雅酷炫的代 ...

  6. [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)

    Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...

  7. JavaScript基础Curry化(021)

    时候我们希望函数可以分步接受参数,并在所有参数都到位后得到执行结果.为了实现这种机制,我们先了解函数在Javascript中的应用过程: 1. 函数的“应用”(Function Application ...

  8. [Javascript] Understand Curry

    The act of currying can be described as taking a multivariate function and turning it into a series ...

  9. 数据结构与算法JavaScript (三) 链表

    我们可以看到在javascript概念中的队列与栈都是一种特殊的线性表的结构,也是一种比较简单的基于数组的顺序存储结构.由于javascript的解释器针对数组都做了直接的优化,不会存在在很多编程语言 ...

  10. 《JavaScript语言精粹》小记

    一.前言 以下内容均摘自<JavaScript语言精粹>一书,本人在读这本书时,发现作者诠释JavaScript很犀利,特别是数组部分,固记录下来,想和大家分享下. 随笔主要包含两大部分: ...

随机推荐

  1. SQL删除重复数据,保留一条

    例如: id           name         value 1               a                 pp 2               a           ...

  2. 手动创建servlet

    1 tomcat/webapps/目录下创建web应用mail.   目录结构如下   tomcat/webapps   |    mail   |    WEB-INF     |        | ...

  3. 图的匹配问题与最大流问题(三)——最大流问题Ford-Fulkerson方法Java实现

    上篇文章主要介绍了Ford-Fulkerson方法的理论基础,本篇给出一种Java的实现. 先借助伪代码熟悉下流程 FORD-FULKERSON(G,t,s) 1 for each edge(u,v) ...

  4. Windows核心编程学习九:利用内核对象进行线程同步

    注:源码为学习<Windows核心编程>的一些尝试,非原创.若能有助于一二访客,幸甚. 1.程序框架 #include "Queue.h" #include <t ...

  5. ActiveMQ NMS使用过程中的一点经验

    最近,项目中使用到了ActiveMQ获取第三方推送过来的数据.具体背景是:公司需要监控全国各地车辆实时运行的GPS数据,但监控本身不是公司做的,而是交给第三方公司做,第三方采集GPS数据后推送给我们. ...

  6. 浅谈 Linux

          1969年,美国贝尔实验室的肯-汤普森在DEC PDP-7机器上开发出了UNIX系统.      1971年,肯-汤普森的同事丹尼斯-里奇发明了C语言:1973年,UNIX系统的绝大部分源 ...

  7. grunt--自动化任务快速上手

    这篇文章将带领你用Grunt来提速和优化网站开发的流程.首先我们会简短介绍Grunt的功能,然后我们直接上手,介绍如何用Grunt的不同插件来替你完成网站项目开发中的很多繁冗工作. 接着我们会创建一个 ...

  8. json字符串参数

    jsp部分        json字符串的属性应该都是实体类的属性 function saveCashier(){ layer.closeAll(); var Reapply = document.g ...

  9. 取汉子拼音首字母的C#和VB.Net方法

    转载http://blog.fwhyy.com/2012/03/take-the-first-initials-method-of-csharp-and-vbnet/

  10. jquery验证表单是否满足正则表达式是否通过验证例子

    //验证通用函数 a表示元素对象,b表示正则表达式,c存bool值 function testyz(a,b,c){ c=false; $(a).on("blur",function ...