Last active
June 14, 2023 17:17
-
-
Save ChecksumFailed/59e19e1632f2f1734f76c648fa7e95f3 to your computer and use it in GitHub Desktop.
Pipe and Compose in ServiceNow
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
function f1(a) { | |
gs.info("Hello " + a); | |
return gs.now(); | |
} | |
function f2(a) { | |
gs.info("The date is: " + a); | |
return 70; | |
} | |
function f3(a) { | |
gs.info("The temperature is " + a + " degrees"); | |
return "Sunny"; | |
} | |
function f4(a) { | |
gs.info("The weather is " + a); | |
} | |
function pipe() { | |
var fns = Array.prototype.slice.call(arguments); | |
return function (result) { | |
var value = result; | |
for (var i = 0; i < fns.length; i++) { | |
value = fns[i](value); | |
} | |
return value; | |
}; | |
} | |
function pipe2() { | |
var fns = Array.prototype.slice.call(arguments); | |
return function (result) { | |
return fns.reduce(function (value, fn) { | |
return fn(value); | |
}, result); | |
}; | |
} | |
pipe(f1, f2, f3, f4)("bob"); | |
pipe2(f1, f2, f3, f4)("joe"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment