Skip to content

Instantly share code, notes, and snippets.

@dennissterzenbach
Created April 19, 2017 06:34
Show Gist options
  • Save dennissterzenbach/f61bf43029c27281b1b32468a8b09cb9 to your computer and use it in GitHub Desktop.
Save dennissterzenbach/f61bf43029c27281b1b32468a8b09cb9 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/seyoraveze
<!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>
// 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