Looks like JavaScript, feels like Ruby, and it is a script language fitting in C programmers.
This project is maintained by Kray-G
The pipeline operator is a syntactic sugar of a function call with a single argument.
For example, 64 |> Math.sqrt
is absolutely same as Math.sqrt(64)
.
This provides a greater readability when chaining multiple functions together.
For example, see examples below a little modified from https://github.com/tc39/proposal-pipeline-operator/.
function doubleSay(str) {
return "%{str}, %{str}";
}
function capitalize(str) {
return str.toUpper(0, 1);
}
function exclaim(str) {
return str + '!';
}
var result = exclaim(capitalize(doubleSay("hello")));
System.println(result); // => "Hello, hello!"
var result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
System.println(result); // => "Hello, hello!"
The operator <|
is also available.
This is connected in opposite direction and passing a value from the right to the left.
Here is an example.
var result =
exclaim
<| capitalize
<| doubleSay
<| "hello";
System.println(result); // => "Hello, hello!"
If you want to use a function with multiple arguments in pipeline, use a lambda as a spot.
function double(x) { return x + x; }
function add(x, y) { return x + y; }
function boundScore(min, max, score) {
return Math.max(min, Math.min(max, score));
}
var person = { score: 25 };
var newScore = person.score
|> double
|> { => add(7, _) }
|> { => boundScore(0, 100, _) };
System.println(newScore); // => 57
function doubleSay(str) {
return "%{str}, %{str}";
}
function capitalize(str) {
return str.toUpper(0, 1);
}
function exclaim(str) {
return str + '!';
}
var result = exclaim(capitalize(doubleSay("hello")));
System.println(result); // => "Hello, hello!"
var result = "hello"
|> doubleSay
|> capitalize
|> exclaim;
System.println(result); // => "Hello, hello!"
Hello, hello!
Hello, hello!
function double(x) { return x + x; }
function add(x, y) { return x + y; }
function boundScore(min, max, score) {
return Math.max(min, Math.min(max, score));
}
var person = { score: 25 };
var newScore = person.score
|> double
|> { => add(7, _) }
|> { => boundScore(0, 100, _) };
System.println(newScore); // => 57
57
const double = &(n) => n * 2;
const increment = &(n) => n + 1;
// Normal case.
var r1 = double(increment(double(double(5)))); // 42
System.println(r1);
// Rewrite it with a pipeline operator.
var r2 = 5 |> double |> double |> increment |> double; // 42
System.println(r2);
42
42
var test = ["first", "second", "third"];
var r = test
|> { => Array.map(_) { => _1 + ":" + _1 } }
|> { => Array.map(_) { => String.split(_1, ":") } }
|> Array.flatten
;
System.println(r);
["first", "first", "second", "second", "third", "third"]
var str = "This is a sample string.";
str |> String.length |> System.println;
24
function doubleSay(str) {
return "%{str}, %{str}";
}
function capitalize(str) {
return str.toUpper(0, 1);
}
function exclaim(str) {
return str + '!';
}
var result =
exclaim
<| capitalize
<| doubleSay
<| "hello";
System.println(result); // => "Hello, hello!"
Hello, hello!