★ wanayoo — archive 1999 https://github.com/PowerShell/PowerShell/pull/11068Nouvelle recherche | Portail wanayoo
Skip to content

Conversation

IISResetMe
Copy link
Collaborator

@IISResetMe IISResetMe commented Nov 14, 2019 •

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.

A related update to the EditorSyntax definition has been prepared here: PowerShell/EditorSyntax#189

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)
  • Should we include more/better tests? (for containment operators in general)

PR Checklist

IISResetMe added a commit to IISResetMe/EditorSyntax that referenced this pull request Nov 14, 2019
To reflect changes to binary comparison operators in PowerShell/PowerShell#11068
@IISResetMe IISResetMe marked this pull request as ready for review November 14, 2019 15:43
IISResetMe added a commit to IISResetMe/EditorSyntax that referenced this pull request Nov 16, 2019
To reflect changes to binary comparison operators in PowerShell/PowerShell#11068
@vexx32 vexx32 added the CL-Engine Indicates that a PR should be marked as an engine change in the Change Log label Nov 18, 2019
@vexx32
Copy link
Collaborator

vexx32 commented Nov 18, 2019

RE: The additional questions -- yes, I'd say it's worth including the negative versions, but I don't see a lot of value in a case-sensitive version, since folx can just use case-sensitive operators inside the scriptblock expression to get the same effect.

Copy link
Collaborator

@vexx32 vexx32 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks lovely overall!

Couple minor thoughts in a few parts, but other than that this is good to go imo! 💖

@ghost ghost added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept labels Nov 18, 2019
@TravisEz13 TravisEz13 added this to the 7.1.0-preview.1 milestone Nov 23, 2019
@ghost ghost assigned daxian-dbw Nov 23, 2019
@ghost ghost added the Review - Needed The PR is being reviewed label May 27, 2020
@ghost
Copy link

ghost commented May 27, 2020

This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days.
Mainainer, Please provide feedback and/or mark it as Waiting on Author

@adityapatwardhan adityapatwardhan removed this from the 7.1.0-preview.4 milestone Jun 29, 2020
@IISResetMe IISResetMe force-pushed the feature/any-all-lambda7 branch from 8b0da2a to e60f1d4 Compare October 13, 2020 21:17
@IISResetMe IISResetMe closed this May 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-Engine Indicates that a PR should be marked as an engine change in the Change Log Review - Needed The PR is being reviewed
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants