Is there any way of ending a pipe operation while you are in a filter?
A good use would be to get the first n elements of the list, similar to SQL's TOP statement. If we wrote a filter, call it "Top", we could retrieve the first 3 elements like so:
dir | Top 3
Once we have returned the first three, we don't need to process any more elements and in effect we want to be able to tell the pipe
Please don't give me any more elements
I know that Select-Object has the -First argument but that is not the question I am asking.
I have answered the question Powershell equivalent to F# Seq.forall but instead I would rather implement it as a filter which ends pipeline input. If there was an operation called "end-pipeline", I would implement ForAll as:
filter ForAll([scriptblock] $predicate) {
process {
if ( @($_ | Where-Object $predicate).Length -eq 0 ) {
$false;
end-pipeline;
}
}
end {
$true;
}
}