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
$ErrorActionPreference = "Stop" | |
function Assert-True { | |
[CmdletBinding()] # add ErrorAction support | |
Param([boolean]$Value) | |
if (-not $Value) { | |
throw "Failed assertion" | |
} | |
} |
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
$ErrorActionPreference = "Stop" | |
# an error will thrown and caught, so you wont see it | |
try { | |
throw "something bad happened..." | |
} catch { | |
Write-Host "It looks like $_, but everything's still fine." | |
} finally { | |
Write-Host "I knew I would get here eventually" | |
} |
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 div($n, $d) {$n / $d} # a seemingly pure function | |
div 1 0 # throws 'Attempted to divide by zero' error |
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
# this outer scope contains only immutable variables | |
$var = & { | |
# this scope contains mutable temporary variables | |
$queue = [System.Collections.Generic]::new() | |
$queue.Enqueue(@{Id = 1}) | |
while ($queue.Count) { | |
$jobInfo = $queue.Dequeue() | |
$newJobInfo = Update-RemoteJob $jobInfo # non-deterministic | |
if ($newJobInfo) { | |
$queue.Enqueue($newJobInfo) |
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 Invoke-AfterEach { | |
Param( | |
[scriptblock[]]$ScriptBlock, | |
[scriptblock]$OnTaskComplete | |
) | |
$ScriptBlock | % { | |
Start-Job { | |
Param($action, $callback) | |
&$action | |
&$callback |
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
$job = Start-Job $action | |
... # do some other stuff | |
$job | Wait-Job |
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
# invokes the action, then calls the callback upon completion | |
function Invoke-Then([scriptblock]$action, [scriptblock]$callback) { | |
Start-Job { | |
&$using:action | |
&$using:callback | |
} | |
} |
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
$job = Start-Job $action # `$action` will be executed asynchronously | |
Invoke-NextCommand # will be called before `$action` has completed | |
$job | Wait-Job -Timeout 60 | Receive-Job |
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
$d = $a | % {&$aToB $_} | % {&$bToC $_} | % {&$cToD $_} |
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
Import-Module functional | |
$aToD = $aToB, $bToC, $cToD | Merge-ScriptBlock |