@stdlib/utils-uncurry
v0.2.2
Published
Transform a curried function into a function invoked with multiple arguments.
Downloads
21
Readme
uncurry
Transform a curried function into a function invoked with multiple arguments.
Installation
npm install @stdlib/utils-uncurry
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
See Also
@stdlib/utils-curry
: transform a function into a sequence of functions each accepting a single argument.@stdlib/utils-uncurry-right
: transform a curried function into a function invoked with multiple arguments.
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.