Loadable and reloadable data. Loadable is an extension of RemoteData to
include a loading state in the success and failure cases.
The value of a Loadable, aside from its loading state
Map the value of a Loadable
map (\a -> a + 1) (succeed 1) --> succeed 2
map (\_ -> 0) (fail "error") --> fail "error"Apply a function to the value of a Loadable
succeed (\a -> a + 1) |> andMap (succeed 1) --> succeed 2Should match the semantics of the Monad instance for
ExceptT e (MaybeT (Writer Any))succeed 1 |> andThen (\a -> succeed (a + 100)) --> succeed 101
succeed 1 |> andThen (\_ -> fail "error") --> fail "error"
fail "error" |> andThen (\_ -> loading) --> fail "error"
notAsked |> andThen (\_ -> loading) --> notAsked
loading |> andThen (\_ -> notAsked) --> loadingCombine a list of Loadable values
combineMap succeed [ 1, 2, 3 ]
--> succeed [ 1, 2, 3 ]
let
odd n =
modBy 2 n == 1
failIfOdd n =
if odd n then
fail (String.fromInt n)
else
succeed n
in
combineMap failIfOdd [ 1, 2, 3 ]
--> fail "1"Convert a Maybe into a Loadable
fromMaybe (Just 1) --> succeed 1
fromMaybe Nothing --> notAskedConvert a Result into a Loadable
fromResult (Ok 1) --> succeed 1
fromResult (Err "error") --> fail "error"Set loading to True
toLoading loading --> loading
toLoading notAsked --> loadingSet loading to False
toNotLoading loading --> notAsked
toNotLoading notAsked --> notAskedApply a function to the value of a Loadable, combining the errors.
NOTE: This is intended to be used in a pipeline, with errors accumulating left to right in the pipeline not in the order of arguments to the function. See the examples.
succeed (\a -> a ++ "b")
|> andMapWith (++) (succeed "a")
--> succeed "ab"
fail "a"
|> andMapWith (++) (fail "b")
--> fail "ab"Combine a list of Loadable values with a function that combines the errors
combineMapWith (\x _ -> x) == combineMap
let
odd n =
modBy 2 n == 1
failIfOdd n =
if odd n then
fail (String.fromInt n)
else
succeed n
in
combineMapWith (++) failIfOdd [ 1, 2, 3 ] --> fail "13"Get the success value out of Loadable or a fallback if it's not the success case
withDefault 0 (succeed 1) --> 1
withDefault 0 notAsked --> 0Convert a Loadable to a Maybe
toMaybe (succeed 1) --> Just 1
toMaybe notAsked --> NothingConvert a Loadable to a Maybe of the error type
toMaybeError (fail "error") --> Just "error"
toMaybeError notAsked --> NothingCheck if the Loadable is loading
isLoading loading --> True
isLoading notAsked --> FalseUnwrap a Loadable
unwrap (succeed 1) --> { value = Success 1, isLoading = False }
unwrap notAsked --> { value = Empty, isLoading = False }
A
Loadablevalue is either empty, failed, or succeeded, and may be loading or not