Created
October 1, 2014 15:05
-
-
Save pierre3/c62db52d977bc2841ff4 to your computer and use it in GitHub Desktop.
Linq.ps1 with Write-Verbose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Where-Block | |
{ | |
[CmdletBinding()] | |
[OutputType([scriptblock])] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[PSObject] | |
$InputObject, | |
[Parameter(Mandatory=$true, | |
Position=1)] | |
[scriptblock] | |
$Predicate | |
) | |
Begin | |
{ | |
} | |
Process | |
{ | |
foreach($item in $InputObject) | |
{ | |
return { | |
if($item -is [scriptblock]) | |
{ | |
$argument = &$item | |
if($argument -eq $null){ return } | |
}else | |
{ | |
Write-Verbose ('[Where] Input Data = {0}' -f $item); | |
$argument = $item | |
} | |
Write-Verbose ('[Where] Predicate({0})' -f $argument); | |
[bool]$result = &$Predicate $argument | |
if($result -eq $true) | |
{ | |
Write-Verbose ('[Where] return {0}' -f $argument); | |
return $argument | |
} | |
}.GetNewClosure() | |
} | |
} | |
End | |
{ | |
} | |
} | |
function Select-Block | |
{ | |
[CmdletBinding()] | |
[OutputType([scriptblock])] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[PSObject] | |
$InputObject, | |
[Parameter(Mandatory=$true, | |
Position=1)] | |
[scriptblock] | |
$Selector | |
) | |
Begin | |
{ | |
} | |
Process | |
{ | |
foreach($item in $InputObject) | |
{ | |
return { | |
if($item -is [scriptblock]) | |
{ | |
$argument = &$item | |
if($argument -eq $null){ return } | |
}else | |
{ | |
Write-Verbose ('[Select] Input Data ={0}' -f $item); | |
$argument = $item | |
} | |
Write-Verbose ('[Select] Predicate({0})' -f $argument); | |
$return = &$Selector $argument | |
Write-Verbose ('[Select] return {0}' -f $return); | |
$return | |
}.GetNewClosure() | |
} | |
} | |
End | |
{ | |
} | |
} | |
function Foreach-Block | |
{ | |
[CmdletBinding()] | |
Param | |
( | |
[Parameter(Mandatory=$true, | |
ValueFromPipeline=$true, | |
Position=0)] | |
[scriptblock[]] | |
$Sequence, | |
[Parameter(Position=1)] | |
[scriptblock] | |
$Action = $null | |
) | |
Begin | |
{ | |
} | |
Process | |
{ | |
foreach($item in $Sequence) | |
{ | |
$result = &$item | |
if($result -eq $null) { continue } | |
if($Action -eq $null) | |
{ | |
Write-Verbose ('[Foreach] return {0}' -f $result); | |
$result | |
}else{ | |
Write-Verbose ('[Foreach] Action({0})' -f $result); | |
&$Action $result | |
} | |
} | |
} | |
End | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment