pete-murphy/elm-loadable/1.0.2

NOTE: the latest version is 2.1.0

Loadable

Loadable and reloadable data

Types

type Loadable e a

A Loadable value is either empty, failed, or succeeded, and may be loading or not

type Value e a
= Empty
| Failure e
| Success a

The value of a Loadable, aside from its loading state

Constructors

An empty value that is not loading

An empty value that is loading

succeed : a -> Loadable e a

A successful value that is not loading

fail : e -> Loadable e a

A failed value that is not loading

Combinators

map : (a -> b) -> Loadable e a -> Loadable e b

Map the value of a Loadable

map (\a -> a + 1) (succeed 1) --> succeed 2


map (\_ -> 0) (fail "error") --> fail "error"
andMap : Loadable e a -> Loadable e (a -> b) -> Loadable e b

Apply a function to the value of a Loadable

succeed (\a -> a + 1) |> andMap (succeed 1) --> succeed 2
andThen : (a -> Loadable e b) -> Loadable e a -> Loadable e b

Should 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 == notAsked
combineMap : (a -> Loadable e b) -> List a -> Loadable e (List b)

Combine 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"
fromMaybe : Maybe a -> Loadable e a

Convert a Maybe into a Loadable

fromMaybe (Just 1) --> succeed 1


fromMaybe Nothing --> notAsked
fromResult : Result e a -> Loadable e a

Convert a Result into a Loadable

fromResult (Ok 1) --> succeed 1


fromResult (Err "error") --> fail "error"

Set loading to True

toLoading loading --> loading


toLoading notAsked --> loading

Set loading to False

toNotLoading loading --> notAsked


toNotLoading notAsked --> notAsked
andMapWith : (e -> e -> e) -> Loadable e a -> Loadable e (a -> b) -> Loadable e b

Apply 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"
combineMapWith : (e -> e -> e) -> (a -> Loadable e b) -> List a -> Loadable e (List b)

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"

Destructors

withDefault : a -> Loadable e a -> a

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 --> 0
toMaybe : Loadable e a -> Maybe a

Convert a Loadable to a Maybe

toMaybe (succeed 1) --> Just 1


toMaybe notAsked --> Nothing
toMaybeError : Loadable e a -> Maybe e

Convert a Loadable to a Maybe of the error type

toMaybeError (fail "error") --> Just "error"


toMaybeError notAsked --> Nothing
isLoading : Loadable e a -> Bool

Check if the Loadable is loading

isLoading loading --> True


isLoading notAsked --> False
unwrap : Loadable e a -> Internals e a

Unwrap a Loadable

unwrap (succeed 1) == { value = Success 1, isLoading = False }


unwrap notAsked == { value = Empty, isLoading = False }
value : Loadable e a -> Value e a

Get the value of a Loadable

value (succeed 1) --> Success 1


value notAsked --> Empty