I realize there are related questions, but all the answers seem to be work-arounds that avoid the heart of the matter. Does powershell have an operation that can use a scriptblock to aggregate elements of an array into a single value? This is what is known in other languages as aggregate
or reduce
or fold
.
I can write it myself pretty easily, but given that its the base operation of any list processing, I would assume there's something built in I just don't know about.
So what I'm looking for is something like this
1..10 | Aggregate-Array {param($memo, $x); $memo * $x}
There is not anything so obviously named as Reduce-Object but you can achieve your goal with Foreach-Object:
1..10 | Foreach {$total=1} {$total *= $_} {$total}
BTW there also isn't a Join-Object to merge two sequences of data based on some matching property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With