Skip to content

Instantly share code, notes, and snippets.

@evancz
Last active January 4, 2016 09:29
Show Gist options
  • Save evancz/8602608 to your computer and use it in GitHub Desktop.
Save evancz/8602608 to your computer and use it in GitHub Desktop.
How one might implement a Workflow (Monad) library in some future version of Elm.
module Workflow where
import List
import Maybe as M
import Either as E
type Workflow container =
{ return : forall a. a -> container a
, bind : forall a. container a -> (a -> container b) -> container b
}
maybe : Workflow Maybe
maybe =
{ return = M.Just
, bind m f = M.maybe M.Nothing f m
}
either : Workflow Either
either =
{ return = E.Right
, bind e f = E.either E.Left f e
}
sequence : Workflow container -> [container a] -> container [a]
sequence wf list =
let cons w ws =
wf.bind w <| \x ->
wf.bind ws <| \xs -> wf.return (x::xs)
in
List.foldr cons (wf.return []) list
map : Workflow container -> (a -> container b) -> [a] -> container [b]
map workflow f list =
sequence workflow (List.map f list)
import open Workflow
import Maybe
import String
ns : Maybe.Maybe [Int]
ns = map maybe String.toInt ["123","42","56"]
main = asText ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment