The _.pipeline() method takes a list of functions, either as an array or as arguments, and returns a function that takes some value as its argument and runs it through a pipeline of the original functions given in this function.
Syntax:
_.pipeline( func1, func2,..., funcn );
Parameters: This method accepts a single parameter in the form of n functions and hence operate upon given value.
Return Value: This method returns a function.
Note: This will not work in normal JavaScript because it requires the underscore.js contrib library to be installed.Â
underscore.js contrib library can be installed using npm install underscore-contrib –save.
Example 1: passing functions as arguments.
Javascript
// Defining underscore contrib variableconst _ = require('underscore-contrib');Â
function cb(x) {Â Â Â Â return x * x * x;};function tp(x) {Â Â Â Â return 3 * x;};function db(x) {Â Â Â Â return 2 * x;};let func = _.pipeline(cb, tp, db);Â
console.log(func(5)); |
Output:
750
Example 2: Passing functions as an array.
Javascript
// Defining underscore contrib variableconst _ = require('underscore-contrib');Â
function cb(x) {Â Â Â Â return x * x * x;};function tp(x) {Â Â Â Â return 3 * x;};function db(x) {Â Â Â Â return 2 * x;};Â
let func = _.pipeline([tp, cb, db]);Â
console.log(func(5)); |
Output:
6750
