Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Powershell have an Aggregate/Reduce function?

Tags:

powershell

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} 
like image 619
George Mauer Avatar asked Aug 06 '14 15:08

George Mauer


1 Answers

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.

like image 196
Keith Hill Avatar answered Nov 03 '22 02:11

Keith Hill