Dies ist ein Weg Maybe
Monade in Livescript der Umsetzung:
class Maybe
({x}:hasValue?) ->
# map :: [Maybe a -> ] (a -> b) -> Maybe b
@map = (f) ->
if !hasValue then Nothing else Just (f x)
# bind :: [Maybe a -> ] (a -> Maybe b) -> Maybe b
@bind = (f) ->
if !hasValue then Nothing else f(x)
# toString :: [Maybe a -> ] String
# note here it's not necessary for toString() to be a function
# because Maybe is can only have either one these values:
# Nothing or Just x
@show =
if !hasValue then 'Nothing' else "Just #{x.toString!}"
# static method
@pure = (x) -> Just x
Der Konstruktor nimmt eine optinal {x}
Parameter. Maybe
in Haskell wird durch Mustervergleich auf seinen Wertkonstanten implementiert. Dieser lustige Parameter ist ein Hack, da JavaScript (LiveScript) SumTypes nicht unterstützt.
Jetzt können wir definieren Just
und Nothing
als:
# Just :: x -> Maybe x
Just = (x) -> new Maybe {x}
# Nothing :: Maybe x
Nothing = new Maybe null
unsere Um zu testen, Vielleicht wollen wir definieren eine safeSqrt
Funktion:
# safeSqrt :: Number -> Maybe Number
safeSqrt = (x) ->
if x > 0 then Just (Math.sqrt x) else Nothing
# operation :: Maybe Number
operation = do ->
a = Just 4
.map (x) -> x * -16
.bind safeSqrt
console.log operation.show
Dieser Code Nothing
gedruckt wird.
liftM2
ist eine Funktion, die auf jeder Monade funktioniert. Es muss die Art des zugrundeliegenden Monade wissen, weil es verwendet pure
(das in unserer Implementierung eine statische Funktion ist):
# liftM2 :: Monad m => (a -> b -> c) -> m a -> m b -> m c
liftM2 = (monadType, f, m1, m2) -->
x1 <- m1.bind
x2 <- m2.bind
monadType.pure (f x1, x2)
Hier ist, wie wir es verwenden können:
# operation :: Maybe Number
operation = do ->
a = Just 4
.map (x) -> x * 16
.bind safeSqrt
b = safeSqrt 81
liftM2 Maybe, (+), a, b
console.log operation.show
Code in JSBin
'ret' sollte keine Instanzmethode sein. – rightfold