RosettaCodeData/Task/Monads-Maybe-monad/J/monads-maybe-monad.j
2023-07-01 13:44:08 -04:00

27 lines
563 B
Text

NB. monad implementation:
unit=: <
bind=: (@>)( :: ])
NB. monad utility
safeVersion=: (<@) ( ::((<_.)"_))
safeCompose=:dyad define
dyad def 'x`:6 bind y'/x,unit y
)
NB. unsafe functions (fail with infinite arguments)
subtractFromSelf=: -~
divideBySelf=: %~
NB. wrapped functions:
safeSubtractFromSelf=: subtractFromSelf safeVersion
safeDivideBySelf=: divideBySelf safeVersion
NB. task example:
safeSubtractFromSelf bind safeDivideBySelf 1
┌─┐
│0│
└─┘
safeSubtractFromSelf bind safeDivideBySelf _
┌──┐
│_.│
└──┘