From 7a0e9725e691bd84cda8f6b169414581e5e1d4f1 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Thu, 29 Aug 2019 06:26:52 -0700 Subject: implement Network.RemoteData --- lore/Network/RemoteData.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 lore/Network/RemoteData.hs (limited to 'lore/Network') diff --git a/lore/Network/RemoteData.hs b/lore/Network/RemoteData.hs new file mode 100644 index 0000000..a2c58a7 --- /dev/null +++ b/lore/Network/RemoteData.hs @@ -0,0 +1,33 @@ +-- | A port of Kris Jenkins' RemoteData Elm module +-- . +-- +module Network.RemoteData where + +data RemoteData a b + = NotAsked + | Loading + | Failure a + | Success b + +-- TODO figure out Http.Error +-- type WebData a = RemoteData Http.Error a + +instance Functor (RemoteData a) where + fmap _ NotAsked = NotAsked + fmap _ Loading = Loading + fmap _ (Failure a) = Failure a + fmap f (Success a) = Success (f a) + +instance Applicative (RemoteData e) where + pure = Success + NotAsked <*> _ = NotAsked + Loading <*> _ = Loading + Failure a <*> _ = Failure a + Success a <*> b = fmap a b + +instance Show (RemoteData a b) +instance Eq (RemoteData a b) + +fromEither :: Either a b -> RemoteData a b +fromEither (Left a) = Failure a +fromEither (Right a) = Success a -- cgit v1.2.3