Skip to content

add new binary (unary?) operator -ALL and -NOTALL and -ANY  #7925

Closed

Description

@p0W3RH311

Steps to reproduce

$a = $true,$true,$false
$b = $false,$false,$false

# Not one False
PS> $a -all $true
$false

# Not one True
PS> $b -all $false
$true 

PS> $a -notall $true
$true

PS> $b -notall $false
$false 

# At least one True
PS> $a -any $true
$true

# At least one True
PS> $b -any $true
$false

another example:

if ($a -any $true -and $a -notall $true) {
   "At least one True and one False"
}

maybe a short syntax like this:

if (-any $a -and -notall $a) {
   "At least one True and one False"
}

Activity

  1. Powershell-User commented on Oct 2, 2018

    @Powershell-User

    -any is the -contains operator
    -notany is the -contains operator

    For all, just use -notcontains if its binary:

    #For -Any or -All
    
    ($a -notcontains $true)
    
    #if any are $true, it returns false, if none are $true (all $false), it returns $true
    
    ($a -notcontains $false)
    
    #if any are $false, it returns false, if none are $false (all $true), it returns $true
    

    -notall is also ambiguous, does it mean that at least one is and at least one is not? in which case:

    ($a -contains $true -and $a -contains $false)
    
    #inherently has the notall property
    

    For objects containing multiple values (other than true/false), you can use Group-Object, Sort-Object and a few other cmdlets. I don't see where these operators would improve things.

  2. kvprasoon commented on Oct 2, 2018

    @kvprasoon
    Contributor

    looks interesting !!!

  3. p0W3RH311 commented on Oct 3, 2018

    @p0W3RH311
    Author

    -any is the -contains operator
    -notany is the -contains operator

    For all, just use -notcontains if its binary:

    #For -Any or -All
    
    ($a -notcontains $true)
    
    #if any are $true, it returns false, if none are $true (all $false), it returns $true
    
    ($a -notcontains $false)
    
    #if any are $false, it returns false, if none are $false (all $true), it returns $true
    

    -notall is also ambiguous, does it mean that at least one is and at least one is not? in which case:

    ($a -contains $true -and $a -contains $false)
    
    #inherently has the notall property
    

    For objects containing multiple values (other than true/false), you can use Group-Object, Sort-Object and a few other cmdlets. I don't see where these operators would improve things.

    ($a -in $collection) is the same ($collection -contains $a)
    in other case -all -notall and -any is easy to read and also simplified syntax

  4. vexx32 commented on Oct 3, 2018

    @vexx32
    Collaborator

    I would argue that $array -contains $item is far easier to read than $array -any $item (at least for those of us perhaps who are most accustomed to English over other languages and/or have it as our birth-tongue).

    And really, the only thing you can't currently do with a single operator that you're suggesting is the -notall, which would be most neatly solved, I think with a fairly simple expression that arguably makes more semantic sense than the overly-shortened -notall operator suggested:

    @($Array -eq $Item).Count -ne $Array.Count

    (And, of course, Powershell-User's suggestion also has merit there, although I am unsure which is "better", at least performance-wise.)

    While I do like the idea, as Powershell-User mentions, the functionality is already present, and I think the existing operators are more than sufficiently clear in intention.

  5. rkeithhill commented on Oct 3, 2018

    @rkeithhill
    Collaborator

    $array -contains $item is far easier to read than $array -any $item

    Agreed but if any accepts (or by default uses) a filter expression it becomes a tiny bit more interesting. This would be kind of like the .Where{} method e.g. (2,4,6,7).Where{$_ -le 4}. An Any method would accept a filter e.g. (2,4,6,7).Any{$_ % 2} which in this case, detects if there are any odd numbers in the array. Or as an operator (2,4,6,7) -any {$_ % 2}. You can do this today with LINQ:

    [int[]]$arr = 2,4,6,7
    [System.Func[int,bool]]$func = {param($val) return ($val % 2) }
    [System.Linq.Enumerable]::Any($arr, $func)
    

    But it's kind of a PITA.

    I suppose you could do the same with -all e.g. (2,4,6,7) -all {$_ -lt 10} would return $true.

    Or maybe it would be better if PowerShell just worked more easily with the various LINQ Enumerable methods?

  6. vexx32 commented on Oct 3, 2018

    @vexx32
    Collaborator

    As you state, .Where{} is perfectly capable of this.

    I believe there have been discussions about whether or not to add -where and foreach operators as well, which it appears may be more useful to you in this kind of scenario.

  7. BrucePay commented on Oct 3, 2018

    @BrucePay
    Collaborator

    When we get around to supporting LINQ, these operations will be covered and it will simply be

    (2,4,6).All{$_ % s -eq 0}
    (1,2,3,4,5,6).Any{$_ -gt 5}
  8. added
    WG-Languageparser, language semantics
    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif
    on Oct 3, 2018
  9. HumanEquivalentUnit commented on Oct 3, 2018

    @HumanEquivalentUnit
    Contributor

    When we get around to supporting LINQ

    Where's the GoFundMe for LINQ support? ;) 👍

  10. vexx32 commented on Oct 4, 2018

    @vexx32
    Collaborator

    If I knew anything about LINQ I'd be all over that, but I think it's a bit over my head for the present moment. 😄

  11. IISResetMe commented on Feb 16, 2019

    @IISResetMe
    Collaborator

    Was looking for interesting issues to work on last night and stumbled on this.

    What Keith Hill (@rkeithhill) suggests (lambda containment operators) makes much more sense, and can easily be implemented with a few modifications to the existing -contains op implementation, like in this branch:

    image


    Bruce Payette (@BrucePay) are there any active efforts to pursue LINQ/extension methods any time soon? No need to add this if that's on the roadmap

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Languageparser, language semantics

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions