time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

papply_right.js (2177B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isFunction = require( '@stdlib/assert/is-function' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Returns a function of smaller arity by partially applying arguments from the right.
     30 *
     31 * @param {Function} fcn - function to partially apply
     32 * @param {...*} [args] - arguments to partially apply
     33 * @throws {TypeError} first argument must be a function
     34 * @returns {Function} partially applied function
     35 *
     36 * @example
     37 * function say( text, name ) {
     38 *     return text + ', ' + name + '.';
     39 * }
     40 *
     41 * var toGrace = papplyRight( say, 'Grace Hopper' );
     42 *
     43 * var str = toGrace( 'Hello' );
     44 * // returns 'Hello, Grace Hopper.'
     45 *
     46 * str = toGrace( 'Thank you' );
     47 * // returns 'Thank you, Grace Hopper.'
     48 */
     49 function papplyRight( fcn ) {
     50 	var pargs;
     51 	var len;
     52 	var i;
     53 	if ( !isFunction( fcn ) ) {
     54 		throw new TypeError( 'invalid argument. First argument must be a function. Value: `' + fcn + '`.' );
     55 	}
     56 	len = arguments.length - 1;
     57 	pargs = new Array( len );
     58 	for ( i = 1; i < arguments.length; i++ ) {
     59 		pargs[ i-1 ] = arguments[ i ];
     60 	}
     61 	return pappliedRight;
     62 
     63 	/**
     64 	* Partially applied function.
     65 	*
     66 	* @private
     67 	* @param {...*} [args] - function arguments
     68 	* @returns {*} partially applied function result
     69 	*/
     70 	function pappliedRight() {
     71 		var nargs;
     72 		var args;
     73 		var j;
     74 		nargs = arguments.length;
     75 		args = new Array( len+nargs );
     76 		for ( j = 0; j < args.length; j++ ) {
     77 			if ( j >= nargs ) {
     78 				args[ j ] = pargs[ j-nargs ];
     79 			} else {
     80 				args[ j ] = arguments[ j ];
     81 			}
     82 		}
     83 		return fcn.apply( null, args );
     84 	}
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = papplyRight;