|
||
---|---|---|
.. | ||
docs | ||
lib | ||
package.json | ||
README.md |
uncurry
Transform a curried function into a function invoked with multiple arguments.
Usage
var uncurry = require( '@stdlib/utils/uncurry' );
uncurry( fcn[, arity][, thisArg] )
Transforms a curried function into a function invoked with multiple arguments.
function add( x ) {
return function add( y ) {
return x + y;
};
}
var fcn = uncurry( add );
var sum = fcn( 2, 3 );
// returns 5
To enforce a fixed number of parameters, provide an arity
argument.
function add( x ) {
return function add( y ) {
return x + y;
};
}
var fcn = uncurry( add, 2 );
var sum = fcn( 9 );
// throws <Error>
To specify an execution context, provide a thisArg
argument.
function addX( x ) {
this.x = x;
return addY;
}
function addY( y ) {
return this.x + y;
}
var fcn = uncurry( addX, {} );
var sum = fcn( 2, 3 );
// returns 5
The function supports providing both an arity
and execution context.
function addX( x ) {
this.x = x;
return addY;
}
function addY( y ) {
return this.x + y;
}
var fcn = uncurry( addX, 2, {} );
var sum = fcn( 2, 3 );
// returns 5
sum = fcn( 4 );
// throws <Error>
Examples
var curry = require( '@stdlib/utils/curry' );
var uncurry = require( '@stdlib/utils/uncurry' );
var uncurried;
var curried;
var bool;
var out;
var i;
function add( x, y, z, w, t, s ) {
return x + y + z + w + t + s;
}
out = add( 0, 10, 20, 30, 40, 50 );
// returns 150
// Transform `add` into a curried function:
curried = curry( add );
out = curried;
for ( i = 0; i < add.length; i++ ) {
out = out( i*10 );
}
bool = ( out === 150 );
// returns true
// Uncurry a curried function:
uncurried = uncurry( curried );
out = uncurried( 0, 10, 20, 30, 40, 50 );
// returns 150