Skip to content

WIP: Add new binary lambda containment operators (-any/-all) - #11060

Closed
Mathias R. Jessen (IISResetMe) wants to merge 3 commits into
PowerShell:masterfrom
IISResetMe:feature/any-all-lambda
Closed

Mathias R. Jessen (IISResetMe) wants to merge 3 commits into
PowerShell:masterfrom
IISResetMe:feature/any-all-lambda

Conversation

@IISResetMe

@IISResetMe Mathias R. Jessen (IISResetMe) commented Nov 13, 2019 •

Copy link
Copy Markdown
Collaborator

PR Summary

This pull request adds 2 new containment operators:

  • -any (ex. 1..5 -any { $_ -eq 2 })
    • Takes a collection as its lhs operand and a predicate as its rhs operand. Returns true if the predicate is true for any lhs value, returns false otherwise
  • -all (ex. 1..5 -all { $_ -gt 0 })
    • Takes a collection as its lhs operand and a predicate as its rhs operand. Returns true if the predicate is true for all lhs value, returns false otherwise

If a non-delegate-like object is passed as the rhs operand, we perform a scalar equality comparison against each item in lhs.

PR Context

This PR is partly motivated by the discussion in #7925, and my own frustration with having to cast nested loop structures to perform similar containment testing.

For example, to find a directory in which all files a marked hidden:

# with the current binary operators
Get-ChildItem -Directory -Recurse |Where {
  $files = @($_ |Get-ChildItem -File -Force) 
  $files.Count -gt 0 -and ($files |Where Attributes -notlike "*hidden*").Count -eq 0
}

# with `-all`
Get-ChildItem -Directory -Recurse |Where {
  @($_ |Get-ChildItem -File -Force) -all {$_.Attributes -like "*hidden*"}
}

(I'm aware that dynamic parameter aliases exist specifically for attribute filtering in the FileSystem provider, but that's sort of missing the point, -any/-all would work in any context)

TODO:

  • Should we include a negation? (-notany/-none and -notall)
  • Should we include case-sensitive variants? (only applies to the -contains fallback edge case)
  • Tests

PR Checklist

This is a prototype for `-any/-all` lambda operators, modelled on the current ParserOps.ContainsOperatorCompiled method.
TODO:
 - non-predicate dispatch for all
 - -not versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant