Skip to content

Instantly share code, notes, and snippets.

@IISResetMe
Created October 9, 2014 16:53
Show Gist options
  • Save IISResetMe/cee36040106bdd4a7fbb to your computer and use it in GitHub Desktop.
Save IISResetMe/cee36040106bdd4a7fbb to your computer and use it in GitHub Desktop.
Implementing map function in PowerShell
function map
{
param(
[Parameter(Mandatory=$true,Position=0)]
[scriptblock]
$Function,
[Parameter(Mandatory=$true,Position=1)]
[Object[]]
$InputObject
)
if($Function.Ast.ParamBlock -eq $null)
{
$Function = [scriptblock]::Create('param($_)'+$Function.ToString())
}
elseif($Function.Ast.ParamBlock.Parameters.Count -eq 1)
{
if($Function.Ast.ParamBlock.Parameters[0].Name.ToString() -ne '$_')
{
Write-Error "Invalid ParamBlock in Function"
return $false
}
}
else
{
Write-Error "Invalid ParamBlock in Function"
return $false
}
foreach($obj in $InputObject)
{
&$Function $obj
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment