The below function Get-PermutationsAll takes an array of values and spits out a list of all possible combinations of these items. For example:
$testarray = @(“1″,”2”)
Get-PermutationsAll -array $testarray
1
1 1
1 2
2
2 1
2 2
I generally understand what’s happening with this function, but as soon as I get to the line where the function itself is called within itself, I can’t wrap my head around what’s going on. Any good samaritans out there that have time to explain?
function Get-PermutationsAll { [CmdletBinding()] Param( [Parameter(Mandatory=$False)] $array, [Parameter(Mandatory=$False)] $cur = "", [Parameter(Mandatory=$False)] $depth = 0, [Parameter(Mandatory=$False)] $list = @() ) $depth ++ for ($i = 0; $i -lt $array.Count; $i++) { $list += $cur+" "+$array[$i] if ($depth -lt $array.Count) { $list = Get-PermutationsAll $array ($cur+" "+$array[$i]) $depth $list } } $list }
submitted by /u/fourierswager
[link] [comments]
The post Understanding function that invokes itself within itself. appeared first on How to Code .NET.