JavaScript-Curry
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的更多相关文章
- javascript curry 柯里化函数 仿lodash的curry
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 详解JavaScript函数模式
JavaScript设计模式的作用是提高代码的重用性,可读性,使代码更容易的维护和扩展.在javascript中,函数是一类对象,这表示他可以作为参数传递给其他函数:此外,函数还可以提供作用域. 创建 ...
- 初涉JavaScript模式 (10) : 函数 【进阶用法】
写在前面 不知不觉写到第10篇了.这篇写起来很忐忑,终于和高级搭上边了(呵呵),这篇我们 主要 说一下 JS 方法的部分高级用法(我知道的),笔者水平有限,难免有错.废话不多少,进入正文. 初始化 我 ...
- JavaScript Patterns 4.10 Curry
Function Application apply() takes two parameters: the first one is an object to bind to this inside ...
- Javascript函数柯里化(curry)
函数柯里化currying,是函数式编程非常重要的一个标志.它的实现需要满足以下条件,首先就是函数可以作为参数进行传递,然后就是函数可以作为返回值return出去.我们依靠这个特性编写很多优雅酷炫的代 ...
- [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 ...
- JavaScript基础Curry化(021)
时候我们希望函数可以分步接受参数,并在所有参数都到位后得到执行结果.为了实现这种机制,我们先了解函数在Javascript中的应用过程: 1. 函数的“应用”(Function Application ...
- [Javascript] Understand Curry
The act of currying can be described as taking a multivariate function and turning it into a series ...
- 数据结构与算法JavaScript (三) 链表
我们可以看到在javascript概念中的队列与栈都是一种特殊的线性表的结构,也是一种比较简单的基于数组的顺序存储结构.由于javascript的解释器针对数组都做了直接的优化,不会存在在很多编程语言 ...
- 《JavaScript语言精粹》小记
一.前言 以下内容均摘自<JavaScript语言精粹>一书,本人在读这本书时,发现作者诠释JavaScript很犀利,特别是数组部分,固记录下来,想和大家分享下. 随笔主要包含两大部分: ...
随机推荐
- Dragon Balls(hdu3635带权并查集)
题意:n个城市有n个龙珠,T a,b 代表将龙珠a移动到b城市,Q a代表查询龙珠a所在城市,该城市有多少颗龙珠,该龙珠移动了多少次. 注意:移动时是将龙珠所在该城市所有龙珠都移动,每个龙珠不会再回 ...
- javascript 闭包基础分享
javascript 闭包基础分享 闭包向来给包括JavaScript程序员在内的程序员以神秘,高深的感觉,事实上,闭包的概念在函数式编程语言中算不上是难以理解的知识.如果对作用域,函数为独立的对象这 ...
- 自制AutoMapper实现DTO到持久层Entity的转换
自制AutoMapper实现DTO到持久层Entity的转换 项目中经常涉及到页面DTO更新,保存到数据库的操作,这就必然牵扯到DTO和持久层对象的转换,常见的第三方库有: java:dozer .n ...
- TFS Services 集成Docker
随着Docker的爆发,越来越多软件研发团体开始享用和受益于Docker系统体系带来的巨大好处.Docker的使用,除了减少软硬件成本的立竿见影效果,更是对软件生命周期过程开发.测试.生成部署和运维整 ...
- JavaScript利用闭包实现模块化
利用闭包的强大威力,但从表面上看,它们似乎与回调无关.下面一起来研究其中最强大的一个:模块. function foo() { var something = "cool"; va ...
- MVC源码分析 - Controller创建和创建扩展
上一篇, 出现了一个至关重要的类:MvcHandler, 接下来就来看一下MvcHandler吧. 先不看具体方法, 先看一下类里面的情况. //这里实现了两个重要的接口, 异步处理和同步处理的接口p ...
- 微型orm框架--dapper的简单使用
1.安装 首先使用nuget安装dapper,因为这里的示例是使用mysql,所以还要安装mysql的驱动.如下图: 2 数据库表 脚本 ; -- -------------------------- ...
- Grant简介以及安装
Grant简介以及安装 1. 安装Grunt-cli需要使用npm,全局安装 命令:npm install –g grunt-cli(可能会涉及权限问题) 注意,安装grunt-cli并不等于安 ...
- struts2框架学习(二)
---恢复内容开始--- 一.搭建步奏 1.新建web项目,进行必备Jar包的拷贝,放到lib目录以便项目的引用. 2.拷贝框架的核心配置文件Struts.xml,放到src目录下,保留一个基本配置即 ...
- 浅谈CSS3 box-reflect 属性
今天说一下 CSS3的box-reflect属性,也就是倒影特效. 语法: box-reflect:包括3个值. 1. direction 定义方向,取值包括 above . below . left ...