Loadable and reloadable data
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))andThen (\a -> map f a) (succeed a) == map f (succeed a)
andThen (\_ -> fail "error") (succeed a) == fail "error"
andThen (\_ -> loading) (fail "error") == loading
andThen (\_ -> notAsked) notAsked == notAskedCombine a list of Loadable values
combineMap (\a -> succeed a) [ 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 default value if the Loadable is not loading
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