diff options
Diffstat (limited to 'src/RemoteData.elm')
| -rw-r--r-- | src/RemoteData.elm | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/src/RemoteData.elm b/src/RemoteData.elm new file mode 100644 index 0000000..7173851 --- /dev/null +++ b/src/RemoteData.elm @@ -0,0 +1,52 @@ +module RemoteData exposing (..) + +import Http + + +type RemoteData a + = Initial + | Loading + | Failure Http.Error + | Success a + + +withDefault : a -> RemoteData a -> a +withDefault default rd = + case rd of + Success value -> + value + + _ -> + default + + +map : (a -> b) -> RemoteData a -> RemoteData b +map fn rd = + case rd of + Initial -> + Initial + + Loading -> + Loading + + Failure err -> + Failure err + + Success value -> + Success (fn value) + + +andThen : (a -> RemoteData b) -> RemoteData a -> RemoteData b +andThen callback rd = + case rd of + Initial -> + Initial + + Loading -> + Loading + + Failure err -> + Failure err + + Success value -> + callback value |
