Last active
December 29, 2017 18:49
-
-
Save ulve/a5ee92ecbfb3fbada3603b6c1002a344 to your computer and use it in GitHub Desktop.
MonadWriter in elm
This file contains hidden or 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
| half : Int -> Writer Int String | |
| half n = | |
| tell ("halved " ++ (toString n)) >>= \_ -> return (n // 2) | |
| use = | |
| half 8 >>= half |> runReader | |
| -- => (2, ["halved 4", "halved 8"]) |
This file contains hidden or 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
| module Writer exposing (..) | |
| type Writer w a | |
| = Writer (List w -> ( a, List w )) | |
| runWriter : Writer w a -> ( a, List w ) | |
| runWriter (Writer w) = | |
| w [] | |
| andThen : Writer w a -> (a -> Writer w b) -> Writer w b | |
| andThen (Writer w) f = | |
| Writer <| | |
| \l -> | |
| let | |
| ( av, al ) = | |
| w l | |
| (Writer g) = | |
| f av | |
| in | |
| g al | |
| return : a -> Writer w a | |
| return v = | |
| Writer <| \l -> ( v, l ) | |
| tell : w -> Writer w () | |
| tell l = | |
| Writer <| \ls -> ( (), l::ls ) | |
| (>>=) : Writer w a -> (a -> Writer w b) -> Writer w b | |
| (>>=) = | |
| andThen |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment