flarebyte / bubblegum-entity / Bubblegum.Entity.Outcome

An outcome is a type which borrows concepts from both Elm Maybe and Result

Basics


type Outcome value
    = Valid value
    | None
    | Warning String

Representation of an outcome which can be:

withDefault : a -> Outcome a -> Outcome a

If the outcome is Valid return the value. If the outcome is a None then return a given default value. If the outcome is a Warning then propagate this warning.

map : (a -> value) -> Outcome a -> Outcome value

Apply a function to an outcome. If the result is Valid, it will be converted. If the outcome is a Warning or None, the same value will propagate through. map sqrt (Valid 4.0) == Valid 2.0

map2 : (a -> b -> value) -> Outcome a -> Outcome b -> Outcome value

Apply a function to two outcomes, if both outcome are Valid. If not, a valid outcome and a None will propagate None, a Warning will always propagate. Two warnings will be merged

or : Outcome a -> Outcome a -> Outcome a

Like the boolean '||' this will return the first value that is positive ('Valid').

or None (Valid "str") -- Valid "str"

fromMaybe : Maybe a -> Outcome a

Convert a maybe to an outcome

fromMaybe (Just "str") -- Valid "str"

fromMaybe Nothing -- None

toMaybe : Outcome a -> Maybe a

Convert an outcome to a maybe

toMaybe (Valid "str") -- Just "str"

toMaybe None -- Nothing

Checking

check : (a -> Basics.Bool) -> String -> Outcome a -> Outcome a

Check that a valid outcome verifies the criteria otherwise raise a warning

check String.isEmpty "should not be empty string" (Valid "some text") -- Valid "some text"

check String.isEmpty "should not be empty string" (Valid "") -- Warning "should not be empty string"

checkOrNone : (a -> Basics.Bool) -> Outcome a -> Outcome a

Check that a valid outcome verifies the criteria otherwise return none

checkOrNone String.isEmpty (Valid "some text") -- Valid "some text"

checkOrNone String.isEmpty (Valid "") -- None

trueMapToConstant : a -> Outcome Basics.Bool -> Outcome a

An outcome with a true value will produce a constant outcome

trueMapToConstant [ "alpha" ] (Valid True) -- Valid ["alpha"]

trueMapToConstant [ "alpha" ] (Valid False) -- None

isValid : Outcome a -> Basics.Bool

Return true if the outcome is valid

isNone : Outcome a -> Basics.Bool

Return true if the outcome is none

isWarning : Outcome a -> Basics.Bool

Return true if the outcome is none