Skip to content

Instantly share code, notes, and snippets.

@codecontemplator
Last active October 10, 2017 18:55
Show Gist options
  • Save codecontemplator/4507f53a5c8a1861b37e52a3aa02ab96 to your computer and use it in GitHub Desktop.
Save codecontemplator/4507f53a5c8a1861b37e52a3aa02ab96 to your computer and use it in GitHub Desktop.
Maybe monad in powershell
function mbind
{
param(
[parameter(valuefrompipeline=$true)]$Ma,
[parameter(position=0)][scriptblock]$fMb
)
process
{
if ($Ma) {
& $fMb $Ma
} else {
$null
}
}
}
function safe-div($a, $b)
{
write-host "$a / $b"
if ($b -eq 0) {
$null
} else {
$a / $b
}
}
(safe-div 10 5) | mbind { param($v) safe-div 4 $v } | mbind { param($v) safe-div $v 0 } | mbind { param($v) safe-div $v 2 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment