Filters are a special form of functions.
Define filter:
Filter (keyword) Filter Name (parameter) {code}
Unlike the definition function, the function uses the "$ input" variable to obtain information from the pipeline operator, while the filter uses the variable "$ _". In terms of data processing, functions and filters are also different. In a function, the "$ input" variable accepts all the information before performing operations. In a filter, the "$ _" variable accepts information from pipeline operators and performs filtering (operation) operations.
To sum up, for a function: the pipe operator transfers data to the function in the form of a set at a time, and the function must be processed cyclically or recursively. For filter: the code is executed once every time a data is imported into the MPs queue until all data is passed in.
At the end of the function described in the previous section, the following example is used:Copy codeThe Code is as follows: function fun {
Begin {
"Start"
$ I = 1
}
Process {
"Run" + $ I
$ _. Name
$ I ++
}
End {
"End"
}
}
Ls | fun
Here, change it:Copy codeThe Code is as follows: function fun {
"Run"
$ _. Name
}
Ls | fun
The running result is as follows:
As you can see, there is no "ls" output. In the original example, because the "process" declaration is used, the code block can be executed cyclically until the data processing ends. However, the results in this example are quite different.
If you change it to the following format:Copy codeThe Code is as follows: filter fun {
"Run"
$ _. Name
}
Ls | fun
Running result:
In this way, you can see the same output as the content in the previous section.
That is to say, functions containing "process" to mark code blocks also act as filters.