summaryrefslogtreecommitdiff
path: root/lore/Network/RemoteData.hs
blob: a2c58a7984d9ead80a6c45ac7395f550ff2b6ddf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
-- | A port of Kris Jenkins' RemoteData Elm module
-- <https://github.com/krisajenkins/remotedata>.
--
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