Skip to content

Instantly share code, notes, and snippets.

@ulve
Last active December 29, 2017 18:49
Show Gist options
  • Select an option

  • Save ulve/a5ee92ecbfb3fbada3603b6c1002a344 to your computer and use it in GitHub Desktop.

Select an option

Save ulve/a5ee92ecbfb3fbada3603b6c1002a344 to your computer and use it in GitHub Desktop.
MonadWriter in elm
half : Int -> Writer Int String
half n =
tell ("halved " ++ (toString n)) >>= \_ -> return (n // 2)
use =
half 8 >>= half |> runReader
-- => (2, ["halved 4", "halved 8"])
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