Last active
October 10, 2017 18:55
-
-
Save codecontemplator/4507f53a5c8a1861b37e52a3aa02ab96 to your computer and use it in GitHub Desktop.
Maybe monad in powershell
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 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