Logo

Programming-Idioms

History of Idiom 37 > diff from v11 to v12

Edit summary for version 12 by :

Version 11

2015-09-05, 11:22:39

Version 12

2015-10-29, 14:05:13

Idiom #37 Currying

Technique of transforming a function that takes multiple arguments and returning a function for which some of the arguments are preset.

Idiom #37 Currying

Technique of transforming a function that takes multiple arguments and returning a function for which some of the arguments are preset.

Code
(curry range 1)
Code
(curry range 1)
Comments bubble
range(a,b) = [a..b]
(curry range 1) = _λ i ↦ [1..i]

Haskell begins with most functions already curried and allowing partial projection, hence the uncurried range sprang to mind as a notable exception for this illustration.
Comments bubble
range(a,b) = [a..b]
(curry range 1) = _λ i ↦ [1..i]

Haskell begins with most functions already curried and allowing partial projection, hence the uncurried range sprang to mind as a notable exception for this illustration.
Doc URL
http://hackage.haskell.org/package/base/docs/Prelude.html#v:curry
Doc URL
http://hackage.haskell.org/package/base/docs/Prelude.html#v:curry
Imports
import std.functional;
Imports
import std.functional;
Code
int add(int n1, int n2)
{
	return n1 + n2;
}

alias add5 = curry!(add, 5);
Code
int add(int n1, int n2)
{
	return n1 + n2;
}

alias add5 = curry!(add, 5);
Demo URL
http://dpaste.dzfl.pl/6cceb5d75a7c
Demo URL
http://dpaste.dzfl.pl/6cceb5d75a7c
Code
function curry (fn, scope) {
   
    scope = scope || window;
    
    // omit curry function first arguments fn and scope
    var args = Array.prototype.slice.call(arguments, 2);
    
    return function() {
	var trueArgs = args.concat(Array.prototype.slice.call(arguments, 0));
        fn.apply(scope, trueArgs);
    };
}
Code
function curry (fn, scope) {
   
    scope = scope || window;
    
    // omit curry function first arguments fn and scope
    var args = Array.prototype.slice.call(arguments, 2);
    
    return function() {
	var trueArgs = args.concat(Array.prototype.slice.call(arguments, 0));
        fn.apply(scope, trueArgs);
    };
}
Comments bubble
Call curry on a function, a scope and then just enumerate the arguments you want to be curried in the returned function ;)
Comments bubble
Call curry on a function, a scope and then just enumerate the arguments you want to be curried in the returned function ;)
Origin
http://www.dustindiaz.com/javascript-curry/
Origin
http://www.dustindiaz.com/javascript-curry/
Demo URL
http://jsfiddle.net/adriangonzy/9SW9p/1/
Demo URL
http://jsfiddle.net/adriangonzy/9SW9p/1/