'use strict';

// MODULES //

var isFunction = require( '@stdlib/utils/is-function' );
var isPositiveInteger = require( '@stdlib/utils/is-positive-integer' ).isPrimitive;


// MAIN //

/**
* Transforms a function into a sequence of functions each accepting a single argument.
*
* @param {Function} fcn - function to curry
* @param {PositiveInteger} [arity=fcn.length] - number of parameters
* @param {*} [thisArg] - evaluation context
* @throws {TypeError} first argument must be a function
* @throws {TypeError} `arity` argument must be a positive integer
*
* @example
* function add( x, y ) {
*     return x + y;
* }
*
* var f = curryRight( add );
*
* var sum = f( 2 )( 3 );
* // returns 5
*/
function curryRight( fcn, arity, thisArg ) {
	var context;
	var args;
	var len;
	if ( !isFunction( fcn ) ) {
		throw new TypeError( 'invalid input argument. First argument must be a function. Value: `' + fcn + '`.' );
	}
	if ( arguments.length < 2 ) {
		len = fcn.length;
	}
	else if ( arguments.length > 2 ) {
		len = arity;
		context = thisArg;
		if ( !isPositiveInteger( len ) ) {
			throw new TypeError( 'invalid input argument. Length argument must be positive integer. Value: `' + len + '`.' );
		}
	}
	else if ( isPositiveInteger( arity ) ) {
		len = arity;
	}
	else {
		len = fcn.length;
		context = arity;
	}
	args = [];

	/**
	* Curry function.
	*
	* @private
	* @param {*} v - curried function parameter
	* @returns {(Function|*)} curry function or curried function result
	*/
	return function curried( v ) {
		if ( args.length < len ) {
			args.unshift( v );
		}
		if ( args.length >= len ) {
			return fcn.apply( context, args );
		}
		return curried;
	}; // end FUNCTION curried()
} // end FUNCTION curryRight()


// EXPORTS //

module.exports = curryRight;

'use strict';

// MODULES //

var isFunction = require( '@stdlib/utils/is-function' );
var isPositiveInteger = require( '@stdlib/utils/is-positive-integer' ).isPrimitive;


// MAIN //

/**
* Transforms a function into a sequence of functions each accepting a single argument.
*
* @param {Function} fcn - function to curry
* @param {PositiveInteger} [arity=fcn.length] - number of parameters
* @param {*} [thisArg] - evaluation context
* @throws {TypeError} first argument must be a function
* @throws {TypeError} `arity` argument must be a positive integer
* @returns {Function} curry function
*
* @example
* function add( x, y ) {
*     return x + y;
* }
*
* var f = curryRight( add );
*
* var sum = f( 2 )( 3 );
* // returns 5
*/
function curryRight( fcn, arity, thisArg ) {
	var context;
	var args;
	var len;
	if ( !isFunction( fcn ) ) {
		throw new TypeError( 'invalid input argument. First argument must be a function. Value: `' + fcn + '`.' );
	}
	if ( arguments.length < 2 ) {
		len = fcn.length;
	}
	else if ( arguments.length > 2 ) {
		len = arity;
		context = thisArg;
		if ( !isPositiveInteger( len ) ) {
			throw new TypeError( 'invalid input argument. Length argument must be positive integer. Value: `' + len + '`.' );
		}
	}
	else if ( isPositiveInteger( arity ) ) {
		len = arity;
	}
	else {
		len = fcn.length;
		context = arity;
	}
	args = [];
	return curried;

	/**
	* Curry function.
	*
	* @private
	* @param {*} v - curried function parameter
	* @returns {(Function|*)} curry function or curried function result
	*/
	function curried( v ) {
		if ( args.length < len ) {
			args.unshift( v );
		}
		if ( args.length >= len ) {
			return fcn.apply( context, args );
		}
		return curried;
	} // end FUNCTION curried()
} // end FUNCTION curryRight()


// EXPORTS //

module.exports = curryRight;

Fix lint errors