This repository has been archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
functional.psm1
283 lines (256 loc) · 7.3 KB
/
functional.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
##
# enums
#
enum MergeStrategy {
Add
Override
Fail
}
enum ParamStyle {
Implicit
Explicit
Infer
}
enum Direction {
Left
Right
}
##
# module variables
#
$Strategies = @{
Add = {
Param($a, $b)
return $a + $b
}
Override = {
Param($a, $b)
return $b
}
Fail = {
Param($a, $b)
throw "Cannot merge type '$($a.GetType())' and '$($b.GetType())'"
}
}
##
# helper functions
#
# don't use `-is [PSCustomObject]`
# https://github.com/PowerShell/PowerShell/issues/9557
function isPsCustomObject($v) {
$v.PSTypeNames -contains 'System.Management.Automation.PSCustomObject'
}
# recursively test $a and $b for deep equality
function recursiveEquality($a, $b) {
if ($a -is [array] -and $b -is [array]) {
Write-Debug "recursively test arrays '$a' '$b'"
if ($a.Count -ne $b.Count) {
return $false
}
$inequalIndexes = 0..($a.Count - 1) | ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalIndexes.Count -eq 0
}
if ($a -is [hashtable] -and $b -is [hashtable]) {
Write-Debug "recursively test hashtable '$a' '$b'"
$inequalKeys = $a.Keys + $b.Keys `
| Sort-Object -Unique `
| ? { -not (recursiveEquality $a[$_] $b[$_]) }
return $inequalKeys.Count -eq 0
}
if ((isPsCustomObject $a) -and (isPsCustomObject $b)) {
Write-Debug "a is pscustomobject: $($a -is [psobject])"
Write-Debug "recursively test objects '$a' '$b'"
$inequalKeys = $a.psobject.Properties + $b.psobject.Properties `
| % Name `
| Sort-Object -Unique `
| ? { -not (recursiveEquality $a.$_ $b.$_) }
return $inequalKeys.Count -eq 0
}
Write-Debug "test leaves '$a' '$b'"
return (($null -eq $a -and $null -eq $b) -or ($null -ne $a -and $null -ne $b -and $a.GetType() -eq $b.GetType() -and $a -eq $b))
}
# merge `$a` and `$b` recursively. If `$a` and `$b` cannot be merged,
# pass `$a` and `$b` to `$strategy` to resolve the conflict.
function recursiveMerge($a, $b, [scriptblock]$strategy) {
if ($null -eq $a) {
Write-Debug "new assignment '$b'"
return $b
}
if ($a -eq $b -or $null -eq $b) {
Write-Debug "existing assignment '$a'"
return $a
}
if ($a -is [array] -and $b -is [array]) {
Write-Debug "merge arrays '$a' '$b'"
return $a + $b | Sort-Object -Unique
}
if ($a -is [hashtable] -and $b -is [hashtable]) {
Write-Debug "merge hashtable '$a' '$b'"
$merged = @{ }
$a.Keys + $b.Keys `
| Sort-Object -Unique `
| % { $merged[$_] = recursiveMerge $a[$_] $b[$_] $strategy }
return $merged
}
if ((isPsCustomObject $a) -and (isPsCustomObject $b)) {
Write-Debug "a is pscustomobject: $($a -is [psobject])"
Write-Debug "merge objects '$a' '$b'"
$merged = @{ }
$a.psobject.Properties + $b.psobject.Properties `
| % Name `
| Sort-Object -Unique `
| % { $merged[$_] = recursiveMerge $a.$_ $b.$_ $strategy }
return [PSCustomObject]$merged
}
Write-Debug "resolve conflict '$a' '$b'"
return &$strategy $a $b
}
##
# exported functions
#
<#
.SYNOPSIS
Merges all the input objects using the specified conflict resolution strategy
.OUTPUTS
The merged value
#>
function Merge-Object {
Param(
# The objects to merge
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "Named")]
[Parameter(Mandatory, ValueFromPipeline, ParameterSetName = "Explicit")]
[ValidateNotNullOrEmpty()]
[object[]] $Object,
# The conflict resolution strategy
[Parameter(Mandatory, ParameterSetName = "Named")]
[ValidateNotNullOrEmpty()]
[MergeStrategy] $Strategy,
# Resolves merge conflicts between two objects
[Parameter(Mandatory, ParameterSetName = "Explicit")]
[ValidateScript( { $_.Ast.ParamBlock.Parameters.Count -in (0, 2) } )]
[scriptblock] $Resolver
)
if (-not $Resolver) {
$Resolver = $Strategies["$Strategy"]
}
# we need to use explicit params because implicit params are invoked in a closure,
# whereas we need our scriptblock to have access to $Strategies
$reducer = { Param($a, $b); recursiveMerge $a $b $Resolver }
$input | Reduce-Object $reducer
}
function Merge-ScriptBlock {
[OutputType([scriptblock])]
Param(
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateScript( { $_ | % { $_.Ast.ParamBlock.Parameters.Count -eq 1 } | Test-All } )]
[scriptblock[]] $ScriptBlock
)
$reducer = { Param($a, $b) { Param($arg) &$a (&$b $arg) }.GetNewClosure() }
$input | Reduce-Object $reducer
}
<#
.SYNOPSIS
Reduces a pipeline with the given reducer function
.OUTPUTS
The accumulated value
.NOTES
Reduce is an unapproved Verb, but none of the approved verbs accurately describe what we're doing,
so we are conforming to Verb-Noun convention like other *-Object cmdlets instead
#>
function Reduce-Object {
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSUseApprovedVerbs", "")]
Param(
# The function applied to the accumulator and each element of the input
[Parameter(Mandatory)]
[ValidateScript( { $_.Ast.ParamBlock.Parameters.Count -in (0, 2) } )]
[scriptblock] $Reducer,
# The objects to merge
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[object[]] $Object,
[ParamStyle] $ParamStyle = "Infer",
[Direction] $Direction = "Right"
)
# deduce and validate scriptblock invokation style
$paramCount = $Reducer.Ast.ParamBlock.Parameters.Count
$implicit = $ParamStyle -ne "Explicit" -and $paramCount -eq 0
$explicit = $ParamStyle -ne "Implicit" -and $paramCount -eq 2
if (-not ($implicit -or $explicit)) {
throw "Could not reconcile Reducer parameter count '$paramCount' with param declaration style '$ParamStyle'"
}
# normalize input
if ($Direction -eq [Direction]::Left) {
[array]::Reverse($input)
}
# invoke the reducer
$accum = $input | Select -First 1
if ($implicit) {
foreach ($object in $input | Select -Skip 1) {
# invoke in scriptblock to minimize exposure of local variables
$safelyScoped = {
Param($a, $b, [scriptblock]$reducer)
. $reducer.GetNewClosure()
}
$accum = &$safelyScoped $accum $object $Reducer
}
}
if ($explicit) {
foreach ($object in $input | Select -Skip 1) {
$accum = &$Reducer $accum $object
}
}
return $accum
}
<#
.SYNOPSIS
Returns true if all elements in the pipeline are truthy
#>
function Test-All {
[OutputType([boolean])]
Param()
foreach ($e in $input) {
if (-not $e) {
return $false
}
}
return $true
}
<#
.SYNOPSIS
Returns true if any of the elements in the pipeline are truthy
#>
function Test-Any {
[OutputType([boolean])]
Param()
foreach ($e in $input) {
if ($e) {
return $true
}
}
return $false
}
<#
.SYNOPSIS
Returns true if all elements in the pipeline are equival
.DESCRIPTION
Compares each element in the pipeline to the first pipeline element using a
deep/recursive equality check of the properties and items
#>
function Test-Equality {
[OutputType([boolean])]
Param(
# The objects to compare for equality
[Parameter(Mandatory, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[object[]] $Object
)
$head = $input | Select -First 1
$input | Select -Skip 1 | % { recursiveEquality $head $_ } | Test-All
}
##
# aliases
#
New-Alias -Name "merge" -Value Merge-Object
New-Alias -Name "reduce" -Value Reduce-Object
New-Alias -Name "forall" -Value Test-All
New-Alias -Name "exists" -Value Test-Any