Last active
January 4, 2016 09:29
-
-
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.
This file contains 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 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) |
This file contains 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
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