This repository was archived by the owner on Feb 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Piping
itevie edited this page Sep 16, 2023
·
3 revisions
Piping is a way to make functions using functions using functions and so on simpler, you simply start with a value and you chain functions together.
Here is the basic syntax:
initialValue >> function1 >> function2 >> function3; // and so on
The initialValue
can be absolutely anything, from a number to a string to a function.
While the functions afterwards can ONLY be a: call expression, member expression or an identifier, here are some examples:
// Here, the only arguments passed to the functions is the initialValue as parameter 1 as no other arguments are given
// This is exactly the same as: function1(function2(function3(intialValue)));
initialValue >> function1() >> function2() >> function3();
// Or, in this case we can omit the parameters:
initialValue >> function1 >> function2 >> function3;
// Or, for example with a member expression
intiailValue >> console.writeLine; // This is the exact same as console.writeLine(intitialValue);
func add(num1, num2) {
return num1 + num2;
}
func mul(num1, num2) {
return num1 * num2;
}
1 >> add(2) >> mul(3) >> console.writeLine;
// The result in this is: 9
// The above code would be the exact same as
console.writeLine(mul(add(1, 2), 3)); // Also logs: 9
Using this method, it's a lot easier to understand, and how it works, is basically how you'd read it.
1, add 2, multiply by 3, then log it to console
Compared to:
log the addition of 1 + 2 multiplied by 3