Skip to content

Instantly share code, notes, and snippets.

@wende
Created September 5, 2017 17:26
Show Gist options
  • Save wende/4723851cdbcd0e7256873942846bb504 to your computer and use it in GitHub Desktop.
Save wende/4723851cdbcd0e7256873942846bb504 to your computer and use it in GitHub Desktop.
module Elchemy.Platoform.Eff exposing (..)
type alias IO eff =
{ getEmpty : () -> eff
, reduce : List (eff -> eff)
}
type Eff x y
= Unhandled (Maybe x)
| Handled y
| None
eff : x -> Eff x y
eff =
(Unhandled << Just)
type alias Effect eff =
{ getEmpty : eff -> eff
, apply : eff -> eff
}
type alias Prints x =
{ x | prints : Eff String () }
print : String -> Effect (Prints x)
print prompt =
{ getEmpty = \x -> { x | prints = None }
, apply = \x -> { x | prints = eff prompt }
}
type alias ReadsFile x =
{ x | readsFile : Eff String String }
readFile : String -> Effect (ReadsFile x)
readFile name =
{ getEmpty = \x -> { x | readsFile = None }
, apply = \x -> { x | readsFile = eff name }
}
empty : IO {}
empty =
{ getEmpty = always {}
, reduce = []
}
andThen : Effect eff -> IO eff -> IO eff
andThen a io =
{ getEmpty = a.getEmpty << io.getEmpty
, reduce = a.apply :: io.reduce
}
test =
empty
|> andThen (print "a")
|> andThen (readFile "file.txt")
|> handle .readsFil someFun
|> handle .prints (Debug.log "Logging")
|> handle .someOther -- This would fail. There was no such effect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment