Created
April 19, 2017 06:34
-
-
Save dennissterzenbach/f61bf43029c27281b1b32468a8b09cb9 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/seyoraveze
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
// first define some functions we want to call in a pipeline | |
function add(start, value) { | |
console.debug('add', start, value); | |
return start + value; | |
} | |
function multiply(start, value) { | |
console.debug('multiply', start, value); | |
return start * value; | |
} | |
function square(start) { | |
console.debug('square', start); | |
return start * start; | |
} | |
// this creates a function which pipes together the arrayOfFunctions given | |
// and this returned function we can then call using an array of parameters | |
function pipeFunctions(arrayOfFunctions) { | |
return function __pipe(params) { | |
let result; | |
arrayOfFunctions.forEach((fnToCall, index) => { | |
let parameters; | |
if (typeof params[index] !== 'undefined') { | |
parameters = params[index]; | |
} | |
// make sure it is an array we use as parameters | |
if (typeof parameters === 'undefined') { | |
parameters = []; | |
} | |
// add the result from first function call as first parameter to any | |
// subsequent function called | |
if (index > 0) { | |
parameters.unshift(result); | |
} | |
result = fnToCall.apply(this, parameters); | |
}); | |
return result; | |
}; | |
} | |
// manually pipe function calls together: | |
// ... this becomes a mess, when the pipeline grows | |
// ... also it is counter-trivial | |
const resv1 = add(1, 2); | |
const resv2 = multiply(resv1, 3); | |
const manuallyPipelinedResult = square(resv2); | |
// use the pipe-function instead: | |
// ... you simply provide an array of functions | |
// ... each takes the base/start value as first argument | |
// ... which it gets provided from the return of the previous function | |
// ... except the first function, because, you know, there's no previous one ;-) | |
// ... result will be returned | |
const pipeline = pipeFunctions([ | |
add, | |
multiply, | |
square | |
]); | |
const pipelinedResult = pipeline([ | |
[1, 2], | |
[3], | |
undefined | |
]); | |
// output the results: | |
console.debug('manuallyPipelinedResult', manuallyPipelinedResult); | |
console.debug('pipelinedResult', pipelinedResult); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">// first define some functions we want to call in a pipeline | |
function add(start, value) { | |
console.debug('add', start, value); | |
return start + value; | |
} | |
function multiply(start, value) { | |
console.debug('multiply', start, value); | |
return start * value; | |
} | |
function square(start) { | |
console.debug('square', start); | |
return start * start; | |
} | |
// this creates a function which pipes together the arrayOfFunctions given | |
// and this returned function we can then call using an array of parameters | |
function pipeFunctions(arrayOfFunctions) { | |
return function __pipe(params) { | |
let result; | |
arrayOfFunctions.forEach((fnToCall, index) => { | |
let parameters; | |
if (typeof params[index] !== 'undefined') { | |
parameters = params[index]; | |
} | |
// make sure it is an array we use as parameters | |
if (typeof parameters === 'undefined') { | |
parameters = []; | |
} | |
// add the result from first function call as first parameter to any | |
// subsequent function called | |
if (index > 0) { | |
parameters.unshift(result); | |
} | |
result = fnToCall.apply(this, parameters); | |
}); | |
return result; | |
}; | |
} | |
// manually pipe function calls together: | |
// ... this becomes a mess, when the pipeline grows | |
// ... also it is counter-trivial | |
const resv1 = add(1, 2); | |
const resv2 = multiply(resv1, 3); | |
const manuallyPipelinedResult = square(resv2); | |
// use the pipe-function instead: | |
// ... you simply provide an array of functions | |
// ... each takes the base/start value as first argument | |
// ... which it gets provided from the return of the previous function | |
// ... except the first function, because, you know, there's no previous one ;-) | |
// ... result will be returned | |
const pipeline = pipeFunctions([ | |
add, | |
multiply, | |
square | |
]); | |
const pipelinedResult = pipeline([ | |
[1, 2], | |
[3], | |
undefined | |
]); | |
// output the results: | |
console.debug('manuallyPipelinedResult', manuallyPipelinedResult); | |
console.debug('pipelinedResult', pipelinedResult); | |
</script></body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// first define some functions we want to call in a pipeline | |
function add(start, value) { | |
console.debug('add', start, value); | |
return start + value; | |
} | |
function multiply(start, value) { | |
console.debug('multiply', start, value); | |
return start * value; | |
} | |
function square(start) { | |
console.debug('square', start); | |
return start * start; | |
} | |
// this creates a function which pipes together the arrayOfFunctions given | |
// and this returned function we can then call using an array of parameters | |
function pipeFunctions(arrayOfFunctions) { | |
return function __pipe(params) { | |
let result; | |
arrayOfFunctions.forEach((fnToCall, index) => { | |
let parameters; | |
if (typeof params[index] !== 'undefined') { | |
parameters = params[index]; | |
} | |
// make sure it is an array we use as parameters | |
if (typeof parameters === 'undefined') { | |
parameters = []; | |
} | |
// add the result from first function call as first parameter to any | |
// subsequent function called | |
if (index > 0) { | |
parameters.unshift(result); | |
} | |
result = fnToCall.apply(this, parameters); | |
}); | |
return result; | |
}; | |
} | |
// manually pipe function calls together: | |
// ... this becomes a mess, when the pipeline grows | |
// ... also it is counter-trivial | |
const resv1 = add(1, 2); | |
const resv2 = multiply(resv1, 3); | |
const manuallyPipelinedResult = square(resv2); | |
// use the pipe-function instead: | |
// ... you simply provide an array of functions | |
// ... each takes the base/start value as first argument | |
// ... which it gets provided from the return of the previous function | |
// ... except the first function, because, you know, there's no previous one ;-) | |
// ... result will be returned | |
const pipeline = pipeFunctions([ | |
add, | |
multiply, | |
square | |
]); | |
const pipelinedResult = pipeline([ | |
[1, 2], | |
[3], | |
undefined | |
]); | |
// output the results: | |
console.debug('manuallyPipelinedResult', manuallyPipelinedResult); | |
console.debug('pipelinedResult', pipelinedResult); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment