Powershell: add new binary (unary?) operator -ALL and -NOTALL and -ANY

Created on 2 Oct 2018  路  10Comments  路  Source: PowerShell/PowerShell

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"
}
Issue-Discussion WG-Language

Most helpful comment

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}

All 10 comments

-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.

looks interesting !!!

-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

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.

$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?

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.

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}

When we get around to supporting LINQ

Where's the GoFundMe for LINQ support? ;) 馃憤

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. 馃槃

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

What @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


@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

Was this page helpful?
0 / 5 - 0 ratings