|
|
|
|
|
by rikkus
3947 days ago
|
|
Powershell is a nice language. Here's map/filter/reduce. Golf welcome. @(1, 2, 3) `
| % { $_ + 1 } `
| ? { $_ -gt 2 } `
| Measure-Object -Sum
| Select Sum
You can write a generic reduce like this: $reduce = {
param ([scriptblock]$reducer)
begin { $initialised = $false }
process {
if (-not $initialised) {
$agg = $_
$initialised = $true
} else {
$agg = &$reducer $agg $_
}
}
end { $agg }
}
And then use it like this: @(1, 2, 3) | &$reduce { param($agg, $i) $agg + $i }
|
|